Thread: fgets erroneoulsy returns null string

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    15

    fgets erroneoulsy returns null string

    I have been working on a problem that is driving me crazy. The fgets now returns null strings instead of the strings in the file it is reading. I extracted the code to the simplest snippet (below). I have changed my gcc from 3.4 to 4.1.1, reloaded all libraries I can identify in cygwin. fgets does work on stdin. My environment is Windows Server 2003 running Cygwin with gcc compiler. The only thing I can say that changed was the installation of gdb 6.5 (debugger) which I loaded to troubleshoot another problem (which I found and fixed). So, what libraries should I be looking at?

    Your help will be greatly appreciated.

    Thanks,
    Leon

    Here's sample code:

    Code:
    #include <stdio.h>
    
    char ch[100];
    char temp_str[200];
    char read_string[200];
    FILE *conf_file;
    
    int main(void)
    {
      printf("enter a string to test the fgets function on stdin: ");
      fgets(ch,100,stdin);
      printf("stdin string is: %s\n",ch);
      /* open the config file*/
      sprintf(temp_str, "D:\\c_projects\\logs.c");
      conf_file = fopen(temp_str, "r");
      if (conf_file == NULL)
      {
        printf("fatal error - failed to open file: %s", temp_str); 
        exit(1);
      }
      if (feof(conf_file))
      {
        printf("end of file encountered\n");
        exit(1);
      }
    
      printf("file: %s has been opened\n", temp_str);
    
      while ( fgets(read_string, strlen(read_string), conf_file) != NULL)
      {
        if (ferror(conf_file))
        {
          printf("read config fail error\n");
          exit(1);
        }
        printf("read_string is: %s\n", read_string);
      }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The fgets now returns null strings instead of the strings in the file it is reading.
    > while ( fgets(read_string, strlen(read_string), conf_file) != NULL)
    That's because strlen(read_string) is zero at this point (because read_string is declared global, it's initialized to an empty string (""). What you really want is sizeof(read_string):
    Code:
      while ( fgets(read_string, sizeof(read_string), conf_file) != NULL)

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    Thanks! It is amazing how I can look and look and not see what I did wrong!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sprintf(temp_str, "D:\\c_projects\\logs.c");
    This is a fairly expensive way of doing
    strcpy(temp_str, "D:\\c_projects\\logs.c");

    Unless it is your intent to start generating more complicated filenames at some point.
    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. fgets returns null
    By strider1974 in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 05:08 AM
  2. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 11:04 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM