ok, so I have read some tutorials on file streams, but i am still confused. I have made a program that edits a .txt (inputted by user) file, but my goal is to search an entire file for a string, and every time the string its found, I want it to be changed to a different string.

Some problems I have run into:

-it just skips through cin.getline(File, 255) without waiting for the user's input
- if I fout (fout is my ofstream thing) something, it deletes what is already in the file.

I pretty much need more info on fstreams. The tut i read was too complicated (i am 13 years old)

p.s. Sorry for the bad english, its 2:00 am here

Recommended Answers

All 5 Replies

Member Avatar for iamthwee

Post your code.

>-it just skips through cin.getline(File, 255) without waiting for the user's input
Probably because you have a cin>> x; somewhere earlier in your code and a newline is left in the stream for getline to terminate successfully on right away. Nothing is getting skipped, it's working as written. You just didn't account for extraneous characters.

>- if I fout (fout is my ofstream thing) something, it deletes what is already in the file.
Yes, that's how writing to a file works.

>-it just skips through cin.getline(File, 255) without waiting for the user's input
Probably because you have a cin>> x; somewhere earlier in your code and a newline is left in the stream for getline to terminate successfully on right away. Nothing is getting skipped, it's working as written. You just didn't account for extraneous characters.

>- if I fout (fout is my ofstream thing) something, it deletes what is already in the file.
Yes, that's how writing to a file works.

so how do I fix the getline problem

also, how do I not delete what already exists in the file, and just edit it?

>so how do I fix the getline problem
The simplest solution is to flush the input stream. The best solution is to avoid formatted input and read everything as a string, then do the conversion yourself with something like stringstream or boost::lexical_cast.

>how do I not delete what already exists in the file, and just edit it?
Provided the information you're going to edit/remove/add is exactly the same length as the old information, you can find where you want to edit and seek to that location. You're still going to overwrite what's there, but at least you'll get the correct result. If the lengths are different, you have little choice but to overwrite the entire file past the point where you want to edit.

thanks, ill just flush the input system, because i have no idea what you just said about string stream or whatever.

+rep for you, your a great help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.