For Loops and Fscanf

This is a discussion on For Loops and Fscanf within the C Programming forums, part of the General Programming Boards category; I am trying to get this program that will read 5 integers from a file using fscanf, and I have ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    For Loops and Fscanf

    I am trying to get this program that will read 5 integers from a file using fscanf, and I have to somehow embed that into a for-loop. I understand how fscanf works, but have no idea on how to embed it into a loop. testdata9 has the 5 integers.


    I have no idea what to do after this.
    Code:
    #include <stdio.h>
    
    int main( ){
    
      int a, b, c, d, e;
      FILE *testdata9;
      testdata9 = fopen("testdata9.c", "r");
    
      for(fscanf(testdata9, "&#37;d %d %d %d %d", &a, &b, &c, &d, &e)){
        
      }
      return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,299
    I'm assuming testdata.c looks like this:

    523
    17
    4343532
    ...etc


    You actually want a while loop and a numeric array (the "for" in this just provides some output):
    Code:
    #include <stdio.h>
    
    int main () {
            int a[5], i=0, x;
            FILE *fstin=fopen("testdata.c", "r");
            if (fstin == NULL) {puts("Couldn't fopen..."); return -1;}
            
            while ((fscanf(fstin, "%d", &a[i])) != EOF) i++;
            close(fstin);
            for (i=0; i<5; i++) printf("%d\n",a[i]);
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    An array would've been easier, but the problem wants a for loop without the use of an array.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,829
    It shouldn't look much different if you modify MK's example to use a for loop. (Except MK's example should also close the file with fclose().)
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Though I still don't know how to do it without an array.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,829
    Read one number, do something with it, repeat.
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 03:52 PM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 06:24 AM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

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