fscanf

This is a discussion on fscanf within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me how to read variable length fields from a sequential file using fscanf. The fields are delimited ...

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

    fscanf

    Can anyone tell me how to read variable length fields from a sequential file using fscanf. The fields are delimited by commas.
    Fields to be read in 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!!])

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    lets assume that the file is set up like thiS:
    12345,ASDFG,FRT,A-,A-,A,B+,A-,F,D-,F,AB-,AB-,AB-,AB-
    22222,etc.

    Thus it is has the requisite fields and is comma delimited. If that's the way the file is set up then I think you would read the entire line into a holding string using fscanf, parse the line into substrings using strtok() with comma as the delimiterm and finally convert the strings containing numeric data into appropriate data type using atoi(), atol(), or atof().

    thus to start I would try this:

    char buffer[100];
    FILE * pFile;
    pFile = fopen("sample.txt", "r");
    fscanf(pFile, "%s", buffer);
    //etc.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    On a side note, please don't cross post and double post, it's very annoying to open up a new thread and find that it's the one you just answered. Post once and only once, then be patient and the answers will come.

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

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

    Unhappy sorry

    Sorry about the cross post!

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