I'm currently trying to read in a file delimited by spaces. I have been able to get it to read line by line, but now I need to separate it by spaces so that I can put it into an array. How does one separate it into the array?
#include <stdio.h>
int main ( void )
{
int data_array[3];
int num1;
int num2;
int num3;
static const char data[] = "data.txt";
FILE *file = fopen ( data, "r" );
if ( file == NULL )
{
printf("An error occured reading the file. Check to make sure file is not locked.");
}
else
{
char line [ 1024 ]; // hopefully each line does not exceed 1024 chars
while ( fgets ( line, sizeof line, file ) != NULL ) // reads each line
{
// reads each number into an array
scanf("%d %d %d", num1, num2, num3);
data_array[0] = num1;
data_array[1] = num2;
data_array[2] = num3;
}
fclose ( file ); // closes file
}
return 0;
}