So I want to read a file containing integers. I can read the first number but how can I read all of them? String is easy but this is different. I also want to read them in a way I can then later add them together. I edited my code and I was able to output them all but my other question is how can I select each number alone so I can add whatever I want. For example, if I want to select the first column only and add it together.
Data:
54 250 19
62 525 38
71 123 6
85 1322 86
97 235 14
Code:
#include <stdio.h>
#include <conio.h>
int main()
{
// pointer file
FILE *pFile;
char line[128];
// opening name of file with mode
pFile = fopen("Carpgm.txt","r");
//checking if file is real and got right path
if (pFile != NULL)
{
while (fgets(line, sizeof(line), pFile) != NULL)
{
int a, b, c;
if (sscanf(line, "%d %d %d", &a, &b, &c) == 3)
{
/* Values read, do something with them */
printf("%d %d %d\n",a, b, c);
}
}
//using fgets to read with spaces
//fgets(line,81, pFile);
//printing the array that got the pfile info
//closing file
fclose(pFile);
}
else
{
printf("Could not open file\n");
}
getch();
return 0;
}