4 Replies - 801 Views - Last Post: 28 February 2010 - 12:37 AM Rate Topic: -----

#1 caden911  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-October 09

reading a file and outputting it in c++

Posted 27 February 2010 - 04:57 PM

Hi,

I need some help with this.

The following code should be reading the file, and outputting it, but it stops when it hits spaces. I need to know how I can get it to read the entire line.

ifstream fin;
fin.open(bunFile.c_str());
string line;
while(!fin.eof()){
  fin >> line;
  cout << line << endl;
}
fin.close();



the bunFile contains an ASCII bunny, but, as I said, it stops when it encounters a space, and doesn't finish reading the line. How can I make this work?

Thanks,
caden911

Is This A Good Question/Topic? 0
  • +

Replies To: reading a file and outputting it in c++

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: reading a file and outputting it in c++

Posted 27 February 2010 - 05:04 PM

Use fin.getline().
Was This Post Helpful? 1
  • +
  • -

#3 caden911  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-October 09

Re: reading a file and outputting it in c++

Posted 27 February 2010 - 05:46 PM

Thanks. What's the syntax for that?
Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: reading a file and outputting it in c++

Posted 27 February 2010 - 06:25 PM

char *buffer = new char[500];
ifstream x;
x.getline(buffer, 500);

Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: reading a file and outputting it in c++

Posted 28 February 2010 - 12:37 AM

You may want to try this:
ifstream fin;
fin.open(bunFile.c_str());
string line;
// loop until we have lines in the file
while (getline(fin, line))
{
  // the getline function from the while loop does the extraction of line too!
  // here, only the printing function is needed!
  cout << line << endl;
}
fin.close();


This post has been edited by sarmanu: 28 February 2010 - 12:38 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1