Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does printf not print anything?

I have this code where I take a file name and generate a new file with the same file name but ending in .c

However, when I attempt to transform the file name to end in a .c, I try to print the result to check if it’s working. And printf just prints nothing. If it makes a difference, the file name is a pointer that points to argv[1] from int main(int argc, char *argv[]).

Here is the code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

bool file_genEquiCFile(char *file_name)
{
    // Maybe add a memmove to preserve the original buffer
    printf("file_name before change: %s\n", file_name);
    while (file_name && *file_name != '.')
        file_name++;

    if (file_name)
    {
        if (*file_name++ == '.')
        {
        *file_name++ = 'c';
        *file_name = '\0';

        // For now use "w" mode which makes an empty file for writing.
        FILE *c_file = fopen(file_name, "w");
        fclose(c_file);
        printf("file_name after change: %s\n", file_name);

        return true;
        }
        else
            return false;
    }
        
    return false;
}

By the way, file_name points to a file called hey.txt
Here is what printf printed:

file_name before change: hey.txt
file_name after change:

>Solution :

After *file_name = '\0';, file_name points at a 0-character string because what file_name points at is the terminating NUL character.

You should store the original file_name at the beginning of the function and use it later instead of the modified file_name later.

bool file_genEquiCFile(char *file_name)
{
    char *file_name_orig = file_name;
    // Maybe add a memmove to preserve the original buffer
    printf("file_name before change: %s\n", file_name);
    while (file_name && *file_name != '.')
        file_name++;

    if (file_name)
    {
        if (*file_name++ == '.')
        {
        *file_name++ = 'c';
        *file_name = '\0';

        // For now use "w" mode which makes an empty file for writing.
        FILE *c_file = fopen(file_name_orig, "w");
        fclose(c_file);
        printf("file_name after change: %s\n", file_name_orig);

        return true;
        }
        else
            return false;
    }
        
    return false;
}

Also you may want to use *file_name instead of file_name in the first while and if to check not if the pointer is not NULL but if what the pointer points at is a NUL character.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading