Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

c - How to read blank-separated info, including one field with possible blanks?

A long time ago I was entrusted with this work at my university which I ended up solving in a very precarious way, I was wondering how you would do to collect information from a file and save it in a structure composed of strings. The fields of the structure are organized in different ways such that, for example, in the name field we have both the first and last names, separated by a space.

That caused a problem in the way I collected the information, since I counted by spaces to enter it in the structure, I solved it by scanning the two strings and then concatenating them.

An example of the file:

IWM14
aq201 Juan Lopez 555815964 3 FP 5.5 TP 7.5 TSO 5.9
aq197 Rosa Fernandez 555863564 3 FP 6.5 TP 8.5 AM 6.8

I was wondering how you would do to take a line of information, and classify it in the different fields of the structure, and so with all the lines of the file (omitting the header), then a linked list of structures is created, but that is a separate issue.

I add my solution:

TAlumn readData(FILE *fich)
{
    Auxiliary aux; //Auxiliary Structure
    TAlumn result;
    int i;
    char name[40];

    fscanf(fich, "%s", result.number);
    fscanf(fich, "%s", aux.name);
    fscanf(fich, "%s", aux.lastname);
    fscanf(fich, "%s", result.phone);
    fscanf(fich, "%d", &result.subjectsnumber);

    strcat(result.name, aux.name);
    strcat(result.name, " ");
    strcat(result.name, aux.surnames);

    for (i = 0; i < result.subjectsnumber; i++)
    {
        fscanf(fich, "%s", result.subjects[i].subjectName);
        fscanf(fich, "%f", &result.subjects[i].grade);
    }
    return result;
}

TAlumn structure definition:

typedef struct
{
    char subjectName[MAX];
    float grade;
} TEvaluation;

typedef struct
{
    char number[N];
    char name[MAX];
    char phone[MAX];
    int subjectsnumber;
    TEvaluation subjects[N];
} TAlumn;

Thanks for the help and the time dedicated, I am a little new to the subject of files.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I propose the pen-and-paper approach to finding the suitable algorithm.
The human eye/mind is much better in recognising names and other patterns than computers. So a basic check is "Can I reliably find the name in these lines?". Ask your self and look for assumptions.
If you find "I determine the name by ignoring the syntax-defined first string and the predictable number of letter and digit fields at the end." then write your algorithm like that.
For that you would of course have to look at the whole string, or from both ends and not attempt to go left to right...
I.e. don't use scanf() and friends. Instead read the whole thing and then parse based on your knowledge of the most strictly defined fields.
If you can make some reliable assumptions, i.e. if you have some very predictable or tightly defined fields in between, the approach should even allow to read more than one field which contains possible blanks. Less likely if the problematic fields could possibly look like containing the others. But for one problematic field between syntax-defined fields it should always be possible.

http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html is discussing that more helpfully than the cynical-seeming title suggests.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...