Thread: Help with reading/writing integers in a specific way into/from a file!!

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    Help with reading/writing integers in a specific way into/from a file!!

    Hi!
    I am trying to learn how to read and write integers into a file in severreral rows.
    Then I want to read the Integers from each row and put the integers in separate arrays.

    Example of how the file looks like after adding integers:
    =======================================
    1 2 3 4 5...
    6 5 4 3 2...
    7 78 5 45...


    How do I write to put maybe 100 integers in the first row, then the second row and so on???
    When I have managed hat I want to read the integers from the first row and put them in a array like this:
    [1][2][3][4][5] without the space betwwen each integer!
    Here I also don't now how I start reading the integers from the second row to put the integers in another array.

    So far I have tested to put 2 integers in the first row. Is it necessary to fprintf a space after each integer or is there another way??

    int main(int argc, char *argv[])
    {
    FILE *myFile = fopen("txtFile.txt", "w");

    fprintf(myFile, "13");
    fprintf(myFile, " ");
    fprintf(myFile, "4\n");
    fclose(myFile);

    system("PAUSE");
    return 0;
    }

    Appreciate any help!

    PS, how do I align the code so it gets nicer?? THe link from the faq is dead.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    [code]/*code goes here */[/code]
    Code:
    #include <stdio.h>
    
    #define SIZE 5
    
    int main(void)
    {
       int i, j, array[3][SIZE];
       /*
        * Write numbers to a file.
        */
       FILE *file = fopen("file.txt", "w");
       if ( file )
       {
          /* write as many number as the entire array will hold */
          for ( i = 0; i < sizeof(array)/sizeof(**array); ++i )
          {
             /* Write numbers to file, 'SIZE' per line. */
             j = i + 1;
             fprintf(file, "%d%c", j, (i % SIZE) == (SIZE - 1) ? '\n' : ' ');
          }
          fclose(file);
       }
       /*
        * Read numbers from file.
        */
       file = fopen("file.txt", "r");
       if ( file )
       {
          /* 'i' is the "row" */
          for ( i = 0; i < sizeof(array)/sizeof(*array); ++i )
          {
             /* 'j' is the "column" */
             for ( j = 0; j < sizeof(*array)/sizeof(**array); ++j )
             {
                /* read the next whitespace-separated number */
                if ( fscanf(file, "%d", &array[i][j]) != 1 )
                {
                   goto end; /* failed or reached end of file */
                }
                printf("array[%d][%d] = %d\n", i, j, array[i][j]);
             }
          }
          end:
          fclose(file);
       }
       return 0;
    }
    
    /* file.txt
    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    */
    
    /* my output
    array[0][0] = 1
    array[0][1] = 2
    array[0][2] = 3
    array[0][3] = 4
    array[0][4] = 5
    array[1][0] = 6
    array[1][1] = 7
    array[1][2] = 8
    array[1][3] = 9
    array[1][4] = 10
    array[2][0] = 11
    array[2][1] = 12
    array[2][2] = 13
    array[2][3] = 14
    array[2][4] = 15
    */
    [edit]
    Added comments.
    [/edit]
    Last edited by Dave_Sinkula; 08-04-2003 at 10:23 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    Thanks for the example. It looks a bit difficult in some parts.
    Let me explain a bit easier.
    Maybe I want to write integers from 1 to 10 in the first row with a loop.
    THen in another loop I want to add 10 random numbers with
    rand() in the second row.

    Then I want to only read the integers from the first row and put them in an array.
    Or I only want to read the Integers from the second row and put them in an array.

    How to I start adding new integers in the second row, third row and so on or reading from the second row until I reach \n in each row???

    fprintf(file, "%d%c", j, (i % SIZE) == (SIZE - 1) ? '\n' : ' ');
    This is a bit hard to understand

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    /**/
    Last edited by valhall; 08-04-2003 at 10:13 AM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    fprintf(file, "%d%c", j, (i % SIZE) == (SIZE - 1) ? '\n' : ' ');
    Since SIZE is 5 in this example, the last parameter reduces to
    Code:
    (i % 5) == 4 ? '\n' : ' '
    For i == 0, i % 5 == 0, so a space goes with the %c.
    For i == 1, i % 5 == 1, so a space goes with the %c.
    ...
    For i == 4, i % 5 == 4, so a newline goes with the %c.
    For i == 5, i % 5 == 0, so a space goes with the %c.
    ...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Maybe I want to write integers from 1 to 10 in the first row with a loop.
    THen in another loop I want to add 10 random numbers with
    rand() in the second row.
    Maybe I'll just write some numbers in a file to make the example simple.
    Then I want to only read the integers from the first row and put them in an array.
    Or I only want to read the Integers from the second row and put them in an array.
    Here's one way.
    Code:
    #include <stdio.h>
    
    int gimme(const char *filename, int row, int *array, size_t size)
    {
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          size_t i;
          while ( row-- )
          {
             for ( i = 0; i < size; ++i )
             {
                fscanf(file, "%d", &array[i]);
             }
          }
          for ( i = 0; i < size; ++i )
          {
             printf("%d,", array[i]);
          }
          putchar('\n');
          fclose(file);
       }
    }
    
    int main(void)
    {
       int array[5];
       gimme("file.txt", 1, array, sizeof(array)/sizeof(*array));
       gimme("file.txt", 3, array, sizeof(array)/sizeof(*array));
       gimme("file.txt", 2, array, sizeof(array)/sizeof(*array));
       return 0;
    }
    
    /* file.txt
    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    */
    
    /* my output
    1,2,3,4,5,
    11,12,13,14,15,
    6,7,8,9,10,
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Jeez!! Here's the code that newbie's should understand
    Code:
    #define MAXVALPERLINE 10
    ...
    for (i=0; i < MAXVALPERLINE; i++)
    {
        fprintf(file, "%d ", i);
    }
    fprintf(file, "\n");
    
    for (i=0; i < MAXVALPERLINE; i++)
    {
        fprintf(file, "%d ", (int)(rand()*x));
    }
    fprintf(file, "\n");
    fprintf(file, "%d%c", j, (i % SIZE) == (SIZE - 1) ? '\n' : ' ');
    I've never used anything that convoluted in 20 years of C programming. Sorry Dave, it's cool and all but....
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Scanning integers from a file:
    By xarmenx in forum C Programming
    Replies: 6
    Last Post: 11-29-2004, 04:57 PM