tags:

views:

95

answers:

1

How do I read into an array a string with space in it, delimited with semicolons from a textfile in the C programming language?

***from textfile***
"My Record; My Second Record; My Third"

. . .

    fopen ...
    for(i = 0; i < 3; i++) {
    fscanf_s(myFile, "%s", myRecords[i].title); /* this want read the records */
    }
    fclose...
A: 

The most obvious possibility would be to use the scanset conversion:

fscanf_s(myFile, "%[^;]", myRecords[i].title);
Jerry Coffin
The format should be " %[^;];" to skip the leading space and to read the semicolon. The third record in the textfile should have a semicolon terminator, consequently.
Steve Emmerson
@Steve: good point.
Jerry Coffin
Ok great and I followed your comment Steve.
Chris_45
fscanf(file, "%30[^;];", myRecords[i].title)Where 30 is the maximum number of characteres to be read, and 30 is just an example you can change it to whatever number you want.I didn't test it, it may be unaccurate.. go here for more info...
Chris_45