0% found this document useful (0 votes)
1 views2 pages

Chapter 10 Code

Uploaded by

yy2482442
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Chapter 10 Code

Uploaded by

yy2482442
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Language Tutorial

(Basic to Advanced)

Topics to be covered :
Installation + Setup
Chapter 1 - Variables, Data types + Input/Output
Chapter 2 - Instructions & Operators
Chapter 3 - Conditional Statements
Chapter 4 - Loop Control Statements
Chapter 5 - Functions & Recursion
Chapter 6 - Pointers
Chapter 7 - Arrays
Chapter 8 - Strings
Chapter 9 - Structures
Chapter 10 - File I/O
Chapter 11 - Dynamic Memory Allocation

File I/O
(Chapter 10)

# include <stdio.h>

int main() {
FILE *fptr;
//Reading a file
char ch;
fptr = fopen("[Link]", "r");
if(fptr == NULL) {
printf("doesn't exist!\n");
} else {
fscanf(fptr, "%c", &ch);
printf("character in file is : %c\n", ch);
fscanf(fptr, "%c", &ch);
printf("character in file is : %c\n", ch);
fclose(fptr);
}

//Writing in a file
ch = 'M';
fptr = fopen("[Link]", "w");
fprintf(fptr, "%cANGO", ch);
fclose(fptr);

//fgets
fptr = fopen("[Link]", "r");
printf("character in file is : %c\n", fgetc(fptr));
printf("character in file is : %c\n", fgetc(fptr));
printf("character in file is : %c\n", fgetc(fptr));
printf("character in file is : %c\n", fgetc(fptr));
printf("character in file is : %c\n", fgetc(fptr));
fclose(fptr);

//fputc
fptr = fopen("[Link]", "w");
fputc('a', fptr);
fputc('p', fptr);
fputc('p', fptr);
fputc('l', fptr);
fputc('e', fptr);
fclose(fptr);

//read the full file (EOF)


fptr = fopen("[Link]", "r");
ch = fgetc(fptr);
while(ch != EOF) {
printf("%c", ch);
ch = fgetc(fptr);
}
printf("\n");
fclose(fptr);

return 0;
}

You might also like