I am trying to read the following text file into an array containing the text data and an array containing the decimal data:
First 0.5
Second 0.39844
Third 0.39844
Fourth 0.5
Second_TCC_Locked 0.39844
Third_TCC_Locked 0.39844
Fourth_TCC_Locked 0.5
SS_Reverse 0.625
My intent is to do a strcmp() later in the code in order to compare user input to this file.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fpin0, *fpout;
int c, i;
int count=0;
float table_gain1[8];
char table_gain0[8];
fpin0 = fopen("TextArray.txt","r");
/* open file 'TextArray.txt' for reading */
while((c=fgetc(fpin0))!=EOF)
{
if(c=='\n')
count++;
}
/* do something with count */
printf ( "There are %d lines in the file\n",count );
/* Set pointer to beginning of file: */
fseek( fpin0, 0L, SEEK_SET );
for(i = 0; i < count; i++)
{
fscanf(fpin0,"%s %d",&table_gain0[i], &table_gain1[i]);
}
fclose(fpin0);
fpout = fopen("TestRead.out","w");
fprintf(fpout, "Mode Gain\n");
for(i = 0; i < count; i++)
{
fprintf(fpout,"%s\n",&table_gain0[i]);
}
for(i = 0; i < count; i++)
{
fprintf(fpout,"%d\n",&table_gain1[i]);
}
fclose(fpout);
}
From the looks of the output, I am guess it is a format issue but I have been trying different formats without any luck. Any help would be greatly appreciated
Output:
Mode Gain
F.S.T.F.5
.S.T.F.5
S.T.F.5
.T.F.5
T.F.5
.F.5
F.5
.5
1245004
1245008
1245012
1245016
1245020
1245024
1245028
1245032