How to read a file line-by-line?
-
Is there an easy way to read a file line-by-line, not using MFC? I looked at fread, ReadFile, and ofstream, but I couldn't find a "ReadLine" function in any of them. Thanks!
Take a look at
ifstream::getline
andstring::getline
. - Mike -
Is there an easy way to read a file line-by-line, not using MFC? I looked at fread, ReadFile, and ofstream, but I couldn't find a "ReadLine" function in any of them. Thanks!
fgets
-
Is there an easy way to read a file line-by-line, not using MFC? I looked at fread, ReadFile, and ofstream, but I couldn't find a "ReadLine" function in any of them. Thanks!
You can use getline()
#include
void main()
{
char buffer[256];
ifstream file;
file.open("c:\\autoexec.bat", ios::in | ios::nocreate);if (file.good())
{
while(file.peek() != EOF)
{
file.getline(buffer,255);
cout << buffer << endl;
}
file.close();
}
else
cout << "error opening autoexec.bat" << endl;}
the default delimiter for getline is \n, see msdn for more details. Hope this helps sledge
-
fgets
-
Is there an easy way to read a file line-by-line, not using MFC? I looked at fread, ReadFile, and ofstream, but I couldn't find a "ReadLine" function in any of them. Thanks!