Question:
c++: file input program?
2011-07-08 19:15:27 UTC
I'm trying to write a pretty basic code (or so I think) that outputs the content of a file (test.txt in my case) onto the screen. The user is also then supposed to be able to input something into that file after the initial content gets printed onto the screen, but I'm just tackling the first part for now (i.e. getting the content of the file printed on the screen). I have this code so far:

#include
#include
#include
#include

using namespace std;

int main()
{

char control = 'y';//this variable is used by user to stay in or exit out of do-while loop

do
{
ifstream in_stream;
in_stream.open("test.txt");

cout << "The previous piece of advice is given below:\n\n";
char next;
while (!in_stream.eof())
{
in_stream.get(next);
cout << next;
}
in_stream.close();

cout << "Would you like to run the program again? (Y/N): ";
cin >> control;
while ((control != 'y') && (control != 'n'))
{
cout << "Incorrect input. Would you like to run the program again? (Y/N): ";
cin >> control;
control = tolower(control);
}

}while(control == 'y');

cout << "Press key to exit: ";
char exit;
cin >> exit;

return 0;
}


It works fairly well as long as the content of the file ends in a newline ("\n") but when the file ends in any other kind of character, let's say "3" for example, then the last character gets repeated twice. So for example if test.txt contains:

"1
2
3"

Then the output that's printed is:

"1
2
33"

When I actually mean to have it print:

"1
2
3"

Any suggestions? Thanks.
Four answers:
Cubbi
2011-07-09 09:03:42 UTC
Note that the part you had a problem with, which was



while (!in_stream.eof())

{

     in_stream.get(next);

     cout << next;

}



and became after "fixing"



in_stream.get(next);

while (!in_stream.eof())

{

     cout << next;

     in_stream.get(next);

}



is a common problem in learning C++. Many poorly-written textbooks suggest loops structured as "while not eof, do ...", which is almost always an error. First of all, do not test for eof(), it's not the only thing that can go wrong: test for the complete stream status. Second, stream status flags (including eof) reflect the result of the most recently attempted operation. They do not predict the future!



In your first code, after reading the last characters in the file, in_stream.eof() still returns true, as it is supposed to. Then you attempt to read another character, which fails and changes in_stream's status flags. The value of "next" remains unchanged (it was '3' in your test from the previous read, so it remains 3). Then you "cout << next" regardless whether the previous in_stream.get() succeeded or not, so you see that second 3. On the next loop iteration, in_stream.eof() returns true and the program leaves the loop.



In your second code, you're attempting to read first, and testing the status later. This is the correct sequence, but the idiomatic C++ solution would have been this:



while (in_stream.get(next))

{

     cout << next;

}
2011-07-08 19:21:07 UTC
A faster, better way would be to get the size of the file. I rarely use C++, but under C:



f=fopen("filename.txt", "rb");

fseek(f, 0, SEEK_END);

size=ftell(f);

fseek(f, 0, SEEK_SET);



Then allocate a "size+1" byte string, read the file contents into that array, set the last index to '0' and display the string. It's faster than character-by-character and you don't have to worry about the nuisances of EOF.



(Also free the string you've allocated when you don't need the string any more.)
Robin
2016-05-15 06:16:07 UTC
no, thats not possible with ASCII,. file you need a binary file. first store data in a binary file. I am ready to give you the code, but first tell me what is netpay, its forumulae?
Singh58
2011-07-08 19:31:49 UTC
Works fine for me. I tested under cygwin.



May be your compiler could have some problems?



Which version did u use? I used g++ 3.4.4


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...