Functions For File I/O In C Language

  • r : opens a file for reading.
  • w : opens a file for writing. It can also create a new file.
  • a : opens a file for appending.
  • r+ : opens a file for both reading and writing but cannot create a new file.
  • w+ : opens a file for both reading and writing.

Basic Syntax

                                
                                    #include <stdio.h>
                                    #include <stdlib.h>
                                    int main()
                                    {
                                        FILE *ptr = NULL; // necessary
                                        char string[64] = "This content was produced by Tutorial64.c";
                                    
                                        // ****** Reading a file ******
                                        // ptr = fopen("myfile.txt", "r");
                                        // fscanf(ptr, "%s", string);
                                        // printf("The content of this file has %s\n",string );
                                    
                                        // ****** Writing a file ******
                                        ptr = fopen("myfile.txt", "a");
                                        fprintf(ptr, "%s", string);
                                    
                                        return 0;
                                    }   
                                
                            

Opening a File: fopen

                                
                                    // To Open a File
                                    // ptr = fopen(“file_location”,”mode”);  // Syntax
                                    ptr = fopen(“D:\\file.txt”,”w”);
                                
                            

Closing a File: fclose

                                
                                    // To Close a File
                                    fclose(ptr);  
                                    // same pointer name 'ptr' which was used for fopen
                                
                            

Reading a File: fscanf

                                
                                    ptr = fopen(“D:\\file.txt”,”r”);
                                    char str[128] = "Welcome to code with Harry";
                                    fscanf(ptr, "%s", str);
                                    printf("%s",str );
                                
                            

Writing to a File: fprintf

                                
                                    char str[128] = "Empty";
                                    ptr = fopen(“D:\\file.txt”,”w”);
                                    fprintf(ptr, "%s", str);
                                
                            

Basic Syntax

                                
                                        #include <stdio.h>
                                        int main()
                                        {
                                            FILE *ptr = NULL; // necessary
                                            ptr = fopen("myfile.txt", "r+");
                                            // char * c = fgetc(ptr);
                                            // printf("The character I read was %c\n", c );
                                            // c = fgetc(ptr);
                                            // printf("The character I read was %c\n", c );
                                        
                                        
                                            char str[4];
                                            fgets(str, 5, ptr);
                                            printf("The string is %s\n", str);
                                        
                                            // fputc('o',ptr);
                                            // fputs("this is harry",ptr);
                                        
                                        
                                        
                                            fclose(ptr);
                                            return 0;
                                        }
                                
                            

Write characters to the file: fputc

                                
                                    // type fputc(‘character’,file_pointer); // Syntax
                                    Int fputc(‘a’,ptr);
                                
                            

ASCII Table