Thread: Reading input from a data file

  1. #1
    Registered User
    Join Date
    Sep 2004

    Question Reading input from a data file

    Hello everyone! I'm new to C and its been about a year since I took C++. I have a question concerning the proper syntax for reading input from a data file. This is what I have:


    while ( ( fscanf(inputptr, "%c, %s, %s, %d, %i\n", &action, name, town, &rain, &temperature ) ) != EOF


    I would appreciate any assistance.

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    that looks good enough to me. the proper way to use code tags is [code ] to begin and [/code ] to end the block

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    your missing a ')' at the end of your while statment. probably a copy and paste error.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Try this instead:
    while ( ( fscanf ( inputptr, " %c, %[^,], %[^,], %d, %i", &action, name, town, &rain, &temperature ) ) == 5 )
    And even better, when reading string data with *scanf, specify a size. For example, if name and town are arrays of size 20 then you would say
    " %c, %19[^,], %19[^,], %d, %i"
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from a file and using the data
    By Ste_Mulv in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 07:44 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Reading from an input file + processing
    By Mooncow in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 02:45 AM
  4. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  5. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM