I want to read from the following file, in C:
example file.txt:
ab cd efg mnop
1234 123 12 21
I want to read and store the space separated words in individual variables. I know I can use:
fscanf(fp, "%s %s %s.....", var1, var2, varN);
But, I don't need this fscanf(fp, "%s %s %s.....", var1,var2,varN);
for my code (for university).
A snippet of code(for university) where I need to use fscanf
s consecutively is:
......
while(!feof(fp)) {
fscanf(fp,"%2s",posRobot);
if(!strcmp(posRobot, "R1") == 0){
fscanf(fp, "%4s", pos_temp);
posR1_temp=0;
.......
But it doesn't work as intended. The code where i am asking for help:
int main()
{
FILE *fp;
char var1[2];
char var2[2];
char var2[3];
char var2[4];
....
fp = fopen("file.txt", "r");
if(fp == NULL) {
printf("Error opening file!");
exit(0);
}
//now using fscanf, trying to read the first two characters.
fscanf(fp,"%s",var1);
//test to see if i read it successfully
printf("\n1st 'fscanf' : %2s",var1);
//now using fscanf again, to read the next string.
fscanf(fp,"%s",var2);
//test to see if i read it successfully
printf("\n1st 'fscanf' : %2s",var2);
Error: it compiles successfully, but it doesn't display anything on the output window.