Solved

Reading line from file and printing line

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

791 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