Hi. I'm reading integers from a file and placing them into an array.
How do you detect for end-of-line? I've tried checking using '\0' and '\n', neither works.
If I can detect the end-of-line then I can remove the 0 that adds itself to the array.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int main()
{
/*Initialisations*/
FILE *fin;
char file_name_in[20]= "test.txt",c;
long int int_array[10000],temp_array[1000],d,j=0,k=0,temp_integer=0,size_of_array=0;
/* Test to see if file opens */
fin = fopen(file_name_in, "r");
if (fin == NULL){
(void)fprintf(stderr,"Cannot open input file %s\n",file_name_in);
}
int i=0;
do{
c = getc(fin);
if (((c == ' ')||(c == '\0'))&&(i!=0)){
temp_integer = 0;
for (j=0;i>0;j++){
i-=1;
temp_integer += (pow(10,i))*temp_array[j];
}
int_array[k] = temp_integer;
printf("\nint_array at position %d is %d\n",k,int_array[k]);
k++;
}
d = atoi(&c);
temp_array[i]= d;
i++;
}while (c!=EOF);
return 0;
}
sample input data:
2 300
4 800
10 10
10 10
10 10
10 100
10 10
The output:
int_array at position 0 is 2
int_array at position 1 is 30004
int_array at position 2 is 800010
int_array at position 3 is 10010
int_array at position 4 is 10010
int_array at position 5 is 10010
int_array at position 6 is 100010
Any Ideas?