Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a string out of a file

Status
Not open for further replies.

xS73v3x

Programmer
Jan 31, 2003
11
US
This may seem kinda dumb, but i dont really know how. I have a file filled with data, which is mostly numbers, but i have to extract a few strings too. I can easily get the numbers out with

myFile >> whateverVariable;

but i tried to get the string like this:

char * str;
str = new char [ 32 ];

myFile.getline( str, 32, '\n' )

i cout'ed str and it was null.

Please help,
-Steve
 
Make sure the line you're getting isn't just the tail of a line of integers. i.e. if you have a file that looks like this:

3 43 5 21 43
Hello, world.

if you read 5 integers, you'll still be on the first line, right after the 43. When you getline at that point, you'll get the rest of that line, which is, of course, blank.

The solution is to ignore the rest of the line by calling myFile.ignore( 80, '\n' ), which ignores characters until it's gone past 80 (hopefully big enough) or until it's hit a newline.
 
Im dumb
the solution was just to do

myFile >> str;

=D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top