Showing posts with label File. Show all posts
Showing posts with label File. Show all posts

COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM

How To COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM.


#include<stdio.h>
int main(){
  FILE *p,*q;
  char file1[20],file2[20];
  char ch;
  printf("\nEnter the source file name to be copied:");
  gets(file1);
  p=fopen(file1,"r");
  if(p==NULL){
      printf("cannot open %s",file1);
      exit(0);
  }
  printf("\nEnter the destination file name:");
  gets(file2);
  q=fopen(file2,"w");
  if(q==NULL){
      printf("cannot open %s",file2);
      exit(0);
  }
  while((ch=getc(p))!=EOF)
      putc(ch,q);
  printf("\nCOMPLETED");
  fclose(p);
  fclose(q);
 return 0;
}

C Program File Input & Output Example

How to write a C Program File Input & Output Example in C Programming Language ?


Solution For C Program :

C Program To Destruc Self Execution File ?

How to write a C Program To Destruc Self Execution File in C Programming Language ?


Solution For C Program :

/*C Program To Destruc Self Execution File.*/

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
printf("This program will destroy itself if u press any key!!!\n");
getch();
remove(_argv[0]);/*array of pointers to command line arguments*/
}

C Program To Store Students Record In A Text File

How to write a C Program To Store Students Record In A Text File in C Programming Language ?


Solution For C Program :

/*C Program To Store Students Record In A Text File.*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    FILE *fp;
    char another='y';
    int marks1,marks2,marks3,marks4,marks5,role;
    char name[20];
    clrscr();

    fp=fopen("Students.txt","w");
    if(fp==NULL)
    {
        puts("Cannot open file");
        exit(1);
    }
       fprintf(fp,"Role No.\tNAME\t  PHYSICS  CHEMISTRY  MATHS  S.S.T  ENGLISH\n");
    while(another=='y')
    {
        printf("\nEnter name :");
        scanf("%s",&name);
        printf("Enter your Role No.:");
        scanf("%d",&role);
        printf("Enter marks in Physics:");
        scanf("%d",&marks1);
        printf("Enter marks in Chemistry:");
        scanf("%d",&marks2);
        printf("Enter marks in Mathematics:");
        scanf("%d",&marks3);
        printf("Enter marks in Social Science:");
        scanf("%d",&marks4);
        printf("Enter marks in English:");
        scanf("%d",&marks5);
        fprintf(fp,"%d\t%s\t%d\t%d\t%d\t%d\t%d\n",role, name, marks1,marks2,marks3,marks4,marks5);
        printf("Add another record (y/n): ");
        fflush(stdin);
        another=getche();
    }
    fclose(fp);
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable



C Program To Write Data In A File And Search Data From File

How to write a C Program To Write Data In A File And Search Data From File in C Programming Language ?


Solution For C Program :

/*C Program To Write Data In A File And Search Data From File.*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int r,found;
struct stud
    {
    char name[30];
    int age;
    int roll_no;
    }st;
FILE *fp;
fp=fopen("test.dat","r+t");
        if(fp==NULL)
            {
            printf("\nCannot open file\n");
            exit(1);
            }

printf("enter role no:");
found=0;
scanf("%d",&r);

while((!feof(fp))&&(found))
    {
    fread(&st,sizeof(stud),1,fp);
    if(st.roll_no==r)
    {
    fseek(fp,-sizeof(stud),SEEK_CUR);
    printf("enter new name\n");
    scanf("%s",st.name);
    fwrite(fp,sizeof(stud),1,fp);
    found=1;
    }
    }
    if(!found)
    printf("record not present");
    fclose(fp);
}

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable


Make a copy of file given in argv[1] to file given in argv[2]

How to write a C Program to Make a copy of file given in argv[1] to file given in argv[2] in C Programming Language ?

You have to use program with two arguments, the file names copy_from copy_to.

This C Program Make a copy of file given in argv[1] to file given in argv[2].

There is an access function with which we can see whether the file exists or not

 access(filename,F_OK);
 open 1. parameter= file name
 open 2. parameter for what we want to use the file
 O_RDONLY=only for reading,O_WRONLY-only for writing

There is errno variable in which there is the number of error --

 if (errno!=0) there is an error

The three parameter long version of open - it can create the file if it doesnt exist

there is a creat function as well - creat(filename,permissions);
O_TRUNC = if it existed, clear the content,
O_CREAT=create if it doesn't exist
3. parameter the permission, if it has to be created
S_IRUSR=permission for reading by the owner e.g.

Read gives back the number of bytes

    1. parameter the file descriptor
    2. parameter the address of variable, we read into
    3. parameter the number of bytes we want to read in

Write gives back the number of written bytes

      1. parameter the file descriptor
      2. parameter the address of variable we want to write out
      3. parameter the number of bytes we want to write out


Solution:

  1. //make a copy of file given in argv[1] to file given in argv[2]
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h> //open,creat
  5. #include <sys/types.h> //open
  6. #include <sys/stat.h>
  7. #include <errno.h> //perror, errno
  8. int main(int argc,char** argv){
  9.  if (argc!=3){perror("You have to use program with two arguments, the file names copy_from copy_to");exit(1);}
  10.  int f,g;
  11.  f=open(argv[1],O_RDONLY);
  12.  //there is an access function with which we can see whether the file exists or not
  13.  //access(filename,F_OK);
  14.  //open 1. parameter= file name
  15.  //open 2. parameter for what we want to use the file
  16.  //O_RDONLY=only for reading,O_WRONLY-only for writing
  17.  if (f<0){ perror("Error at opening the file\n");exit(1);}
  18.  //There is errno variable in which there is the number of error --
  19.  //if (errno!=0) there is an error
  20.  g=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
  21.  //the three parameter long version of open - it can create the file if it doesnt exist
  22.  //there is a creat function as well - creat(filename,permissions);
  23.  //O_TRUNC = if it existed, clear the content,
  24.  //O_CREAT=create if it doesn't exist
  25.  //3. parameter the permission, if it has to be created
  26.  //S_IRUSR=permission for reading by the owner e.g.
  27.  if (g<0){ perror("Error at opening the file\n");exit(1);}
  28.  char c;
  29.  while (read(f,&c,sizeof(c))){
  30.     //read gives back the number of bytes
  31.     //1. parameter the file descriptor
  32.     //2. parameter the address of variable, we read into
  33.     //3. parameter the number of bytes we want to read in
  34.     printf("%c",c); //we prints out the content of the file on the screen
  35.     if (write(g,&c,sizeof(c))!=sizeof(c))
  36.       {perror("There is a mistake in writing\n");exit(1);}
  37.       //write gives back the number of written bytes
  38.       //1. parameter the file descriptor
  39.       //2. parameter the address of variable we want to write out
  40.       //3. parameter the number of bytes we want to write out
  41.  }
  42.  close(f);
  43.  close(g);
  44.  return 0;
  45. }

C Program to copy a file to another file

How to write a C Program to copy a file to another file in C Programming Language ?

This C Program to copy a file to another file.

Solution:

  1. #include<stdio.h>
  2. void main()
  3. {
  4.         FILE *fp1,*fp2;
  5.         char ch;
  6.         char fname1[30],fname2[30];
  7.         printf("Enter the source file: ");
  8.         fflush(stdin);
  9.         scanf("%s",fname1);
  10.         printf("Enter the destination file: ");
  11.         fflush(stdin);
  12.         scanf("%s",fname2);
  13.         fp1=fopen(fname1,"r");
  14.         fp2=fopen(fname2,"w");
  15.         if(fp1==NULL)
  16.         {
  17.                 printf("\n cannot open file %s for reading",fname1);
  18.                 exit(1);
  19.         }
  20.         else if(fp2==NULL)
  21.         {
  22.                 printf("\n cannot open file %s for writing",fname2);
  23.                 exit(1);
  24.         }
  25.         else
  26.         {
  27.                 ch=getc(fp1);
  28.         while(ch!=EOF)
  29.         {
  30.                 putc(ch,fp2);
  31.                 ch=getc(fp1);
  32.         }
  33.         fclose(fp1);
  34.         fclose(fp2);
  35.         printf("\n files copied");
  36.         }
  37. }