Reading Lines with Fgets and a bit of C++ {Novice Programmer}

Discussion in 'C Programming' started by AMT2K5, Jun 29, 2005.

  1. AMT2K5

    AMT2K5 Guest

    Hello.
    I have a file (for a school assignment) with the following format and
    delimiter format. Each record in the file has the following format:
    123423454567987,29873,James,Harry,St. Louis,416-555-5555;
    "accountNumber,balance,lastName,city,phoneNumber;" Each record is
    guranteed to be no longer than 350 characters. "balance" is no longer
    than 20 characters, "accountNumber" is exactly 15 characters and the
    total number of characters in "lastName,firstName,city,phoneNumber;" is
    no longer than 315 characters. Copy the fields in this record to the
    appropriate data member of the first empty Account in the array
    savings, using all the rules for copying (initializing).

    Would fgets work in this situation (I have never used fgets)? How would
    you guys do it?

    Thanks in advance.
    AMT2K5, Jun 29, 2005
    #1
    1. Advertising

  2. AMT2K5

    Eric Sosman Guest

    AMT2K5 wrote:
    > Hello.
    > I have a file (for a school assignment) with the following format and
    > delimiter format. Each record in the file has the following format:
    > 123423454567987,29873,James,Harry,St. Louis,416-555-5555;
    > "accountNumber,balance,lastName,city,phoneNumber;" Each record is
    > guranteed to be no longer than 350 characters. "balance" is no longer
    > than 20 characters, "accountNumber" is exactly 15 characters and the
    > total number of characters in "lastName,firstName,city,phoneNumber;" is
    > no longer than 315 characters. Copy the fields in this record to the
    > appropriate data member of the first empty Account in the array
    > savings, using all the rules for copying (initializing).
    >
    > Would fgets work in this situation (I have never used fgets)? How would
    > you guys do it?


    fgets() makes sense if each "record" is a complete
    line, but is less appropriate if the semicolon is the
    only delimiter:

    ...,St. Louis,416-555-555;987654321654321,...

    If each record is in fact a line, you could use
    fgets() this way:

    #include <stdio.h>
    #define MAXLEN 350
    ...
    char buff[MAXLEN+1+1]; /* extra for '\n' and '\0' */
    FILE *input = fopen(...); /* the input stream */
    if (input == NULL)
    die(); /* fopen() failed */
    while (fgets(buff, sizeof buff, input) != NULL) {
    /* process the line in `buff' */
    }
    if (ferror(input))
    die(); /* input error */
    fclose (input);

    --
    Eric Sosman, Jun 29, 2005
    #2
    1. Advertising

  3. AMT2K5

    AMT2K5 Guest

    Contents of a3.dat

    123423454567987,29873,James,Harry,St.
    Louis,416-555-5555;223423454567987,198745,Jones,Beth,Toronto,416-555-5556;323423454567987,2349,Ng,Wei,Montreal,416-555-5557;423423454567987,9234617,Woo,Charles,Winnipeg,416-555-5558;523423454567987,2534,DeJesus,Pancho,Edmonton,416-555-5559;623423454567987,543876,Smith,Bob,Charlottetown,416-555-5544;723423454567987,1234,Kasim,Vladislov,Halifax,416-555-5566;823423454567987,98765,Yamaha,David,Vancouver,416-555-5577;923423454567987,26486,Lee,Jim,Calgary,416-555-5588;113423454567987,83456,Baker,Susan,St.
    Louis-de-ha-ha,416-555-5599;133423454567987,29873,James,Harry,St.
    Louis,416-555-5555;
    AMT2K5, Jun 29, 2005
    #3
  4. AMT2K5

    Richard Bos Guest

    "AMT2K5" <> wrote:

    > I have a file (for a school assignment) with the following format and
    > delimiter format. Each record in the file has the following format:
    > 123423454567987,29873,James,Harry,St. Louis,416-555-5555;
    > "accountNumber,balance,lastName,city,phoneNumber;" Each record is
    > guranteed to be no longer than 350 characters. "balance" is no longer
    > than 20 characters, "accountNumber" is exactly 15 characters and the
    > total number of characters in "lastName,firstName,city,phoneNumber;" is
    > no longer than 315 characters. Copy the fields in this record to the
    > appropriate data member of the first empty Account in the array
    > savings, using all the rules for copying (initializing).
    >
    > Would fgets work in this situation (I have never used fgets)?


    Yes. Declare an array of 352 chars (350 plus line-ending '\n' plus
    terminating '\0'), and use fgets() on it. Use the return value of
    fgets() to see whether you have reached the end of the file or have read
    a valid line.
    Then you can use whatever you like to split the fields from the line:
    sscanf() could work, provided the fields can't contain spaces; so could
    strtok(), provided all fields are guaranteed to be present and not
    empty. If you can have both empty fields and spaces within fields (e.g.,
    if you have customers called "Jon Bob" _and_ corporate customers with no
    first name), you could use strchr().

    Solid code, of course, would either not assume that all lines are less
    then 350 characters, or, if this were an innate requirement of the file
    (e.g., because of database field sizes), reject longer lines with a
    warning. fgets() can help with that, as well, since if you've read at
    most

    Richard
    Richard Bos, Jun 29, 2005
    #4
  5. AMT2K5

    Default User Guest

    AMT2K5 wrote:
    > Hello.
    > I have a file (for a school assignment) with the following format and
    > delimiter format.



    Where does the "bit of C++" mentioned in the subject come into this?
    Are you working in C++? If so, why aren't you posting to comp.lang.c++?





    Brian
    Default User, Jun 29, 2005
    #5
  6. AMT2K5

    AMT2K5 Guest

    I figure I have to use Fgets which is a stdio standard C function.
    Where C++ comes into play is where I am sending the recieved
    information from the dat file and sending it off to data members of a
    instantiated class of a class of arrays.
    AMT2K5, Jun 29, 2005
    #6
  7. AMT2K5

    AMT2K5 Guest

    Any other help guys?

    AMT2K5 wrote:
    > I figure I have to use Fgets which is a stdio standard C function.
    > Where C++ comes into play is where I am sending the recieved
    > information from the dat file and sending it off to data members of a
    > instantiated class of a class of arrays.
    AMT2K5, Jul 6, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    Little job for novice programmer

    Roedy Green, Sep 9, 2003, in forum: Java
    Replies:
    0
    Views:
    338
    Roedy Green
    Sep 9, 2003
  2. AMT2K5
    Replies:
    6
    Views:
    730
    Larry I Smith
    Jun 9, 2005
  3. Justme
    Replies:
    9
    Views:
    595
    clayne
    Oct 1, 2006
  4. Replies:
    43
    Views:
    1,873
    U S Contractors Offering Service A Non-profit
    Nov 30, 2006
  5. AMT2K5
    Replies:
    12
    Views:
    618
    AMT2K5
    Jul 6, 2005
Loading...

Share This Page