Home » C solved programs » C file handling programs

C program to create a text file using file handling, example of fopen, fclose

#include< stdio.h >
int main()
{
 
    FILE *fp;   /* file pointer*/
    char fName[20];
 
    printf("Enter file name to create :");
    scanf("%s",fName);
 
    /*creating (open) a file, in “w”: write mode*/
    fp=fopen(fName,"w");
    /*check file created or not*/
    if(fp==NULL)
    {
        printf("File does not created!!!");
        exit(0); /*exit from program*/
    }
 
    printf("File created successfully.");
    return 0;
}

Output

    Run 1:
    Enter file name to create : file1.txt
    File created successfully.

    Run 2:
    Enter file name to create : d:/file1.txt
    File created successfully.

    “file will be created in d: drive”.

    Run 3:
    Run 1:
    Enter file name to create : h:/file1.txt
    File does not created!!!






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.