File Handling

Basic Syntax

FILE *file=fopen("xyx.txt","r/w/r+/w+/a");
if(!file)return printf("Error Msg"), 1;
.
.
.<<CODE>>>
.
.
fclose(file);


Red Color Highlight Meaning :
You Can Write As you Wish. Instead of file you can write suman,sabin, sunam etc, xyz.txt you can write abc.txt, xyz.doc etc.

File Creation

#include<stdio.h>
int main(){
    FILE *file=fopen("xyz.txt","w"); //here we are creating file                                             xyz.txt 
        if(!file) return printf("Tero File Sile Vetiyana, Ramro Sanga         Bana!");
        fprintf(file,"Hello Dasari!");//Content Here
        fprintf(file, "K cha tero Khabar?");
    fclose("file");
return 0;}


File Reading

Basic Syntax:

FILE *file=fopen("xyx.txt","r/w/r+/w+/a");
if(!file)return printf("Error Msg"), 1;
.
.

char line[];
fgets(line, sizeof(line),file);
printf("%s",line)
.
.
fclose(file);



Now Lets Demostrate

#include<stdio.h>
int main() {
    FILE *file = fopen("xyz.txt", "r");
        if(!file) return printf("error!"), 1;

        char line[100];
        fgets(line, sizeof(line), file);  // Read the first line
        printf("%s", line);               // Print it

    fclose(file);
return 0;
}


0 Comments had been done yet:

Post a Comment