reading multiple variable types from single line in file C

后端 未结 5 1202
无人及你
无人及你 2021-01-27 14:09

Alright I\'ve been at this all day and can\'t for the life of me get this down, maybe you chaps can help. I have a file that reads as follows

1301,1055150

相关标签:
5条回答
  • 2021-01-27 14:32

    With the fscanf format string "%d,%d,\"%[^\"]\",\"%[^\"]\",%[^,],%d,=\"%[^\"]\"" we can read a whole line's data. Besides, you have to define

    char lastname[MAX_NAME+1];
    char firstname[MAX_NAME+1];
    char subjectname[MAX_SUBSEC+1];
    int catalog;
    char section[MAX_SUBSEC+1];
    

    — the +1 to account for the terminating '\0' character.

    0 讨论(0)
  • 2021-01-27 14:33

    I have a question for you... If you want to know how to use a diamond cutter, do you try it and see, or do you consult the manual? The problem here isn't the result of your choice, but your choice itself. Believe it or not, I have answered these questions so often that I'm tired of repeating myself. The answer is all in the manual.

    Read the POSIX 2004 scanf manual — or the POSIX 2008/2013 version — and the answer this question and you'll have some idea of what you're not doing that you should be. Even fscanf code should use assert as a debugging aid to ensure the number of items read was correct.

    %[^,]s It seems as though there's a mistake here. Perhaps you meant %[^,]. The %[ format specifier is a different format specifier to the %s format specifier, hence in the presumably mistaken code there are two directives: %[^,] and s. The s directive tells scanf to read an 's' and discard it.

    0 讨论(0)
  • 2021-01-27 14:35

    What you have there is a CSV file with quoted strings, and so I would recommend you use a CSV parser (or roll your own) rather than trying to do it all with scanf (since scanf cannot deal with quotes, e.g. commas within quoted strings). A quick Google search turns up libcsv.c which you may be able to use in your project.

    0 讨论(0)
  • 2021-01-27 14:53

    1.There is a syntax error in

    while(result != NULL){
    
          printf(".....);
    
          ......
    
    }
    
    }//error
    
    1. fscanf(inputf, "%s", lastname); can't read a line ,fscanf will stop when it comes across an space
    0 讨论(0)
  • 2021-01-27 14:55

    fscanf reads one line at a time, and you can easily capture the contents of each line because your file is formatted pretty nicely, especially due to the comma separation (really useful if none of your separated values contain a comma).

    You can pass fscanf a format like you're doing with "%d" to capture an int, "%s" to capture a string (ends at white space, be weary of this when for example trying to find a name like "Annitto Grassis, which would require 2 %s's), etc, from the currently read line of the file. You can be more advanced and use regex patterns to define the contents you want captured as chars, such as "Boatswain", a sequence comprised chars from the sets {A-Z}, {a-z}, and the {"}. You'll want to scan the file until you reach the end (signified by EOF in C) so you can do such and capture the contents of the line and appropriately assign the values to variables like so:

    while( fscanf(inputf, "%d,%d,%[\"A-Za-z ],%[\"A-Za-z .]", &term, &id, lastname, firstname) != EOF) {

    .... //do something with term, id, lastname, firstname - put them in a student struct

    }

    For more about regex, Mastering Regex by Jeff Friedl is a good book for learning about the topic.

    0 讨论(0)
提交回复
热议问题