C file I/O functions
-
(stdio.h) library details can be found here:: -
(math.h)library details can be found here:: -
(cstring.h library )function
memset -
function feof():: it contains goood library and function information. good reference
/* feof example: byte counter */ #include <stdio.h> int main () { FILE * pFile; int n = 0; pFile = fopen ("myfile.txt","rb"); if (pFile==NULL) perror ("Error opening file"); else { while (fgetc(pFile) != EOF) { ++n; } if (feof(pFile)) { puts ("End-of-File reached."); printf ("Total number of bytes read: %d\n", n); } else puts ("End-of-File was not reached."); fclose (pFile); } return 0; }
-
EOF, getc() and feof() in C
#include <stdio.h> int main() { FILE *fp = fopen("test.txt", "r"); int ch = getc(fp); while (ch != EOF) { /* display contents of file on screen */ putchar(ch); ch = getc(fp); } if (feof(fp)) printf("\n End of file reached."); else printf("\n Something went wrong."); fclose(fp); getchar(); return 0; }
- Detecting EOF in C
- Checking if the file pointer has reached EOF without moving the file pointer?
- How to use EOF to run through a text file in C?
- End of File (EOF) in C
- C scanf()
- C printf()
- C – printf and scanf
- C Programming Examples on File Handling
-
File Handling in C Language:: good examples
an example to write to a file from std input and display the file::
#include<stdio.h> #include<conio.h> main() { FILE *fp; char ch; fp = fopen("one.txt", "w"); printf("Enter data"); while( (ch = getchar()) != EOF) { putc(ch,fp); } fclose(fp); fp = fopen("one.txt", "r"); while( (ch = getc(fp)! = EOF) printf("%c",ch); fclose(fp); }
another example::
/* read and write to file */ #include<stdio.h> #include<conio.h> struct emp { char name[10]; int age; }; void main() { struct emp e; FILE *p,*q; p = fopen("one.txt", "a"); q = fopen("one.txt", "r"); printf("Enter Name and Age"); scanf("%s %d", e.name, &e.age); fprintf(p,"%s %d", e.name, e.age); fclose(p); do { fscanf(q,"%s %d", e.name, e.age); printf("%s %d", e.name, e.age); } while( !feof(q) ); getch(); }
- File Handling in C with Examples (fopen, fread, fwrite, fseek)
- File Handling in C Language
- Detecting EOF in C
- How to use EOF to run through a text file in C?
logic how to detect end of file:
the following section is about file I/O related functions.
No comments:
Post a Comment