fopen - Inputting a file to fill an array.

This is a discussion on fopen - Inputting a file to fill an array. within the C Programming forums, part of the General Programming Boards category; source.c Code: #include <stdio.h> #define ARRAY_SIZE 20 int main() { FILE * pointer; int test1[ARRAY_SIZE], test2[ARRAY_SIZE]; int c, count; pointer ...

  1. #1
    C Newbie
    Join Date
    Oct 2011
    Posts
    59

    fopen - Inputting a file to fill an array.

    source.c
    Code:
    #include <stdio.h>
    
    #define ARRAY_SIZE 20
    
    
    int main()
    {
        FILE * pointer;
        int test1[ARRAY_SIZE], test2[ARRAY_SIZE];
        int c, count;
        
        pointer = fopen("input.txt", "r");
        
        if(pointer == NULL)
        {
            printf("Open operation failed.");
            return 1;
        }
        
        fscanf(pointer, "%d", &count);
        
        for(c = 0; c < count; c++)
        {
            fscanf(pointer, "%d%d", &test1[c], &test2[c]);
        }
        
        fclose(pointer);
    input.txt
    Code:
    7
    78    88
    79    67
    80    90
    81    56
    82    76
    83    99
    70    82
    Am I inputting the file into my two arrays correctly? Only asking because I am away from home and can only use public computers.

    Thanks in advance,
    ~Alan

    PS: Anyone know of a portable IDE compatible with MS C++? I was using CodeBlocks, but the source file would not compile correctly with MS C++, which is what my profs use to check for compile errors. Thanks again!
    Last edited by Alan Gott; 11-07-2011 at 10:13 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You need a tab ('\t', presuming that is a tab and not a fixed number of spaces*) in between those %d. I might include the newline too, altho it should work either way.

    * this:
    Code:
    fscanf(pointer, "%d%*[ \t]%d"...
    should cover either case. * means discard/ignore, [] indicates a string of characters in the set.
    Last edited by MK27; 11-07-2011 at 10:24 AM.
    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
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by MK27 View Post
    You need a tab ('\t', presuming that is a tab and not a fixed number of spaces*) in between those %d. I might include the newline too, altho it should work either way.* this:
    Code:
    fscanf(pointer, "%d%*[ \t]%d"...
    should cover either case. * means discard/ignore, [] indicates a string of characters in the set.
    Thanks! And, would it be easier to just have one space in between instead of a tab? I'm totally new to reading files into the program. ;S

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by MK27 View Post
    You need a tab ('\t', presuming that is a tab and not a fixed number of spaces*) in between those %d.
    Thats wrong.
    No tab is needed in formatstring, each %d in scanf will eat all heading whitespaces.

  5. #5
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by BillyTKid View Post
    Thats wrong.No tab is needed in formatstring, each %d in scanf will eat all heading whitespaces.
    So, none of the what he said is needed? scanf will only search for the first two integers? Now you've got me confused lol. ;P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-06-2011, 08:43 AM
  2. Inputting to an array.
    By mintsmike in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2009, 10:26 AM
  3. Replies: 2
    Last Post: 04-27-2008, 04:39 AM
  4. Fill an array with strings from file
    By niponki in forum C Programming
    Replies: 12
    Last Post: 08-01-2005, 07:50 PM
  5. How to fill a 2d array from a file?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2002, 06:01 PM

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