how to read a file line by line?

J

jtan212

Should i use the function getline?
the length of a line is unknown.
how to use it?
can someone show me a example?
 
M

mlimber

Should i use the function getline?
the length of a line is unknown.
how to use it?
can someone show me a example?

Yes, use getline, but use it with a std::string:

#include <string>
#include <fstream>
using namespace std;

int main()
{
ifstream file( "file.txt" );
// Do error checking here

string str;
while( getline( file, str ) )
{
// ...
}
return 0;
}

Cheers! --M
 
P

pibru

mlimber said:
Yes, use getline, but use it with a std::string:

#include <string>
#include <fstream>
using namespace std;

int main()
{
ifstream file( "file.txt" );
// Do error checking here

string str;
while( getline( file, str ) )
{
// ...
}
return 0;
}
does it work even if a line contains a space ?
 
J

jtan212

When I compile this program, an error happened:
d:\c++work\gltest.cpp(20) : fatal error C1010: unexpected end of file
while looking for precompiled header directive

what does this error mean?
can some one give me help? Thank u!
 
J

John Harrison

When I compile this program, an error happened:
d:\c++work\gltest.cpp(20) : fatal error C1010: unexpected end of file
while looking for precompiled header directive

what does this error mean?
can some one give me help? Thank u!

It means you told the compiler that you were using a precompiled header,
but then you didn't include one in your source file.

If you don't know what a precompiled header is then I would find the
right way to turn that feature off in your compiler. Compiler specific
questions are off topic here you should ask in a group appropriate to
your compiler if you can't figure out how to do this.

john
 
J

Jim Langston

When I compile this program, an error happened:
d:\c++work\gltest.cpp(20) : fatal error C1010: unexpected end of file
while looking for precompiled header directive

what does this error mean?
can some one give me help? Thank u!

It means you are using a Microsoft compiler and have precompiled headers
turned on. I always turn them off myself. This is OS (compiler) even
specific but I'll answer for Microsoft Visual C++ .net 2003: Project -> ...
Properties -> C/C++ -> Precompiled Headers -> Create/Use Precompiled Header
Change that to Not Using Precompiled Header
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top