I am trying to write some code that reads a file line by line and then outputs the text of each line using cout.

Here is what I have so far:

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>

using namespace std;

int main()
{
    ifstream input("test.txt");
    if (input.fail())
    {
        cerr << "File not found!";

        return -1;
    }
    else
    {
        string str;

        while(getline(input,str))
        {
            istringstream(str);

            cout << str;
            cin.get();
        }

        return 0;
    }
}

The problem I have is that it just outputs "0x22fe84" lots of times. What am I doing wrong?

Thanks,

James

Recommended Answers

All 6 Replies

Why are you creating the istringstream object in between? Just print the string. And move the cin.get outside the loop.

A quick google turned this out (I just added ' system("pause"); ') :

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

int main () 
{
  char buffer[256];
  ifstream myfile ("test.txt");

  while (! myfile.eof() )
  {
	myfile.getline (buffer,100);
	cout << buffer << endl;
  }
  system("pause");
  return 0;
}

Seemed to work for me :)

I got beat to posting :(

Here is an alternate that I worked on for you. Put comments to help explain it.

Make sure that you have one extra blank line at the end of your file so the last line is read

// input.cpp : Basic ifstream function
//

#include<iostream>
#include<string> // support for strings
#include<fstream> // support for I/O

using namespace std;

int main()
{
    ifstream input;  // define ifstream
	input.open("test.txt"); // open using ifstream object
    if (!input.is_open()) // if file is not opened
    {
        cerr << "File not found!";
		system("PAUSE"); // halt program to see that it worked
        return -1;
    }
    else
    {
        string str;
		getline(input,str); // get first line
        while(input.good()) // input is valid
        {
            cout << str << endl;
            getline(input,str); // get next line of input
        }
		system("PAUSE"); // halt program to see that it worked
        return 0;
    }
}

Why are you creating the istringstream object in between? Just print the string. And move the cin.get outside the loop.

Come to think of it, no idea?!?!? Fixed anyway. Thanks.

Well done! I haven't seen this bug/error before. Anyway it made me smile.

What you have done, is simple use istringstream in a very odd way, the effect is to create a local variable

std::string str;
while(getline(input,str))
  {
    istringstream(str);   // This line is the proble:
    // str is a istringstream JUST within the scope of the WHILE:  
    cout << str;   // OUTPUT the STATE of the LOCAL istringstream
    cin.get();

You could replace it with an instance of the object, e.g.

while(getline(input,str,'\n'))
  {
    istringstream DX(str);
    cout <<"String == "<< str<<std::endl;
    cin.get();
  }

Then you can do whatever you wish to do with the object DX, which is the instance of the istringstream, and str is still the string in all scopes of your program.

Even though the thread is marked solved I would like to point out that your earlier code was much nicer. When you changed it you added a few things which are better avoided, like eof() and system("pause"). If you want to know why, read the link system("pause") and there is a similar article there on use of eof.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.