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,300
    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
    7,762
    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().)

  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
    7,762
    Read one number, do something with it, repeat.

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-29-2008, 12:11 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 04:52 PM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 07:24 AM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 02:22 PM

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