/*
@Author: Mr.Suppakit Thongdee
@Website: www.sourcecode.in.th
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main(){
clrscr();
FILE *file;
const num = 24;
char buf[500];
char temp[3];
int data[num];
int count;
int count_num;
//Open text file
file = fopen("data.txt","r");
if(!file) return 1;
count_num = 0;
if(fgets(buf,500,file)!=NULL){ //read data in text file
printf("Data from data.txt = %s\n",buf);
count = 0;
strcpy(temp," ");
for(int i=0; i<strlen(buf); i++){
if(buf[i] != char(32)){ //check space
temp[count] = buf[i];
count++;
}else{ //not space
//save value to array
data[count_num] = atoi(temp);
//clear temp value
strcpy(temp," ");
count = 0;
count_num++;
}
}
//save last value to array
data[count_num] = atoi(temp);
}else{
printf("Error: Can not open data.txt file.");
return 1;
}
fclose(file);
printf("Result in array--------------------------\n");
//Display
for(int i=0; i<=num;i++){
printf("data[%d]=%d\n",i,data[i]);
}
getch();
return 1;
}