fscanf

This is a discussion on fscanf within the C Programming forums, part of the General Programming Boards category; can someone please tell me how to read in a record that has variable length fields. The fields are delimited ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    fscanf

    can someone please tell me how to read in a record that has variable length fields. The fields are delimited by commas.

    The fields to be read are:
    Student Number (5 digits)
    Student Name (25 characters max.)
    Curriculum Code (3 characters)
    8 Class Work Grades (2 characters max., may be : a+, a, a-, b+, b, b-, c+... f)
    4 Test Grades (3 digits max.)
    Final Exam (3 characters max., [not a mistake!!])

    Sample Record:
    43695,robert hilling,dpi,a+,c-,c,b+,b,d,a,f,100,88,93,82,93

    Any Help would be greatly appreciated.

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Try this:
    Code:
    char line[200];
    
    char studnum[5];
    char studnam[25];
    char curr[3];
    char grades[2][8];
    int test[4];
    char final[3];
    
    FILE *fp;
    
    fp = fopen(filename, "r"); /* for "filename", insert a file or var */
    
    if (fp == NULL)   /* Invalid file */
        printf("Invalid file\n");
    
    fgets(line, sizeof(line), fp); /* get a line from the file */
    fscanf(line, "%s, %s, %s, %s, %s, %s, %s, %s, 
    %s, %s, %s, %d, %d, %d, %d, %c, %c, 
    %c", studnam, studnam, curr, grades[0], 
    grades[1], grades[2], grades[3], grades[4], 
    grades[5], grades[6], grades[7], &test[0], 
    &test[1], &test[2], &test[3], &final[0], &final[1], 
    &final[2]);
    I know, I know. A really, really big fscanf, but you are asking a lot from one single line. Due to the length, there might be a bug. I didn't bring it through a compiler. But, logically, this should work.

    Does this help?

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >fscanf(line, "%s, %s, %s, %s, %s, %s, %s, %s, ...
    I think you meant sscanf(). fscanf takes a file pointer as the first argument, an array in its place will probably cause an error.

    >can someone please tell me how to read in a record that has variable length fields.
    If the fields will have varying lengths, then the only way to read them in properly is to either read each string into a buffer, get the size of it and then allocate memory and place it in it's variable, or parse the string any number of ways to get what you need, or just declare all of your arrays to be a maximum length so that they will hold the longest string read from the file.
    Unless this is a large program that requires as little memory usage as possible, then I would suggest the easiest way which is to make really long arrays.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    I think you should use a structure, then you could read it all with ease
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> I think you meant sscanf(). fscanf takes a file pointer as the first argument, an array in its place will probably cause an error. <<

    Ooops!!! I don't know what I was thinking
    1978 Silver Anniversary Corvette

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    Thanx - about using a structure

    Thanx all especially garfield this helps alot. C_coder. I thought about using a structure but wasn't sure how to read it in. Would I just read the whole line in as a string.

    ie:
    fscanf(Filein, "%s", strucGrade);

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Reading a structure in is really easy, just use fread
    fread(record, sizeof record, 1, IN);

    -Prelude
    Last edited by Prelude; 12-08-2001 at 05:40 PM.
    My best code is written with the delete key.

  8. #8
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Are you reading from a file? I think so as you use filein.
    Yes you read the structure in with one function but you use fread.
    Code:
    fread(struct_ptr, sizeof(struct), 1, filein);
    Here fread is reading the number of bytes returned by the sizeof into struct_ptr from filein, the 1 specifys how many structures you wish to read, if you had an array of 20 structs you could read them all with that 1 statement just by changing the 1 to a 20.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    When I Read get alot of junk

    I get alot of junk along with the data I am not getting this please help.

    #include<iostream>
    #include<stdio.h>
    using namespace std;


    int main()
    {
    char line[200];
    char studnum[5];
    char studnam[25];
    char curr[3];
    char grades[2][8];
    int test[4];
    char final[3];

    FILE *fp;
    fp = fopen("StuGrade.txt", "r"); /* for "filename", insert a file or var */

    if (fp == NULL) /* Invalid file */
    printf("Invalid file\n");

    fgets(line, sizeof(line), fp);
    sscanf(line, "%s%*,%[abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ]%*,"
    "%s,%s,%s,%s,%s,%s,%s,%s,%s,%d,%d,%d,%d,%c,%c,%c," ,
    studnam, studnum, curr, grades[0],
    grades[1], grades[2], grades[3], grades[4],
    grades[5], grades[6], grades[7], &test[0],
    &test[1], &test[2], &test[3], final);
    printf ("%s%s%s%s%s%s%s%s%s%s%s%d%d%d%d%c%c%c",
    studnam, studnum, curr, grades[0],
    grades[1], grades[2], grades[3], grades[4],
    grades[5], grades[6], grades[7], &test[0],
    &test[1], &test[2], &test[3], &final[0], &final[1], &final[2]);
    cin.get();
    return 0;
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The fields to be read are:
    Student Number (5 digits)
    Student Name (25 characters max.)
    Curriculum Code (3 characters)
    8 Class Work Grades (2 characters max., may be : a+, a, a-, b+, b, b-, c+... f)
    4 Test Grades (3 digits max.)
    Final Exam (3 characters max., [not a mistake!!])


    Sample Record:
    43695,robert hilling,dpi,a+,c-,c,b+,b,d,a,f,100,88,93,82,93
    sscanf("%5d,%23[^,],%3s,%2s,%2s,%2s,%2s,%2s,%2s,%2s,%2s,%3d,%3d,%3d,% 3d,%3d", ...variables go here... );

    That should do the trick. I only question the second argument. If I recall correctly, "%s" reads until a space is encountered, which is why I suggested you try the [^,]. If that doesn't work, you may have to play with that parameter a bit. Should work though.

    Garfield:

    I know, I know. A really, really big fscanf, but you are asking a lot
    from one single line. Due to the length, there might be a bug.
    No, that won't be the problem.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> No, that won't be the problem. <<

    What I meant was that I may have made a typo along the way.
    1978 Silver Anniversary Corvette

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    Talking Thanx All

    Thanks for all the help I really appreciate. Finally got it working. Just have to create a couple of classes and I am good to go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 10:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 01:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21