Add as reading from file

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

    Add as reading from file

    Hi,

    Im reading a file, with 2 columns. this is working

    as Im reading I want to be able to count how many rows I've read and also add the all values as im reading from the second column, could some help plz
    This is the code

    #include <stdio.h>
    #include <stdlib.h>

    struct data{

    int x;
    int y;
    };
    void read_info(void) ;

    int main()
    {
    read_info();
    }

    void read_info(void)
    {
    FILE *stocks;
    struct data stock;
    int x;
    char buf[80];
    int total = 0;
    int numberofrecords = 0;

    stocks = fopen("banana.d at", "r");
    if(stocks==NULL )
    {
    puts("No data in file");
    return;
    }

    while (fgets(buf, 80, stocks) != NULL && !feof(stocks))

    {
    sscanf(buf, "%d %d", &stock.x, &stock.y);

    printf("n1 = %d n2 = %d\n", stock.x, stock.y);

    /*numberofrecord s = stock.x++;
    total += stock.y;
    printf("%d %d", numberofrecords , total);*/
    }


    fclose(stocks);

    }
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by electromania
    Hi,

    Im reading a file, with 2 columns. this is working

    as Im reading I want to be able to count how many rows I've read and also add the all values as im reading from the second column, could some help plz
    This is the code

    #include <stdio.h>
    #include <stdlib.h>

    struct data{

    int x;
    int y;
    };
    void read_info(void) ;

    int main()
    {
    read_info();
    }

    void read_info(void)
    {
    FILE *stocks;
    struct data stock;
    int x;
    char buf[80];
    int total = 0;
    int numberofrecords = 0;

    stocks = fopen("banana.d at", "r");
    if(stocks==NULL )
    {
    puts("No data in file");
    return;
    }

    while (fgets(buf, 80, stocks) != NULL && !feof(stocks))

    {
    sscanf(buf, "%d %d", &stock.x, &stock.y);

    printf("n1 = %d n2 = %d\n", stock.x, stock.y);

    /*numberofrecord s = stock.x++;
    total += stock.y;
    printf("%d %d", numberofrecords , total);*/
    }


    fclose(stocks);

    }
    You are reading the values in the struct,,,thats right.
    You should have a global variable in which u are adding up and maintaining the count.
    Add that too in the code

    raghuram

    Comment

    • electromania
      New Member
      • Mar 2008
      • 20

      #3
      Originally posted by gpraghuram
      You are reading the values in the struct,,,thats right.
      You should have a global variable in which u are adding up and maintaining the count.
      Add that too in the code

      raghuram
      I've got the variables in the function total and numberofrecords
      As you can see I also tried to increment the total after each line but didnt work. I'm not too good with C, I've tried everything I knew but could come to solve the problem.

      Comment

      Working...