?
Solved

problem with read line from ifstream file

Posted on 2003-02-28
6
Medium Priority
?
7,608 Views
Last Modified: 2008-02-26
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
Comment
Question by:Ivaandr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:Mafalda
ID: 8040895
1) Don't you mean BLANKS ?
2) use getline

char buf[256]; // max line in file
logfile.getline(buf, 256);
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 8040981
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
 

Author Comment

by:Ivaandr
ID: 8062541
Good advice for using (string::)getline. And using (fstream::)getline is also accepted.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Ivaandr
ID: 8062567
Sorry, how to remove my current question?
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 8063169
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
 

Expert Comment

by:nidwannaya
ID: 12804551
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question