0

test.c scans file.txt and prints name for a given id.
I would like to add a 3rd column to file.txt

I also would like to ask:

what does %99s and == 2 mean in: while (fscanf(fff, "%d %99s", &id, name) == 2) {

file.txt ( new column added )

1 name1 newcolumn1
2 name2 newcolumn2
3 name3 newcolumn3

test.c modified to work with 3rd column ( added char name2[100]; and cloned %99s )

Result: Not Working. ( compiles: ok. but outputs empty (nothing) )

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main() {
    char name[100];
    char name2[100];

    FILE *fff;

    int found = 0;
    int id;

    fff = fopen("/file.txt", "r");

    while (fscanf(fff, "%d %99s %99s", &id, name, name2) == 2) {
        if (id == 2) {
            printf("%s\n", name2);
            found = 1;
            break;
        }
    }

    fclose(fff);
    return 0;
}
  • %99 means read 99 chars into the char array while == in this case will compare the number of successful conversions from fscanf...check out what fscanf returns...https://stackoverflow.com/questions/10469643/value-returned-by-scanf-function-in-c – Pushan Gupta Jun 21 '17 at 07:59
  • *"Result: Not Working."* That is not valid problem description. Please see [ask] and read on [mcve]. Also, please indent your code. – user694733 Jun 21 '17 at 07:59
  • 1
    the `scanf()` family of function returns the number of *successful conversions*. So if you add another one, you would have to check against the new number, in your case `3`. -- in general, [a manual can help](https://linux.die.net/man/3/scanf). –  Jun 21 '17 at 07:59
  • Your text file already has 3 columns? Or you want to add it pro-grammatically? – Pushan Gupta Jun 21 '17 at 08:00
  • @UnreasonableDirection what you say is you want to add a third column, but you are already reading a third column. So it is really unclear if you want to add or what? – Pushan Gupta Jun 21 '17 at 08:04
  • 1
    @UnreasonableDirection just read my comment. Next time, consult the manual before asking. Questions with just a simple minor problem caused by *not* reading the manual aren't all that helpful. –  Jun 21 '17 at 08:05
  • @UnreasonableDirection: I compiled it with **== 3**, it works! Look at my answer, do you mean `file.txt` and not `/file.txt`? – Andre Kampling Jun 21 '17 at 08:10
  • @UnreasonableDirection: I compiled it, it works... It prints: `newcolumn2` because of your if statement... – Andre Kampling Jun 21 '17 at 08:11
  • Are you sure your input file is at the directory root (`/file.txt` vs `file.txt`). Always test the return value of many functions with prototype in ``: `if (fff == NULL) { perror("file.txt"); exit(EXIT_FAILURE); }` – pmg Jun 21 '17 at 08:14
  • how about "man scanf"? Google will find you the document, of course you have to read and understand it. –  Jun 21 '17 at 08:19

2 Answers2

0

It must be: while (fscanf(fff, "%d %99s %99s ", &id, name, name2) == 3) { ... }, note the == 3. From the manpage of fscanf():

These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

Also note that your file is at the Linux root folder: /file.txt. Do you mean just file.txt for the current working directory?

The %99s means, that fscanf will read 99 characters and add one terminating NUL character ('\0'), so that your buffer will not overflow because it's size is 100. Further if you want to provide always the right buffersize via %s to fscanf you can follow this explanation.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
0

The %99s means that the program must only accept upto 99 characters in name and name2.

Now fscanf returns an int value of number of inputs accepted and placed into variables. So fscanf(fff, "%d %99s %99s", &id, name) == 2 is always wrong, as 3 inputs are accepted.

The fscanf condtion is just a fail safe to be careful (common c idiom).

The correct program now would be:

#include <stdio.h>

int main(void)
{
    char name[100];
    char name2[100];
    FILE *fff;
    int found = 0;
    int id;

    fff = fopen("/file.txt", "r");

    if (fff == NULL)
        return -1;

    while ( fscanf(fff, "%d %99s %99s ", &id, name, name2) == 3 )
    {
        if (id == 2)
        {
            printf("%s\n", name2);
            found = 1;
            break;
        }
    }

    fclose(fff);

    return 0;
}
Gam
  • 684
  • 1
  • 8
  • 17
Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27