Ivaandr
asked on
problem with read line from ifstream file
I have been trying to read lines (ascii chars with backspaces) from textfile (Squid access.log) using fstream:
char log_line[255];
ifstream log_file("access.log");
while(!log_file.eof()){
log_file>>log_line;
cout<<log_line<<endl;
}
log_file.close();
But if original line from file is like "1046385006.546 2001 192.168..." , then variable log_line is "1046385006.546".
How to read full line with BACKSPACES?
char log_line[255];
ifstream log_file("access.log");
while(!log_file.eof()){
log_file>>log_line;
cout<<log_line<<endl;
}
log_file.close();
But if original line from file is like "1046385006.546 2001 192.168..." , then variable log_line is "1046385006.546".
How to read full line with BACKSPACES?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good advice for using (string::)getline. And using (fstream::)getline is also accepted.
ASKER
Sorry, how to remove my current question?
The questions are not removed, once you accept a comment as answer it turns into a PAQ for future reference.
So, you do not have to do anything more.
So, you do not have to do anything more.
I have a similar question...
I have to read in lines from a file and parse it to extract the data.
For example I read in a line "0 11 2 5", I can do this with a "getline", but then I have to assign 0 to a variable 11 to another variable, 2 to another and 5 to another. These variables are all of data type "int".
I've tried doing a "get" instead of "getline", but that reads in one character at a time. What I need is a way to read int. Any ideas or suggestions?
Thanks,
raj
I have to read in lines from a file and parse it to extract the data.
For example I read in a line "0 11 2 5", I can do this with a "getline", but then I have to assign 0 to a variable 11 to another variable, 2 to another and 5 to another. These variables are all of data type "int".
I've tried doing a "get" instead of "getline", but that reads in one character at a time. What I need is a way to read int. Any ideas or suggestions?
Thanks,
raj
2) use getline
char buf[256]; // max line in file
logfile.getline(buf, 256);