Results 1 to 4 of 4

Thread: Problem with fgets function

  1. #1
    Join Date
    Mar 2009
    Posts
    176

    Problem with fgets function

    I have a file foo.txt with 2 lines. I want to retrieve each of them in variables for use in the main code.

    Code:
    void reading_file() 
    { 
    char *name = "foo.txt"; 
    char line [BUFSIZ]; 
    
    FILE *file = fopen (name, "r"); 
    if (file != NULL) 
    { 
    while (fgets (line, sizeof line, file) != NULL) 
    { 
    printf("%s", line); 
    } 
    fclose (file); 
    } 
    else 
    { 
    perror ("Impossible to open the file."); 
    } 
    }

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Problem with fgets function

    If you want to use your variables in the main code you have to create storage space for your lines with a malloc.

    Then (if you do not know the number of variables lines refer to the main code) you must do a dynamic sort a linked list that contains:

    - A pointer to the malloc that contains your credit line
    - A pointer to the next cell containing the next cell

    For code I had think there's not too far already for a moment, I tried to find it in the evening if necessary.

  3. #3
    Join Date
    Mar 2009
    Posts
    176

    Re: Problem with fgets function

    My first line is set to 4 bytes. The second is an integer between 1 and 1000. I want to code if it is not too much a constraint. I used this piece of code using the function scanf that works and meets my needs but I do not know if this is optimal.

    Code:
    void reading_file()
    {
       char *name = "foo.txt";   
       char value1[4];
       char value2[BUFSIZ];
       FILE *file;
       
       if ((file = fopen (name, "r")) == NULL)
       perror ("Impossible to open the file.");
       else
       {
          while ( fscanf ( file, "%s %s", value1, value2 ) == 2) 
          printf("%s\n%s\n", value1, value2);
       fclose (file);
       }
    }
    The problem is when I try to do a comparison test between value1 and an integer, I have this error message:

    Attention: Comparison between a pointer and an integer

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Problem with fgets function

    This is normal: value1 is a pointer to an array of 4 characters.

Similar Threads

  1. Read one line at a time using fgets in C
    By ElroyDJ in forum Software Development
    Replies: 6
    Last Post: 30-12-2011, 10:26 PM
  2. Problem with wildcard function
    By ASHER in forum Software Development
    Replies: 4
    Last Post: 16-02-2010, 02:57 PM
  3. Problem in boolean function
    By Luis-Fernando in forum Software Development
    Replies: 4
    Last Post: 08-02-2010, 08:02 PM
  4. Problem understanding function in C
    By DutchDude in forum Software Development
    Replies: 2
    Last Post: 05-05-2009, 11:46 PM
  5. Problem with SUMPROD function
    By Duck in forum Software Development
    Replies: 3
    Last Post: 27-04-2009, 11:41 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,123,553.24050 seconds with 17 queries