C File Handling


Q. 1: Write a program to print Hello World in a text file, by using C .

Ans.

          #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void main()
     {
      FILE *fp;
      char store[]="Hello World ";
      clrscr();
      fp = fopen("your_file_name.txt", "w");
      fprintf(fp, "%s", store);
      fclose(fp);
     }

Message : You will find the text file in the BIN Directory which is inside your Turbo Installation (D:\TurboC++\Disk\TurboC3\BIN) .

Q. 2: Write a program to write some data in text file which is  input from the user screen.

Ans.
    #include<stdio.h> 
    #include<conio.h> 
    #include<string.h>   
    void main()    
    {    
     FILE *fp;  
     char store[50];  
     clrscr(); 
     printf("Enter what you want to write:>");  
     gets(store);   
     fp = fopen("your_file_name.txt", "w");   
     fprintf(fp, "%s", store);  
     fclose(fp); 
     getch(); 
    }

Q. 3:  Create a text file by using c program . By which you can write some text Document into the text file. But cant see in compiler's user screen, but also you can see in the folder directory.

Ans.

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void main()
     {
      FILE *fp;
      char store[50];
      int index;
      clrscr();
      fp = fopen("your_file_name.txt", "w");
      printf("Type something you want to write in file:>");
      gets(store);
      for(index=1; index<=7; index++)
        fprintf(fp,"Line : %d > %s\n",index, store);
      fclose(fp);
      getch();
     }
       Message : you will find that what  you entered that is print for 7 times. That is for the                       using  of (for_loop).

Post a Comment

0 Comments