0

I am currently having problems reading data from txt file in C. The structure of the data in the file is something like this:

Mike is 26 years old and he lives in Canada.

I want to get the name, the age and country from the data listed using fscanf

  • This doesn't seem like an appropriate use of `fscanf()`. A regular expression would probably work better. – Barmar May 23 '22 at 17:48
  • Do all sentences have the same pattern "X in Y years old and s/he lives in Z"? In that case, you might be able to read a sentence and try to extract the strings between the common substrings, whose positions you can find with `strstr`. If your sentences are more varied (e.g. "Zoe is a 24 year-old woman from Boston.") things get more complicated, of course. – M Oehm May 23 '22 at 18:02
  • 1
    Tell Mike to use `fgets()` instead of `fscanf()` and trim the trialing newline with `strcspn()`. – David C. Rankin May 23 '22 at 20:01
  • I can't tell if this question needs a scanf tag or not. – Joshua May 24 '22 at 01:04

1 Answers1

0

If all sentences have the same pattern you can read the text line by line and split the line in words. You can do this with the below code :

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

int main(int argc, char *argv[])
{
    FILE * database;
    char buffer[100];

    database = fopen("test.txt", "r");

    if (NULL == database)
    {
        perror("opening database");
        return (-1);
    }

    while (EOF != fscanf(database, "%[^\n]\n", buffer))
    {
        printf("> %s\n", buffer);
        char * token = strtok(buffer, " ");
         
        while (token != NULL) 
        {
            //First token is the name , third token is the age etc..
            printf( " %s\n", token );//printing each word, you can assign it to a variable
            token = strtok(NULL, " ");  
        }
    }

    fclose(database);

    return (0);
}

For fscanf() i use the following post and you can also check it: Traverse FILE line by line using fscanf

When you take each word from the sentence you can assign it to a variable or handle it as you wish

panas007
  • 36
  • 8
  • Curious why, if your code execution makes it all the way to the end of `main` with no issues, you don't `return 0;`? – Chris May 23 '22 at 19:54
  • 1
    `"%[^\n]\n"` doesn't do what you think it does. There should be no `'\n'` at the end (would be equivalent to putting a space or tab character there). It does not match at `'\n'`, it matches ANY whitespace, including leading whitespace in the next line of input. Correct use would be `" 99[^\n]"` (notwithstanding that `fscanf()` is a poor choice here as `fgets()` would be the better approach) – David C. Rankin May 23 '22 at 20:03
  • @Chris yes that was a mistake i change it. @DavidC.Rankin sure `fscanf()` is a poor choice and `fgets()` is better choice.I used`fscanf()` because it said so in the question ( at the link there is a good solution with `fgets()` ). Also i tried the `" 99[^\n]"` like you said and it does not give me the expected results – panas007 May 23 '22 at 20:59
  • `while (1 == fscanf(database, "%[^\n]\n", buffer))` makes more sense than `while (EOF != fscanf(database, "%[^\n]\n", buffer))`. `"%[^\n]\n"` lacking a width can readily lead to _undefined behavior_. – chux - Reinstate Monica May 23 '22 at 21:37