FSCANF format string issue

This is a discussion on FSCANF format string issue within the C++ Programming forums, part of the General Programming Boards category; The following file has the following data in it. Each record (containing various fields seperated by commas, ends with a ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    FSCANF format string issue

    The following file has the following data in it. Each record (containing various fields seperated by commas, ends with a semi-colon).

    a3.dat
    Code:
    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;
    The following function of mine has the following code.

    Code:
    void Bank::workFP(FILE* fp)
    { 
         char accountTemp[15];
         int balanceTemp = 0;
         char stringTemp[315];
         
         if(fp != NULL){
               fscanf(fp, "%15[^,],%d[^,],%315[^;];", accountTemp, &balanceTemp, stringTemp);
    
                printf("%d\n", balanceTemp);     
                puts(accountTemp);
                puts(stringTemp);
         }
         else printf("Unable to open file.\n");
                       
    }
    I am testing my code to see if it can print out the first record but I am having trouble with my fscanf format string.

    Each record in the file has the following format: "accountNumber,balance,lastName,firstName,city,phon eNumber;" Each record is guaranteed 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.

    With the above code, I recieve the output
    Code:
    29873
    123423454567987
    ☺
    Where the box is a random garbage character.

    As seen in the first record
    Code:
    123423454567987,29873,James,Harry,St. Louis,416-555-5555;
    The balance and accountnumber are correct, although I cant display the last group "James,Harry etc".

    I believe this has something to do with my fscanf format string.

    Appreciate any help in advance. Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by INFERNO2K
    The following file has the following data in it. Each record (containing various fields seperated by commas, ends with a semi-colon).
    Try it with this:
    Code:
    fscanf(fp, "%15[^,],%d,%315[^;];", accountTemp, &balanceTemp, stringTemp);
    I would probably make a loop, maybe like this:

    Code:
           while(fscanf(fp, "%15[^,],%d,%315[^;];", accountTemp, &balanceTemp, stringTemp) == 3) {
     /* print it, or whatever */
    }
    [edit]
    Make sure the char arrays are large enough to hold the fields (plus terminating zero).
    [/edit]

    Regards,

    Dave
    Last edited by Dave Evans; 07-06-2005 at 07:02 PM.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Thanks that worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

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