So what I want to do is there is some data in one of the files called data.txt. The file has lines separated by periods . What I want to do is read the data from the data.txt file and store in another file Line by Line that means in this fashion
Line 1 : some text some text some
Line 2 : some text some text some
A single line could be either 75 characters long or until a period (.) is reached .
Content of the data.txt file The default behaviour of ifstream type stream (upon opening files) allows users to read contents from the file. if the file mnode is ios::in only then readin is performed on a text file and if the file mode also includes ios::binary along with ios::in then, reading is performed in binary mode. No tranformation of characters takes place in binary mode whereas specific treanformations take place in text mode.
Line 1 : he default behaviour of ifstream type stream (upon opening files) allows u
Line 2 : ers to read contents from the file
Line 3 : if the file mnode is ios::in only then readin is performed on a text file
Line 4 : and if the file mode also includes ios::binary along with ios::in then, re
Line 5 : ding is performed in binary mode
Line 6 : No tranformation of characters takes place in binary mode whereas specifi
Line 7 : treanformations take place in text mode
Line 8 :
As you can see the first characters are gone . I know thats because I use fin.get(ch) in the while condition, but I have no idea what else to do. Also I tried !fin.eof() as condition but this resulted into an infinite loop. What do I do to test if there is no more data left to be read in the file ?
But then how do I test whether period comes first or 75 characters are read ? Cuz the line can either be 75 characters long or as long as a period is not encountered
If you just want to copy the file, open in binary mode and use read/write. Don't use text methods like getline(), the enod-of-line stuff varies between systems and is not always \n.
OK, back to your original question and what Pogrady was saying;
When you get input or output from a file there is an invisible variable that tells your program how far along it has gotten and where the next character to read is. Your while(fin.get(ch)) argument does test if there is something to be read, but it also tells that invisible variable that it has read another letter and must increase by one.
What pogrady's while(fin.good()) option does is it checks if there is still something to be read, but it doesn't increase that invisible variable. (It actually checks that the file is open, no errors have occurred, and the end of the file hasn't been reached)
So what you should do is replace the line "while(fin.get(ch))" with "while(fin.good())"
So do this and tell us how it works for you.
As a side note, if you want to get information about this invisible variable I told you about then look up seekp(Get the possition of the put pointer), seekg(Get the position of the get pointer), tellp(Set the position of the put pointer), and tellg(set the position of the get pointer).
fin.good() results in the output only upto Line 2 . (reference : first post )
I have provided the actual file and program, I will really appreciate if anyone checks it on their computer and helps me out .