Hello ,
I'm a newbie in C and have been programming this for a while now .
Task: Read a csv file and look for floating point numbers . If you see a floating point number, then change the floating point number to six significant figures after the decimal point.
I have the code where the program reads the file, and seperates the strings from commas, I use the strtok function to tokenize between commas. Because my file is not in a proper array format or anything, its just goes on and its huge there are just some random commas in between.
My problem is , I'm not able to find a proper solution to check if the seperated token(i.e., strings) is a floating point number or not.
The file has words, binary numbers, integers, signed/unsigned floating point numbers.
Below is the code that I've got through so far and below that is the example of how the file looks like.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <conio.h>
char *token;
// Program that copies file frst_100l to a new file new_data.
int main()
{
char ch, data_in[1024], data_out[1024]; // initialize character for files
char line[102400];
char temp[30];
char size;
int i = 0;
FILE *fp, *tp; // file pointers
printf("Enter name of file to copy\n"); // enter the name of the file for data
gets(data_in); // get data to character pointer
fp = fopen(data_in, "r"); // open file to read
// if file has error then exit the debug
if( fp == NULL )
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while(fgets(line,102400,fp) != NULL)
{
token=strtok(line,",");
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, "," );
}
if((token == 'a') && (*token <= 'z'))
{
printf("%c",token);
}
}
free(token);
//Enter the name of the file for the data to copy to
//printf("Enter name of file data should be changed to \n");
gets(data_out); // get data_out pointer
tp = fopen(data_out, "w"); // open the file to write
// close if error in creating or writing the file
if( tp == NULL )
{
fclose(fp);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
// while character is for data_out file is not equal to end of file
// copy the data to new file
while( ( ch = fgetc(fp) ) != EOF )
{
fputc(ch, tp);
}
// print file copied successfully and close the debug window
printf("File copied successfully.\n");
fclose(fp);
fclose(tp);
return(0);
}
Example of file :
I_TOTAL,WL_WTX_N_5280_LDO_CLK_I,WL_WTX_N_5280_LDO_ DCO_WL_I,WL_WTX_N_5280_LDO_WL_I,WL_WTX_N_5280_VBAT _FM_I,WL_WTX_N_5280_VBAT_IN_PM_I,WL_WTX_N_5280_VBA T_PA_I,WL_WTX_N_5280_VDDS2_I,WL_WTX_N_5280_VDDS3_I ,WL_WTX_N_5280_VDDS4_I,WL_WTX_N_5280_VDDS_I,WL_WTX _N_5540_I_MCS7_PWR,WL_WTX_N_5540_I_TOTAL,WL_WTX_N_ 5540_LDO_CLK_I,WL_WTX_N_5540_LDO_DCO_WL_I,WL_WTX_N _5540_LDO_WL_I,WL_WTX_N_5540_VBAT_FM_I,WL_WTX_N_55 40_VBAT_IN_PM_I,WL_WTX_N_5540_VBAT_PA_I,WL_WTX_N_5 540_VDDS2_I,WL_WTX_N_5540_VDDS3_I,WL_WTX_N_5540_VD DS4_I,WL_WTX_N_5540_VDDS_I,WL_WTX_N_5640_I_MCS7_PW R,WL_WTX_N_5640_I_TOTAL,WL_WTX_N_5640_LDO_CLK_I,WL _WTX_N_5640_LDO_DCO_WL_I,WL_WTX_N_5640_LDO_WL_I,WL _WTX_N_5640_VBAT_FM_I,WL_WTX_N_5640_VBAT_IN_PM_I,W L_WTX_N_5640_VBAT_PA_I,WL_WTX_N_5640_VDDS2_I,WL_WT X_N_5640_VDDS3_I,WL_WTX_N_5640_VDDS4_I,WL_WTX_N_56 40_VDDS_I,WL_WTX_N_5700_I_MCS7_PWR,WL_WTX_N_5700_I _TOTAL,WL_WTX_N_5700_LDO_CLK_I,WL_WTX_N_5700_LDO_D CO_WL_I,WL_WTX_N_5700_LDO_WL_I,WL_WTX_N_5700_VBAT_ FM_I,WL_WTX_N_5700_VBAT_IN_PM_I,WL_WTX_N_5700_VBAT _PA_I,WL_WTX_N_5700_VDDS2_I,WL_WTX_N_5700_VDDS3_I, WL_WTX_N_5700_VDDS4_I,WL_WTX_N_5700_VDDS_I,WL_WTX_ N_5805_I_MCS7_PWR,WL_WTX_N_5805_I_TOTAL,WL_WTX_N_5 805_LDO_CLK_I,WL_WTX_N_5805_LDO_DCO_WL_I,WL_WTX_N_ 5805_LDO_WL_I,WL_WTX_N_5805_VBAT_FM_I,WL_WTX_N_580 5_VBAT_IN_PM_I,WL_WTX_N_5805_VBAT_PA_I,WL_WTX_N_58 05_VDDS2_I,WL_WTX_N_5805_VDDS3_I,WL_WTX_N_5805_VDD S4_I,WL_WTX_N_5805_VDDS_I,X,XY,Y
1,,,0,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,,,,, ,,,,,,,,,,,,,,,,23,3,0,3,1.2470109462738,1.2248870 1343536,,,,0,,0,1,23659,12976,32090,1.247010946273 8,112.743698120117,23,8869.6796875,,,,,,,,,,,,,,,, ,,,,,,,,98.8096694946289,,98.8096694946289,,.82850 6410121918,.986691415309906,,,,,,,,.79969382286071 8,,,,,,,,2.03000092506409,,2.0292010307312,,1.0346 8096256256,1.22431802749634,,,,.380130290985107,20 ,3,,,,20,0,,,,,,,,,,,,,,,,28.2810001373291,,27.870 7695007324,,.410234898328781,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.7565901279449 46,.736221790313721,.579420387744904,.560720205307 007,,,,,,,,,,,,,,,,,,,,,,2.07184100151062,,2.07184 100151062
Thank you and please let me know
Ruthy