Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int main() {
- FILE *readFile, *writeFile; /*creating 2 files*/
- char * buff = (char*)malloc(sizeof(char)); /*creating and memory allocation for the buffer*/
- readFile = fopen("FileRead.txt", "r"); /*opening a read file named FileRead.txt*/
- writeFile = fopen("FileOut.txt", "w"); /*opening a write file named FileOut.txt*/
- if (readFile == NULL) { /* if file to read from doesn't exist*/
- printf("ERROR, Unable to open [FileRead.txt].....exiting program");
- exit; /* force program exit */
- } else {
- printf("[FileRead.txt] opened successfully \n");
- }
- if (writeFile == NULL) { /* if file to write to doesn't exist*/
- printf("ERROR, Unable to create [FileOut.txt].....exiting program");
- exit; /* force program exit */
- }
- while(fscanf(readFile, "%s", buff ) != EOF){ /*as long as the buffer doesnt hit the end of file...continue*/
- fprintf(writeFile, "I read from FileRead.txt -> %s\n", buff); /*writing the following */
- }
- printf("[FileRead.txt] successfully written to [FileOut.txt]");
- /*closing both files*/
- fclose(readFile);
- fclose(writeFile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement