Solved

Reading line from file and printing line

Posted on 2003-12-01
5
759 Views
Last Modified: 2010-04-01
I am testing this file and cout<<line<<endl; doesn't print anything. After I excuete a.out, input.dat becomes empty file too!! Why is that?

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

int main()
{
     
   ifstream inFile("input.dat");
  string line;
  while (!inFile.eof())
  {
    getline(inFile, line);
    cout<<line<<endl;
  }
   return 0;
}
0
Comment
Question by:dkim18
5 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 100 total points
ID: 9854101
Make sure you close the file after you are done with it:

ifstream inFile("input.dat");
  string line;
  while (!inFile.eof())
  {
    getline(inFile, line);
    cout<<line<<endl;
  }
  inFile.close();

-bcl
0
 

Author Comment

by:dkim18
ID: 9854118
Thank you pointing out my mistake.
0
 
LVL 1

Expert Comment

by:joesp
ID: 9856029
hey! works perfectly!  are you going to award points to yourself?
0
 
LVL 3

Expert Comment

by:RJSoft
ID: 9858227
getline can be a little screwy. getline keeps reading until it finds the \n char in a string of text, then it replaces the \n with a null ('\0') to create a string.

If the value of line is too small to read in the line requested then getline returns a junk string. (what I seem to remember anyway) , so the solution was to use a overly large value for line. Usually 80 chars is the max for most editing windows but wrap arround text can change that possibility. So use something like 200 or 300. That ensures that getline will find the \n (newline).

RJ
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9858493
RJ -

There are two different getline functions. One, defined as part of istream, behaves exactly as you describe. It has the signature (simplified istream type for clarity)

std::istream &  std::istream::getline(char * buffer, unsigned int length, char delimiter = '\n');

This version reads into a fixed size buffer and, as you say, is prone to buffer overrun problems

The version used in the program fragment above is the other version of getline, the one defined along with std::string:

std::istream & getline(std::istream & in, std::string buffer, char delimter = '\n');

This version reads into a standard string which grows dynamically to accomodate the input stream.

Hope this is clear enough,
-bcl
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

778 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