0% found this document useful (0 votes)
35 views11 pages

Prac 10

The document contains code to create a text file, write a sentence to it, then read the sentence back and output it. It also contains code to read numbers from a data file, write odd numbers to one file and even numbers to another file, then output the contents of the two files.

Uploaded by

Hemant Gaurkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views11 pages

Prac 10

The document contains code to create a text file, write a sentence to it, then read the sentence back and output it. It also contains code to read numbers from a data file, write odd numbers to one file and even numbers to another file, then output the contents of the two files.

Uploaded by

Hemant Gaurkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

//10A

#include <stdio.h>

#include <stdlib.h>

int main()

char str[1000];

FILE *fptr;

char fname[20]="test";

printf("\n\n Create a file (test.txt) and input text :\n");

printf("----------------------------------------------\n");

fptr=fopen(fname,"w");

if(fptr==NULL)

printf(" Error in opening file!");

exit(1);

printf(" Input a sentence for the file : ");

fgets(str, sizeof str, stdin);

fprintf(fptr,"%s",str);

fclose(fptr);

printf("\n The file %s created successfully...!!\n\n",fname);

return 0;
}
//10B

#include <stdio.h>

#include <stdlib.h>

int main()

char str[1000];

FILE *fptr;

char fname[20]="test";

printf("\n\n Create a file (test) and input text :\n");

printf("----------------------------------------------\n");

fptr=fopen(fname,"w");

if(fptr==NULL)

printf(" Error in opening file!");

exit(1);

printf(" Input a sentence for the file : ");

fgets(str, sizeof str, stdin);

fprintf(fptr,"%s",str);

fclose(fptr);

printf("\n The file %s created successfully...!!\n\n",fname);


fptr=fopen(fname,"r");

if(fptr==NULL)

printf(" Error in opening file!");

exit(1);

printf(" Output a sentence for the file : ");

printf("%s",str);

return 0;

}
#include<stdio.h>

main()

FILE *f1, *f2, *f3;

int number , i;

printf("Contents of Data File \n\n");

f1 = fopen("C:\\Users\\STUDENTS\\Desktop\\PRASHIK\\DATA","w");

for(i=1;i<=10;i++)

scanf("%d",&number);

if (number == -1) break;

putw(number,f1);

fclose(f1);

f1 = fopen("C:\\Users\\STUDENTS\\Desktop\\PRASHIK\\DATA","r");

f2 = fopen("C:\\Users\\STUDENTS\\Desktop\\PRASHIK\\ODD","w");

f3 = fopen("C:\\Users\\STUDENTS\\Desktop\\PRASHIK\\EVEN","w");

while ((number = getw(f1)) != EOF)

if(number % 2 == 0)

putw(number,f3);

else

putw(number, f2);
}

fclose(f1);

fclose(f2);

fclose(f3);

f2 = fopen("C:\\Users\\STUDENTS\\Desktop\\PRASHIK\\ODD","r");

f3 = fopen("C:\\Users\\STUDENTS\\Desktop\\PRASHIK\\EVEN","r");

printf("\nContents of ODD file \n");

while((number = getw(f2)) != EOF)

printf(" %d", number);

printf("\nContents of EVEN file \n");

while((number = getw(f3)) != EOF)

printf(" %d", number);

fclose(f2);

fclose(f3);

You might also like