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");
      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;
    }
    
  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 */
        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;
    }
    
  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::

    #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();
    }
    
  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