Reading line from file and printing line

Posted on 2003-12-01
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);
   return 0;
Question by:dkim18
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
LVL 11

Accepted Solution

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);


Author Comment

ID: 9854118
Thank you pointing out my mistake.

Expert Comment

ID: 9856029
hey! works perfectly!  are you going to award points to yourself?

Expert Comment

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).

LVL 11

Expert Comment

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,

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 166
TCP/IP Socket - connection close results in data lost 14 152
Add values of each row in an array 3 70
I could not set window to top 4 40
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

735 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