0

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 fscanfs 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.

CaptainDaVinci
  • 975
  • 7
  • 23
All.in
  • 11
  • 4
  • 1
    Please read [Why is “while (!feof(file))” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Ed Heal Jun 10 '19 at 16:02
  • Also, using `*scanf()` without checking the return value... – DevSolar Jun 10 '19 at 16:06
  • 2
    Your strings are too small - you don’t allow room for a terminator. – Paul R Jun 10 '19 at 16:06
  • @paulR, thank you for your answer, how can i check the "minimum string length" when using `fscanf`? – All.in Jun 10 '19 at 16:09
  • `if (!strcmp(...)==0)` looks fishy – pmg Jun 10 '19 at 16:18
  • 1
    @All.in String in C are nothing but _null_ terminated character arrays. The size of the array should be at-least one more that the length of the string, to hold all the characters as well as the _null_ character. – CaptainDaVinci Jun 10 '19 at 16:28
  • @captainDaVinci i remember it now. Thank you very much, i will try to edit the code:) – All.in Jun 10 '19 at 16:30
  • @CaptainDaVinci I LOVE YOU! IT WORKS I FORGOT THE NULL CHARATER. LOVE YOU GUYS. Thank you very much! – All.in Jun 10 '19 at 16:33
  • Why do you want to do all of this? There's no reason to use scanf .... ever. Just read your data. Find the first char of "ab" (the 'a') in the buffer and assign a pointer to it. Find the first char of "cd" and assign a pointer to it. Copying all of your data into variables is inelegant, wasteful, and difficult. – William Pursell Jun 10 '19 at 16:36
  • @WilliamPursell This was a "test fscanf code". I didn't understood why my "main code" is not working properly and i wanted to use another piece of code to test how fscanf works. I'm at the beggining, pointers are terrifying me ahahahha – All.in Jun 10 '19 at 16:40
  • pointers are far less terrifying than scanf! :) Really, the time you spend learning the foibles of the scanf format language would be much better spent learning C. – William Pursell Jun 10 '19 at 16:52
  • @pmg i saw it right now...just a typing error, i know the correct syntax ahahhaha thank you :) – All.in Jun 10 '19 at 17:48

1 Answers1

0

Thank you very much guys, very fast answers. The problem was i didn't allocate another "space" for the "NULL CHARACTER"

the working code:

int main()
{
FILE *fp;
char xxx[3];
char bb[3];
char cc[4];
char aa[5];
char hh[4];

fp=fopen("fscanf.txt", "r");
if(fp==NULL){
    printf("Error opening file!"); 
    exit(0);    
    }


fscanf(fp,"%s",xxx);
printf("\n1st 'fscanf' : %3s",xxx);

fscanf(fp,"%s",bb);
printf("\n2nd 'fscanf' : %3s",bb);

fscanf(fp,"%s",cc);
printf("\n3rd 'fscanf' : %4s",cc);

fscanf(fp,"%s",aa);
printf("\n4th 'fscanf' : %5s",aa);

fscanf(fp,"%s",hh);
printf("\n5th 'fscanf' : %4s",cc);

return 0;

}

All.in
  • 11
  • 4
  • What if the user changes the input? The documentation for `fscanf` specifies "An optional non-zero decimal integer that specifies the maximum field width." Suggest `fscanf(fp, "%2s", xxx)`? – Neil Jun 10 '19 at 18:47