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);
}
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);
}
Comment