fork download
  1. #include <stdio.h>
  2. #define BUFFER_SIZE 80
  3.  
  4. int readLineFromFile(FILE* file, char* buffer) {
  5.  
  6. if(! fgets(buffer, BUFFER_SIZE, file)) {
  7. return -1;
  8. }
  9. int length;
  10. for(length=0; buffer[length] != 0; length++);
  11. return length;
  12. }
  13.  
  14. void writeLineToFile(FILE* file, char* buffer) {
  15. fprintf(file, "%s", buffer);
  16. }
  17.  
  18. int main(int argc, char **argv) {
  19. if (argc != 3) {
  20. printf("incorrect number of arguments, 2 text files expected, recieved %d\n", argc - 1);
  21. return 0;
  22. }
  23. FILE* input = fopen(argv[1], "r");
  24. FILE* output = fopen(argv[2], "w");
  25. char buffer[BUFFER_SIZE];
  26. int charsRead = readLineFromFile(input, buffer);
  27. int i;
  28. while(0 < charsRead) {
  29. writeLineToFile(output, buffer);
  30. charsRead = readLineFromFile(input, buffer);
  31. printf("%d\n", i++);
  32. }
  33. fclose(input);
  34. fclose(output);
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
incorrect number of arguments, 2 text files expected, recieved 0