fgets erroneoulsy returns null string

This is a discussion on fgets erroneoulsy returns null string within the C Programming forums, part of the General Programming Boards category; I have been working on a problem that is driving me crazy. The fgets now returns null strings instead of ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09:33 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21