Thread: ReadData txt file

  1. #16
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Don't use feof in the control of a loop - i.e. That while loop.

    Here is another FAQ explaining why...
    Question 12.2

  2. #17
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Hi I appreciate your guys your effort to help me.
    my problem is this:
    I want to read the data from txt file line by line which that data is like this pattern:
    when the length of the data line of the txt file is three numbers as passing those three numbers(integers) to fun1 .
    when the length of the data line of the txt file is two numbers as passing those two numbers (integers) to func2.
    when the length of the data of the line of txt file is one number as passing one number to fun3.

    for example if i have txt file like this:

    1 2 3
    3 4 5
    3 4
    2
    4 5 6
    then I want like this to do:
    first line goes with its parameters to func1(int x=1,int y=2,int z=3)
    second line goes with its parameters to func1(3,4,5)
    third line goes with its parameters to func2(3,4)
    fourth line goes with its parameters to func3(2)
    fifth line goes with its parameters to func1(4,5,6);

    any clue how can I start? I tried the code that already posted here but didn't work well.
    there's no specified pattern for the number of integers in line, it might be one integer in line or two integer in line or three integers in line. (max three integer in line)

    I tried also to pick up line by line as string and split it up but didn't work!


    thanks alot

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How's this?
    Code:
    char buff[BUFSIZ];
    while ( fgets(buff,BUFSIZ,filep) ) {
      int v1,v2,v3;
      if ( sscanf(buff,"%d %d %d", &v1, &v2, &v3) == 3 ) {
        func1(v1,v2,v3);
      }
      else if ( sscanf(buff,"%d %d", &v1, &v2) == 2 ) {
        func2(v1,v2);
      }
      else if ( sscanf(buff,"%d", &v1) == 1 ) {
        func3(v1);
      } else {
        // it's borked
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Salem View Post
    How's this?
    Code:
    char buff[BUFSIZ];
    while ( fgets(buff,BUFSIZ,filep) ) {
      int v1,v2,v3;
      if ( sscanf(buff,"%d %d %d", &v1, &v2, &v3) == 3 ) {
        func1(v1,v2,v3);
      }
      else if ( sscanf(buff,"%d %d", &v1, &v2) == 2 ) {
        func2(v1,v2);
      }
      else if ( sscanf(buff,"%d", &v1) == 1 ) {
        func3(v1);
      } else {
        // it's borked
      }
    }
    A minor optimization:
    Code:
    char buff[BUFSIZ];
    
    while ( fgets(buff,BUFSIZ,filep) ) {
      int v1,v2,v3,count;
    
      count = sscanf("%d %d %d", &v1, &v2, &v3 );
      switch ( count )
      {
        case 1: func1(v1); break;
        case 2: func2(v1,v2); break;
        case 3: func3(v1,v2,v3); break;
        default: // error handlning...
          ...
      }
    }
    Notice sscanf() will return the number of consecutive conversions made.

    OOOOppps! @laserlight caught an error here... fgets() will return NULL on error or EOF.
    Sorry...
    Last edited by flp1969; 05-05-2019 at 06:00 AM.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by flp1969
    Notice the test against EOF
    Isn't that wrong, though? fgets returns a pointer to char, not an int, hence Salem's original implicit test for a non-null pointer is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by laserlight View Post
    Isn't that wrong, though? fgets returns a pointer to char, not an int, hence Salem's original implicit test for a non-null pointer is correct.
    You are absolutely RIGHT! Sorry...
    The snippet was edited....

  7. #22
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Isn't that wrong, though? fgets returns a pointer to char, not an int, hence Salem's original implicit test for a non-null pointer is correct.
    Hi laser, I appreciate your help, and thanks for clarifications. also thanks for flp to make an effort.

    I'm still stuck in and I tried many methods like getting line by line as string and splitting it to get every "number" and afterwards converting it by atoi function .. sounds horrible but didn't help me because I always arrive to null pointer error and have many errors in that method.
    I tried the method that I declared here before but didn't help.

    May you help me please how can I solve the problem? thanks alot.

  8. #23
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Stop saying "you're stuck" and post the latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #24
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Hi salem; this is my final code but it's not printing at all the parameters!!

    for example I just used printf function for func1, func2,func3 ..

    Code:
    int main()
    {
        int v1, v2, v3;
        char line[100000000];
        FILE *file = fopen("FILE.txt", "r");
        if (file != NULL)
        {
            /* or other suitable maximum line size */
            while (fgets(line, sizeof line, file) != NULL)
            {
                if (sscanf(line, "%d %d %d", &v1, &v2, &v3) == 3)
                {
                    printf("%d %d %d\n", v1, v2, v3);
                } else if (sscanf(line, "%d %d", &v1, &v2) == 2) {
                    printf("%d %d %d\n", v1, v2);
                } else if (sscanf(line, "%d", &v1) == 1) {
                    printf("%d\n", v1);
                }
            }
        }
        else
            {
            printf("error file openning");
        }
        return 0;
    }
    FILE TXT is the same as:
    60
    4 1 4
    607
    4 5 6
    3 1
    4 2
    Last edited by RyanC; 05-05-2019 at 09:56 AM.

  10. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char line[100000000];
    What are you smoking!?

    Pick a reasonable size, like BUFSIZ

    100MB FFS
    Sheesh!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #26
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Salem View Post
    > char line[100000000];
    What are you smoking!?

    Pick a reasonable size, like BUFSIZ

    100MB FFS
    Sheesh!
    it's not working either!
    I changed to 100MB it, it runs and there's not output, it's just running and at the end tell me in command line : Process finished with exit code -1073741571 (0xC00000FD)

  12. #27
    Banned
    Join Date
    Apr 2015
    Posts
    596
    [code]
    Code:
    int main()
    {
        int v1, v2, v3;
        char line[104857600];
        FILE *file = fopen("FILE.txt", "r");
        if (file != NULL)
        {
            /* or other suitable maximum line size */
            while (fgets(line, sizeof line, file) != NULL)
            {
                if (sscanf(line, "%d %d %d", &v1, &v2, &v3) == 3)
                {
                    printf("%d %d %d\n", v1, v2, v3);
                } else if (sscanf(line, "%d %d", &v1, &v2) == 2) {
                    printf("%d %d %d\n", v1, v2);
                } else if (sscanf(line, "%d", &v1) == 1) {
                    printf("%d\n", v1);
                }
            }
        }
        else
            {
            printf("error file openning");
        }
        return 0;
    }

  13. #28
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Stop being an idiot, and make a decent buffer size.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #29
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Salem View Post
    Stop being an idiot, and make a decent buffer size.
    Hi salem, I've done this code
    Code:
    int main()
    {
        int v1, v2, v3;
        char line[10000];
        FILE *file = fopen("FILE.txt", "r");
        if (file != NULL)
        {
            /* or other suitable maximum line size */
            while (fgets(line, sizeof line, file) != NULL)
            {
                if (sscanf(line, "%d %d %d", &v1, &v2, &v3) == 3)
                {
                    printf("%d %d %d\n", v1, v2, v3);
                } else if (sscanf(line, "%d %d", &v1, &v2) == 2) {
                    printf("%d %d %d\n", v1, v2);
                } else if (sscanf(line, "%d", &v1) == 1) {
                    printf("%d\n", v1);
                }
            }
        }
        else
            {
            printf("error file openning");
        }
        return 0;
    }
    


    it didn't output anything , I'm not kidding !

  15. #30
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Nothing at all?
    Not even your error message?

    Works for me.
    Code:
    $ cat foo.c
    #include <stdio.h>
    int main()
    {
        int v1, v2, v3;
        char line[BUFSIZ];
        FILE *file = fopen("foo.txt", "r");
        if (file != NULL)
        {
            /* or other suitable maximum line size */
            while (fgets(line, sizeof line, file) != NULL)
            {
                if (sscanf(line, "%d %d %d", &v1, &v2, &v3) == 3)
                {
                    printf("V3 = %d %d %d\n", v1, v2, v3);
                } else if (sscanf(line, "%d %d", &v1, &v2) == 2) {
                    printf("V2 = %d %d\n", v1, v2);      //!! WAS foo.c:16:24: warning: format ?%d? expects a matching ?int? argument [-Wformat=]
                } else if (sscanf(line, "%d", &v1) == 1) {
                    printf("V1 = %d\n", v1);
                }
            }
        }
        else
            {
            printf("error file openning");
        }
        return 0;
    }
    $ cat foo.txt
    60
    4 1 4
    607
    4 5 6
    3 1
    4 2
    $ gcc foo.c
    $ ./a.out 
    V1 = 60
    V3 = 4 1 4
    V1 = 607
    V3 = 4 5 6
    V2 = 3 1
    V2 = 4 2
    Yes, your 2nd printf was broken.

    Are compiler messages preventing you from making a valid executable?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-21-2017, 10:48 AM
  2. Replies: 6
    Last Post: 12-28-2015, 03:36 AM
  3. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  4. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM

Tags for this Thread