Passing data from file to arrays

This is a discussion on Passing data from file to arrays within the C Programming forums, part of the General Programming Boards category; sscanf and sprintf stand for "string scan format" "string print format", just like fscanf/fprintf stand for "file scan format", "file ...

  1. #16
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    sscanf and sprintf stand for "string scan format" "string print format", just like fscanf/fprintf stand for "file scan format", "file print format". It's all the same thing only that the first two read and write to a string and the other two read/write in a file.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  2. #17
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The idea is that if you use fgets you will read whole lines at a time in a string containing the comma separated values. sscanf will allow you to read from that string. Alternatively, you can use strtok to separate the tokens.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 01:23 AM
  2. Storing # from Data File in Arrays?
    By dakarn in forum C Programming
    Replies: 7
    Last Post: 11-13-2008, 09:15 PM
  3. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  4. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-30-2005, 12:24 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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