Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main() {
  5.  
  6. FILE *readFile, *writeFile; /*creating 2 files*/
  7.  
  8. char * buff = (char*)malloc(sizeof(char)); /*creating and memory allocation for the buffer*/
  9.  
  10. readFile = fopen("FileRead.txt", "r"); /*opening a read file named FileRead.txt*/
  11. writeFile = fopen("FileOut.txt", "w"); /*opening a write file named FileOut.txt*/
  12.  
  13. if (readFile == NULL) { /* if file to read from doesn't exist*/
  14. printf("ERROR, Unable to open [FileRead.txt].....exiting program");
  15. exit; /* force program exit */
  16. } else {
  17. printf("[FileRead.txt] opened successfully \n");
  18. }
  19. if (writeFile == NULL) { /* if file to write to doesn't exist*/
  20. printf("ERROR, Unable to create [FileOut.txt].....exiting program");
  21. exit; /* force program exit */
  22. }
  23. while(fscanf(readFile, "%s", buff ) != EOF){ /*as long as the buffer doesnt hit the end of file...continue*/
  24. fprintf(writeFile, "I read from FileRead.txt -> %s\n", buff); /*writing the following */
  25.  
  26. }
  27. printf("[FileRead.txt] successfully written to [FileOut.txt]");
  28. /*closing both files*/
  29. fclose(readFile);
  30. fclose(writeFile);
  31.  
  32. return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement