PDA

View Full Version : reading file



ewanewbie
May 3rd, 2012, 04:02 PM
Hello !

i can read the file in C++ but don't know how to read it between two points

to read the whole file



string STRING;
ifstream infile;
infile.open("file");

while(!infile.eof())
{
getline(infile,STRING);
cout<<STRING <<endl;
}


what will be logic to stop reading the file when program finds -1 character? if you do not want to write down the code just show me the logic

thanks in advance.

souravc83
May 3rd, 2012, 04:15 PM
Something like this should do (I didn't test the program, but just to give you the logic)


string STRING;
ifstream infile;
infile.open("file");

while(!infile.eof() || STRING!="-l" )
{
infile>>STRING;
cout<<STRING <<endl;
}

Basic idea is that you can overload the ">>" operator to take in formatted input.Also, by using getline, you are reading a whole line at a time, and not a single word string.

Also, are you looking for a "String" or a "character"? They are not the same in C++. A string is an array of char variables.If you are looking for a character (i.e. a char variable), then read one character at a time by defining a character instead of a string.

ewanewbie
May 3rd, 2012, 04:40 PM
Hi !

thanks for reply, i fixed it and your instruction was very helpful

it was && instead of ||

while(!infile.eof() && STRING.compare("-1")){
getline(infile,STRING);
cout<<STRING<<endl;
}

but anyways thanks for the logic it worked
Regards,

Ewa

souravc83
May 3rd, 2012, 09:24 PM
You are welcome. Indeed, it should be and '&&'. Sorry about that.
BTW, you can mark it as solved,if you feel that this has been solved.

lisati
May 3rd, 2012, 09:31 PM
Thread moved to Programming Talk.