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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading strings into a linked list

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi all,

I have a file containing records as follows:

1634 2439484 the guess who - Get Your Ribbons On

I need to load this into a linked list containg:" int, int char[256],char[256]". the split between the 2 chars is (obviously?) at the hyphen on the input file.I have been attempting all manner of fscanf arguments along the lines of the code given below, but I can not get the fscanf to add the white spaces.

Can anyone help me with this!

regards

Tom



----snip-----------------------------------
void read_file (void) {
INPUT* lstptr;
textfile = fopen("input.txt","r");
while(!feof(textfile))

lstptr = (INPUT*) malloc(sizeof(INPUT));
fscanf(textfile,"%s %s %s %s", lstptr->field0, lstptr->field1,
lstptr->field2, lstptr>field3);
rcdCtr++;
lstptr->next = endOfInputPtr
endOfInputPtr = lstptr;
}
fclose(textfile);
std::cout << &quot;Records read in.... &quot;<< rcdCtr <<'\n';
return;
}

-----snip--------------------------------------------------------

 
there you shopuld try to use fgetch and fread. They just read bites, ignonring all the nulls, spaces, newlines...
John Fill
1c.bmp


ivfmd@mail.md
 
Try reading in each line using fgets() and then parsing the line with sscanf(), strtok() or even strtol(). fscanf() can be a tricky function to use and is often best avoided.

Also, you have a common error in your loop where you use the feof() function to test for end of file at the top. The problem is that feof() doesn't evaluate the stream passed to it, so it won't detect end of file until the code inside the loop has already detected it, in this case fscanf(). This will cause you to read the value of EOF into your variables, if this conversion is possible.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top