File help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • electromania
    New Member
    • Mar 2008
    • 20

    File help

    I'm writing this function which can read a file in the format of
    1999 24324938
    2000 59469955
    2001 68478498
    2002 73547547
    2003 86796079

    store each column in a separate array so I can use them later for some mathematical formula.
    so far I've written this much, and dont know where to take it from here

    int ReadFile(void)
    {

    FILE *fp; /* create a file pointer that will reference your text file*/
    char buf[80]; /* what I read into*/
    int n1, n2; /* reading two numbers */
    char filename;

    fp = fopen("input.tx t", "r"); /* open up the file you're looking for*/

    if (fp == NULL) /* problem opening the file*/
    return(1); /*quit*/

    while (fgets(buf, 80, fp) != NULL && !feof(fp)) /* keep reading till i reach the end*/

    {
    sscanf(buf, "%d %d", &n1, &n2); /* buf is one line of the input file which contained two columns, so read in each var*/
    printf("n1 = %d n2 = %d\n", n1, n2); /* print the results to stdout*/
    }

    fclose(fp);
    }


    Any help would be appreciated.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    So, do you see the expected results when you printf() n1 and n2?

    If so, then you just need to store these values in an array.

    Comment

    • electromania
      New Member
      • Mar 2008
      • 20

      #3
      Originally posted by weaknessforcats
      So, do you see the expected results when you printf() n1 and n2?

      If so, then you just need to store these values in an array.

      Yes i do see the results in n1 and n2, but i'm not sure how to store them in an array. Can you show me how? maybe an examples?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Using arrays is covered in any C or C++ textbook.

        There is also this article: http://www.thescripts.com/forum/thread772412.html.

        Comment

        Working...