Thread: ReadData txt file

  1. #31
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Salem View Post
    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?
    No; Im using compiler Clion (c99) does that matter?

    It still not working; I guess my compiler not configuring "sscanf" function; what should I do?
    What compiler are you using?

  2. #32
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    This?
    CLion: A Cross-Platform IDE for C and C++ by JetBrains

    "A cross-platform IDE for C and C++ "
    That just means it's a tarted up text editor.

    What is your compiler?

    > I guess my compiler not configuring "sscanf" function; what should I do?
    You're really in no position to make such guesses.

    Working with Tool Windows - Help | CLion
    I suggest you find whichever tool window contains the RAW output from the compiler.

    > What compiler are you using?
    Read my post, it says gcc.


    Did you run the program from a console?
    Or did you just select "Run..." in the IDE?
    Because if you ran it within an IDE, the console window could have opened and closed before you'd even noticed it.
    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