• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7622
  • Last Modified:

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?
0
Ivaandr
Asked:
Ivaandr
1 Solution
 
MafaldaCommented:
1) Don't you mean BLANKS ?
2) use getline

char buf[256]; // max line in file
logfile.getline(buf, 256);
0
 
nietodCommented:
That is a good improvement, but an even better solution is to use the global getline, instead of the member getline.   The global getline will read a line of _any_ length and store it in a string object, like

sring log_line;
ifstream log_file("access.log");

while(!log_file.eof())
{
   getline(log_file,log_line); // read a line of any length.
   cout<<log_line<<endl;
}
log_file.close();

0
 
IvaandrAuthor Commented:
Good advice for using (string::)getline. And using (fstream::)getline is also accepted.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
IvaandrAuthor Commented:
Sorry, how to remove my current question?
0
 
MafaldaCommented:
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.
0
 
nidwannayaCommented:
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
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now