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!

reading strings from file using scanf

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm having trouble reading strings from a file using scanf.

The file looks something like this:
Smith, John 8.5 8.0 9.0 9.5

It contains 15 different names.
This is how I tried to read one of the names(reading first and last name separately):
char name1[15][10];
char name2[15][10];
r = 0;

fscanf(in, "%s%s", name2[r],name1[r]);

when I do this, it gives me a Segmentation Fault. But when I remove the array subscripts I get a Bus Error. I do not know what to do, I have tried everything.
 
I may be wrong, but surely you should try reading in the following pattern?

long a,b,c,d;
fscanf(in, "%s, %s %d %d %d %d\n", name2[r],name1[r],a,b,c,d);

Otherwise, wouldn't it be looking for "SmithJohn" instead of "Smith, John 8.5 8.0 9.0 9.5"? If it is looking for just two strings, each of ten chars on that line, the line would overrun.

That's just my opinion. I'd welcome anyone else's?
 
Hey thanks for the help, but I figured out the problem. I didn't declare enough spaces of memory for the array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top