If I`ve got a TXT file with a pre-determined structure, is there any way I can read some of the text directly from the file without having to parse or tokenize a string? For instance, if I have the following code:
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <fstream>
int main(int argc, char** argv) {
FILE* test = fopen("test.txt","w+");
char* read;
fprintf(test,"(foo,_TEMP71_,_,_)\n");
fprintf(test,"(foo,_TEMP52_,_,_)\n");
fprintf(test,"(foo,_TEMP43_,_,_)\n");
fprintf(test,"(foo,_TEMP24_,_,_)\n");
fprintf(test,"(foo,_TEMP15_,_,_)\n");
fprintf(test,"(foo,_TEMP06_,_,_)\n");
fseek(test, 0, SEEK_SET); //returns the file pointer to start of file
while(feof(test) == 0)
{
fscanf(test, "_%s_", read); //or fscanf(test, "_TEMP%s_", read);
printf("%s\n", read);
}
fclose(test);
}
Note that on the fscanf
line, I read from this website that you could specify a certain string of characters to be read, but I don`t think I understood quite how it works.