Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • File I/O in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 55
    Comment on it

    File represents a set of bytes whether a text file or a binary file.

     

    File is used to store data in the textual or in encoded format as per as use requirement.

     

    Modes of File

    Mode Description
    r Opens an existing text file for reading purpose.
    w Opens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file.
    a Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content.
    r+ Opens a text file for both reading and writing.
    w+ Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.
    a+ Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.

     

    Opening a File:

    For opening a file you will use fputs function

    FILE *fopen( const char * filename, const char * mode );

     

     

    Closing a File :

    For closing a file you will use fclose function.

     

    int fclose( FILE *fp );

     

    Reading a File :

     

    #include <stdio.h>
    
    main() {
       FILE *fp;
    
       fp = fopen("/tmp/test.txt", "w+");
       fprintf(fp, "This is testing for fprintf...\n");
       fputs("This is testing for fputs...\n", fp);
       fclose(fp);
    }

    Writing a File :
     

    For writing a file you will use fputs function

     

    int fputs( const char *s, FILE *fp );

     

    Writing a File :

     

    #include <stdio.h>
    
    main() {
       FILE *fp;
    
       fp = fopen("/tmp/test.txt", "w+");
       fprintf(fp, "This is testing for fprintf...\n");
       fputs("This is testing for fputs...\n", fp);
       fclose(fp);
    }

     

     

    Reading a File :

     

    #include <stdio.h>
    
    main() {
    
       FILE *fp;
       char buff[255];
    
       fp = fopen("/tmp/test.txt", "r");
       fscanf(fp, "%s", buff);
       printf("1 : %s\n", buff );
    
       fgets(buff, 255, (FILE*)fp);
       printf("2: %s\n", buff );
       
       fgets(buff, 255, (FILE*)fp);
       printf("3: %s\n", buff );
       fclose(fp);
    
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: