Big problems with the Text function

This is a discussion on Big problems with the Text function within the C Programming forums, part of the General Programming Boards category; > Do the strncpy, strcpy and strcat functions add the '

Thread: Big problems with the Text function

' at the end of the string Not always (not ...

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > Do the strncpy, strcpy and strcat functions add the '\0' at the end of the string
    Not always (not if source string length is > n)
    strcpy and strcat always append a \0

    In addition, strcat first seeks the existing \0, so if you missed this, then there's a problem in itself.

    > Now I'm trying with printf's to detect the bug but I just can't find it
    You're unlikely to find the bug by this method (or perhaps even with the debugger, though that would be a better approach).

    The problem is such that you're overwriting something you shouldn't in a very subtle way.

    The basic problem here, is whenever you change the code, you change the problem. You know this already, just recompiling it with different options causes a change in the run-time behaviour of your code.

    These tricky problems need you to stare at the code, and work out where potential memory problems exist.

  2. #17
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Hammer, the mistake was only made here, when I posted the code and not in my program.

    Now I discovered that when I try to open the file in the ReadLanguage () function with the STPFILE constant, I get the error that the file could not be opened. That's strange. Beacuse when I write it like this "fopen ("setup.cfg", "rb")" everything is OK. What could be wrong??
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #18
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When you spot that you cannot open the file, use this code to find out why (or a similar version of):
    Code:
    if ((fp = fopen(STPFILE, "rb")) == NULL)
    {
           perror(STPFILE);  /* This will print a sensible error message. */
           return (1);
    }
    /* good open, so continue */
    Last edited by Hammer; 05-22-2002 at 04:48 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #19
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    The bug is 100% in this function:
    Code:
    char *ReadLanguage () // function reads a language file from the file "setup.cfg"
    {
        FILE *fin = NULL;
        Setup file;
    
        if ((fin = fopen (STPFILE, "rb")) != NULL)
        {
            fread (&file, sizeof (Setup), 1, fin);
    	fclose (fin);	
    	return file.LanguageFile;
        }
        else return NULL;
    }
    The file STPFILE is opened but I get no return. It's like that the "file.LanguageFile" is empty, but when I open the "setup.cfg" I see that is not empty and contains the right path and filename. Anyone knows what could be wrong in this function?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #20
    Unregistered
    Guest
    Probably this

    >fread (&file, sizeof (Setup), 1, fin);

    is going wrong. I have no C reference here, but you could debug like this to see if you've read properly:

    Code:
    char *ReadLanguage () // function reads a language file from the file "setup.cfg"
    {
        FILE *fin = NULL;
        Setup file;
    
        if ((fin = fopen (STPFILE, "rb")) != NULL)
        {
            fread (&file, sizeof (Setup), 1, fin);
    
            printf ("The read language file is %d\n", file.LanguageFile);
    
            fclose (fin);	
    	return file.LanguageFile;
        }
        else return NULL;
    }

  6. #21
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I already wrote that I get the empty string. And I'm sure that the fread sentence is wrong, but I don't know how to fix it.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  7. #22
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The file STPFILE is opened but I get no return. It's like that the "file.LanguageFile" is empty, but when I open the "setup.cfg" I see that is not empty and contains the right path and filename. Anyone knows what could be wrong in this function?
    - How do you know the file is opened correctly?
    - Put the perror() line in the bottom end of the function to ensure that it tells you if it fails to do the open.
    - Put printf() in, like suggested before, except, don't do this:
    >printf ("The read language file is %d\n", file.LanguageFile);
    because if will fail. You'll need something like this
    >printf ("The read language file is %s\n", file.LanguageFile);

    And last, change your fread() call to something like this
    Code:
    if (fread (&file, sizeof (Setup), 1, fin) != 1)
    {
         printf("Failed to read 1 record from the config file\n");
         fclose(fin);
         return (NULL);
    }
    This will ensure that you actually read enough data from the config file to file the struct. I presume that your config file has enough bytes in it?? If not, fread() won't read anything in.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #23
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    How do you know the file is opened correctly?
    - Put the perror() line in the bottom end of the function to ensure that it tells you if it fails to do the open.
    Beacuse I put the perror in the if sentence and then it tells me that there is no error.

    Put printf() in, like suggested before, except, don't do this:
    >printf ("The read language file is %d\n", file.LanguageFile);
    because if will fail. You'll need something like this
    >printf ("The read language file is %s\n", file.LanguageFile);
    In the if sentence I get the result. But when I was calling the function the result was an empty string. And of course, I used the second sentence to do that.

    And finally I found the bug. The bug was the "return file.LanguageFile" sentence. Here's the code that fixes the bug .
    Code:
    char *ReadLanguage () // function reads a language file from the file "setup.cfg"
    {
        FILE *fin = NULL;
        Setup file;
        char *Text = NULL, *Temp = NULL;
    
        if ((fin = fopen (STPFILE, "rb")) != NULL)
        {
            fread (&file, sizeof (Setup), 1, fin);
    	fclose (fin);	
            Text = malloc (sizeof (char) * 30);
            if (Text == NULL) exit (1); // this is just temporary
            Temp = Text;
            strncpy (Temp, file.LanguageFile, strlen (file.LanguageFile) + 1);
    	return Text;
        }
        else return NULL;
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  9. #24
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I see, so file.LanguageFile was just a pointer. What I don't understand is why you have temp and text. It seems to me that you can use text only.

    Code:
    char *ReadLanguage () // function reads a language file from the file "setup.cfg"
    {
        FILE *fin = NULL;
        Setup file;
        char *Text = NULL;
    
        if ((fin = fopen (STPFILE, "rb")) != NULL)
        {
            fread (&file, sizeof (Setup), 1, fin);
    	fclose (fin);	
            Text = malloc (sizeof (char) * 30);
            if (Text != NULL)
            {
                strncpy (Text, file.LanguageFile, strlen (file.LanguageFile) + 1);
            }
    	return Text;
        }
        else return NULL;
    }

  10. #25
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I thought that I'll get that warning that I must not return the temporary variable.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  11. #26
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Aren't both temp and text local variables? You can return text, it is a pointer. So when returning text, you return the start address of the text where text points to. This is valid.

  12. #27
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Ok. Thanks guys for helping me.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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