Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C File Input Question

Options
  • 24-10-2004 6:54pm
    #1
    Closed Accounts Posts: 63 ✭✭


    Right so lets say i have an input file called input.txt, the file contains the following (its a sort of a list with accounts):
    001 Bart 20.35
    002 John 345.00

    basicaly i have a loop to read account information from the input file like so
    while( fscanf( input_txt, "%d\t%s\t%f", &acc_number, acc_name, &acc_balance ) != EOF )
    {
    //processing of accounts
    }

    basicaly i get into trouble when i have the following input file with surnames into the same variable?
    001 Bart Simpson 20.35
    002 John Doe 345.00

    i know fscanf only reads untill it encounters whitepace, so thats my problem.

    anybody know how i can read both the name and surname?


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    if ALL the names have surnames, why not scan in first name and surname seperately, into floats (or whatever) called first_name and sur_name.

    Then combine them using something like name = (first_name sur_name), if you know what i mean.


  • Closed Accounts Posts: 1,746 ✭✭✭0utshined


    Do you have to have use Whitespace in the file to seperate the fields?

    Could you use a comma or something and then read in line by line and search for the comma?

    If it has to be whitespace then maybe read in the whole line and count the number of spaces in it. If there are two then assign item two in the list to the name field, if three then add items two and three and assign them to the name field.

    0.


  • Closed Accounts Posts: 63 ✭✭smileyrecords


    if ALL the names have surnames, why not scan in first name and surname seperately, into floats (or whatever) called first_name and sur_name.

    Then combine them using something like name = (first_name sur_name), if you know what i mean.

    thanks i know what you mean but, the input is not guaranteed to be formated so, i cant rely on fscanf
    Do you have to have use Whitespace in the file to seperate the fields?

    Could you use a comma or something and then read in line by line and search for the comma?

    If it has to be whitespace then maybe read in the whole line and count the number of spaces in it. If there are two then assign item two in the list to the name field, if three then add items two and three and assign them to the name field.

    0.
    yea i can use underscore and stuff, but its not user friendly, and is just a way of avoiding facing the problem of reading white space in C.

    im triying at the moment to use fgets to read the whole line then, scan for whitespaces, then put each string into separate variable,


  • Registered Users Posts: 950 ✭✭✭jessy


    look into strtok(), you might also want to use tabs as delimiters.


Advertisement