Hi Guys, I'm trying to parse a log that looks like this:
Code:
data,b1a571,3324 d,123.46,987.65
[2022-09-01 16:08] ,RSSI: -25
[2022-09-01 16:08] ,Sent a reply
[2022-09-01 16:08] ,mdata,b1a571,3324 d,123.46,987.65
[2022-09-01 16:08] ,RSSI: -25
[2022-09-01 16:08] ,Sent a reply
[2022-09-01 16:08] ,mdata,b1a571,3324 d,123.46,987.65
[2022-09-01 16:08] ,RSSI: -25
[2022-09-01 16:08] ,Sent a
Notice the first and last lines are often partial entries. If the first line doesn't begin with a '[', I intend to append it to the last line of the prior log.
If the last entry ends without a '\n', I'm planing to save it to a temporary string and append the first line of the next log. Am I making sense? I'm a novice C coder but, really enjoying it.
Here's my code that fails to figure out if the first line begins with a '['.
Code:
#include <stdio.h>
#include <string.h>
// How to read a text file line by line in C Programming - YouTube
char fpath4[100] = "C:/work/arduino/teraterm-4.106/logs/";
char fname4[] = "logToParse";
void main(){
strcat(fpath4, fname4); // put the log name at the end of the path
FILE *the_file = fopen(fpath4, "r"); // open the file Read Only
if(the_file == NULL){ // if the file doesn't exist, display an error
perror("\nUnable to open the file");
exit(1); // exit the program
}
char line[100];
//while(fgets(line, sizeof(line), the_file)){ // while there is a line : fgets gets a single line
fgets(line, sizeof(line), the_file);
printf("\n%s", line); // prints the entire line of the log
printf("\n%s", line[0]); // DOES NOT WORK
//}
fclose(the_file);
//fclose(???);
return 0;
}
I'm try to read only the first character of 'line' to see if it is a '['. How come
Code:
printf("\n%s", line[0]);
does not work? Do I need to convert 'line' to an array first or something? Thank you.