Friday, September 16, 2016

C file I/O functions

C file I/O functions

    logic how to detect end of file:

  1. (stdio.h) library details can be found here::
  2. (math.h)library details can be found here::
  3. (cstring.h library )function memset
  4. 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");
        while (fgetc(pFile) != EOF) {
        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;
  5. 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 */
        ch = getc(fp);
      if (feof(fp))
         printf("\n End of file reached.");
         printf("\n Something went wrong.");
      return 0;
  6. Detecting EOF in C
  7. Checking if the file pointer has reached EOF without moving the file pointer?
  8. How to use EOF to run through a text file in C?
  9. End of File (EOF) in C
  10. the following section is about file I/O related functions.

  11. C scanf()
  12. C printf()
  13. C – printf and scanf
  14. C Programming Examples on File Handling
  15. File Handling in C Language:: good examples

    an example to write to a file from std input and display the file::

     FILE *fp;
     char ch;
     fp = fopen("one.txt", "w");
     printf("Enter data");
     while( (ch = getchar()) != EOF) {
     fp = fopen("one.txt", "r");
     while( (ch = getc(fp)! = EOF)

    another example::

    /* read and write to file */
    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.age);
       fprintf(p,"%s %d",, e.age);
          fscanf(q,"%s %d",, e.age);
          printf("%s %d",, e.age);
       while( !feof(q) );
  16. File Handling in C with Examples (fopen, fread, fwrite, fseek)
  17. File Handling in C Language
  18. Detecting EOF in C
  19. How to use EOF to run through a text file in C?

No comments:

Post a Comment