/* Writing to a file. Reading from a file */
#include<stdio.h>
int main()
{
//writing to file
FILE *fpw;
fpw=fopen("1.txt","w+");
fputs("Hello India\n",fpw);
fclose(fpw);
//Reading from file
char buff[20];
FILE *fpr;
fpr=fopen("1.txt","r");
fgets(buff,20,(FILE*)fpr);
printf("%s\n",buff);
fclose(fpr);
return 0;
}
/*
Hello India
*/
---------
/*
Writing to a file
Reading from a file
Writing to another file
*/
#include<stdio.h>
int main()
{
//writing to file
FILE *fpw;
fpw=fopen("1.txt","w+");
fputs("Hello India\n",fpw);
fclose(fpw);
//Reading from file
char buff[20];
FILE *fpr;
fpr=fopen("1.txt","r");
fgets(buff,20,(FILE*)fpr);
printf("%s\n",buff);
//Writing to another file
FILE *fpo;
fpo=fopen("22.txt","w+");
fputs(buff,fpo);
//fprintf(fpo,"%s",buff);
fclose(fpr);
fclose(fpr);
return 0;
}
/*
Hello India
*/
---------
/*
Writing and reading to and from file
*/
#include <stdio.h>
int main()
{
/*Writing to a file*/
FILE *fp;
fp = fopen("test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
/*Reading from a file*/
FILE *fp1;
char buff[255];
fp1 = fopen("test.txt", "r");
fscanf(fp1, "%s", buff);//upto space
printf("1 : %s\n", buff );
fgets(buff, 255, (FILE*)fp1);//upto new line
printf("2: %s\n", buff );
fgets(buff, 255, (FILE*)fp1);//last line
printf("3: %s\n", buff );
fclose(fp1);
return 0;
}
/*
1 : This
2: is testing for fprintf...
3: This is testing for fputs...
*/
/*
The functions fgets() reads up to n - 1 characters from the input stream referenced by fp.
It copies the read string into the buffer buf, appending a null character to terminate the string.
We can also use int fscanf(FILE *fp, const char *format, ...) function to read strings froma file but it stops reading after the first space character encounters.
*/
No comments:
Post a Comment