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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

input whole line from file: c++

Reply to Message Icon

Name: moltar
Date: September 29, 2003 at 21:49:40 Pacific
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 -_-;;



Sponsored Link
Ads by Google

Response Number 1
Name: Ronin1
Date: September 30, 2003 at 10:55:07 Pacific
Reply:

As I recall, it *should* be something like

ifstream input;
input.getline(..);


0

Response Number 2
Name: moltar
Date: September 30, 2003 at 15:33:43 Pacific
Reply:

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;
}


0

Response Number 3
Name: moltar
Date: September 30, 2003 at 17:24:29 Pacific
Reply:

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


0

Response Number 4
Name: Ronin1
Date: September 30, 2003 at 23:03:16 Pacific
Reply:

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?


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Encypting with C Program VC++ Mid function/Excepti...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: input whole line from file: c++

Batch:Getting random line from file www.computing.net/answers/programming/batchgetting-random-line-from-file/16304.html

batch extract lines from files www.computing.net/answers/programming/batch-extract-lines-from-files/16749.html

Delete blank lines from a text file www.computing.net/answers/programming/delete-blank-lines-from-a-text-file/14525.html