Computing.Net > Forums > Programming > input whole line from file: c++

input whole line from file: c++

Reply to Message Icon

Original Message
Name: moltar
Date: September 29, 2003 at 21:49:40 Pacific
Subject: input whole line from file: c++
OS: windows xp prof
CPU/Ram: 700mhz 256 mb
Comment:


this is what total needs to be done
http://www.cs.uakron.edu/~margush/210/Prog2.htm

what i can't figure out is how to input a whole line from a file into a character array. all my text shows is how to get a single word. i'm thinking it's cin.getline, but i'm not sure how the code would be written

yes it is late -_-;;


Report Offensive Message For Removal

Response Number 1
Name: Ronin1
Date: September 30, 2003 at 10:55:07 Pacific
Subject: input whole line from file: c++
Reply: (edit)

As I recall, it *should* be something like

ifstream input;
input.getline(..);


Report Offensive Follow Up For Removal

Response Number 2
Name: moltar
Date: September 30, 2003 at 15:33:43 Pacific
Subject: input whole line from file: c++
Reply: (edit)

how's it all look together?
i know this is wrong cause i only get a blank line =/

#include <iostream>
#include <fstream>
using std::ios;

int main(){
const int max = 14;
char isbn[max];
std::ifstream input("somebook.dat",ios::in);
input.getline(isbn,max,'\n');
std::cout << isbn << std::endl;
return 0;
}


Report Offensive Follow Up For Removal

Response Number 3
Name: moltar
Date: September 30, 2003 at 17:24:29 Pacific
Subject: input whole line from file: c++
Reply: (edit)

okay, now i got it to input, but it doesn't see the eof and goes inf. -_-;
stupid dat files


Report Offensive Follow Up For Removal

Response Number 4
Name: Ronin1
Date: September 30, 2003 at 23:03:16 Pacific
Subject: input whole line from file: c++
Reply: (edit)

I've been playing with C for months, so I've forgotten a lot of cpp. However, I *think* something like this will allow you to read the entire file. I haven't checked your program link, so I dunno what the program requires. :)

int ch;

while((ch = input.peek()) != EOF)
{
input.getline(isbn, max, '\n');
// do something with isbn
}

input.close();

make sure you're not trying to read in more than your array can hold. Since you're using cpp, why not use the string class instead?


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: input whole line from file: c++

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software