Taking input from text file..

This is a discussion on Taking input from text file.. within the C Programming forums, part of the General Programming Boards category; Hi guys, Ultra nooby question here. Just doing an assignment, and I've got my array setup working and printing out ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    10

    Cool Taking input from text file..

    Hi guys,

    Ultra nooby question here. Just doing an assignment, and I've got my array setup working and printing out as it should, but I'm using scanf to take input.. and the input file we're given starts with string column headers (which we don't need to store).

    scanf goes EOF when it reads these characters first, and hence I'll just get a bunch of garbage when it prints with whatever's in memory. What should I be using to take the number inputs to integers, while ignoring the text at the top? I can't for the life of me remember how this is done. This is what I have storing the input:

    Code:
        for (i=0; i < ROWS; i=i+1) {
    
            scanf("%d", &month[i]);
            scanf("%d", &year[i]);
            scanf("%d", &postcode[i]);
            scanf("%d", &bedrooms[i]);
            scanf("%d", &price[i]);
    
            }
    And this is the input file:

    Code:
    Month Year Postcode Bedrooms   Price
      12   2008  2033     3       783000
       7   2009  2034     4       777000
      12   2008  2034     4       821000
       3   2008  2034     3       781500
       2   2009  2034     4       847000
      11   2008  2032     3       729000
       7   2008  2034     4       768500
      12   2009  2034     4       868000
       4   2009  2033     5       746000
       2   2008  2034     2       831500
    I hope my question makes sense!
    Last edited by Passa; 09-14-2010 at 07:31 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Read and ignore the first line. Have a single fgets to read the first line, don't store/parse the data. Then continue on as normal with your loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    Wow, fast reply! Thanks, using fgets ignores the line like I want.

    One other small query - how do I avoid storing it like you suggest? I get a compile error for lack of arguments when I try not specifying a character array. I've got this at the moment:

    Code:
    char dummystr[50];
        
    
    fgets(dummystr, 50, stdin);

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Well, you will have to store it somewhere with fgets but you do it into a scratch/dummy variable that you will either overwrite soon after or one that you don't do any further processing to/with. You can ignore the first line with scanf however, not even needing such a scratch/dummy variable to store something you'd never even use:

    Code:
    scanf("%*[^\n]");
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    scanf("%*[^\n]");
    Wow good use of scanf()!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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