Showing posts with label File Handling. Show all posts

Write program to store data in file object oriented approch.

#include <iostream>
#include <string.h>
#include <fstream>
using namespace::std;
class student
{
      string name;
      int age;
      public:
      student()     //constructor
      {
              
      }
      ~student()   //distructor
      {
               
      }
      void setData(string n, int a)     //setter function
      {
           name=n;
           age=a;
      }
      void save()// dada save in file
      {
           ofstream outfile;  // create object of of class
           outfile.open("abs.txt");  //open file inwhich data will be stored
           outfile<<"Your name is "<<name<<"\nYour age is "<<age<<endl;  // data is storing in file.
           cout<<"saved\n";
      }
      void Dispaly()                   //getter function
      {
           cout<<"Your name is "<<name<<"\nYour age is "<<age<<endl;
      }
};
int main()
{
       student a; // object of class student
       string name;
       int age;
       cout<<"Enter your name: ";
       getline(cin, name); //get complete line from user.
       cout<<"Enter your age: ";
       cin>>age;
       a.setData(name, age); // calling setter function
       a.save();
       a.Dispaly(); // calling getter function
       system("pause"); // hold screen untill user enter any character from screen
}

Write a programe in c language to get file name from user and read it. If file name does not exist then print file does not exist



#include <stdio.h>
#include <conio.h>
main()
{
   
FILE *fp;
char a, read;
char b[]="fahad.txt";
again:
printf("\nEnter name of file: ");
scanf("%s", &b);
fp=fopen(b, "r");
if(fp==NULL)
{
            printf("\nFile is Empty or not exist.\n");        
}
else
{
    while(read!=EOF)
    {
                    read=getc(fp);
                    printf("%c", read);            
    }
}
printf("\nYou want read again data from file? Y/N");
         a=getch();
         if(a == 'y' || a == 'Y')
         {
              printf("\n");
                goto again;    
         }
}

Write a program to get data from user and write into a file in c language.

#include <stdio.h>

#include <conio.h>
int main()
{
         again:
         FILE *fp;
         fp=fopen("g.txt", "a+");
         char a;
         a=getche();    
         while(a!='.')
         {
                      fputc(a, fp);
                       a=getche();        
                 
         }
         fprintf(fp, "\n");
         printf("\nPrograme is ended, would you like to input again? y/n: ");
         a=getch();
         if(a == 'y' || a == 'Y')
         {
              printf("\n");
                goto again;  
         }
}

Write a program to read data from a file.


#include <stdio.h>
#include <conio.h>
int main()
{
    FILE *fp;
    char a;
    fp=fopen("file_name.txt", "r");
    while(a!=EOF)
    {
                  a=fgetc(fp);
                  printf("%c", a);          
             
    }

    getch();
}