Solved

Reading line from file and printing line

Posted on 2003-12-01
5
757 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now