is there an efficient way to read a numbers-and-text file? what i am currently doing is to use fgets() to read in a file line by line, and for each line i use strstr() to find if my desired quantity is present. if it is, i convert every character to its ascii integer, and if the character is a number or full-stop, i output it character by character to a dummy file, which i later open and read in line by line and convert each line of string to float using atof(). i find this very inefficient, and if say a line has got 10 different numbers and i want only the 4th number (not digit, but number), this gets very tedious. my code for the above would look something like this:
Code:
while (fgets(str, LENGTH, infile_hd) != NULL) {
if(strstr(str, str_samples) != NULL) {
len = strlen(str);
for (j=0; j<len; j++){
ch = (int)str[j];
if (ch > 47 && ch < 58) {
fprintf(outfile_hd_dummy, "%c", str[j]);
}
}
fprintf(outfile_hd_dummy, "\n");
}
else if(strstr(str, str_lines) != NULL){
len = strlen(str);
for (j=0; j<len; j++){
ch = (int)str[j];
if (ch > 47 && ch < 58) {
fprintf(outfile_hd_dummy, "%c", str[j]);
}
}
fprintf(outfile_hd_dummy, "\n");
}
}
fclose(infile_hd);
fclose(outfile_hd_dummy);
FILE *infile_hd_dummy = fopen("/media/Data/prog/out/hd_dummy", "rb");
float a[2];
j=0;
while (fgets(str, LENGTH, infile_hd_dummy) != NULL) {
a[j] = atof(str);
j++;
}
int samples = (int)a[0];
int lines = (int)a[1];
is there a more efficient way to go about doing this?