Solution for Exercise 23-6

ex2306

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *test;

    test=fopen("blorfus","w");
    if(!test)
    {
        puts("Unable to create file");
        exit(1);
    }
    fclose(test);
    puts("File created");
    if(rename("blorfus","wambooli") == -1)
    {
        puts("Unable to rename file");
        exit(1);
    }
    puts("File renamed");
    return(0);
}

Output

File created
File renamed

Notes

* Your code can probe for further reasons why a file management function like rename() fails. View my blog post on the errno constant here.