Advertisement
mcgizmo

Untitled

Dec 28th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. /* Write a program that accepts as parameters the names of the three files and copy their contents to a new file as follows:
  2.  
  3. One line first file
  4. One line second file
  5. One line from a third
  6. 2 lines of the first file
  7. 2 rows from a hand
  8. 2 rows from a third
  9. .
  10. .
  11. .
  12. i lines of the first file
  13. i ranks second file
  14. i rows from a third
  15.  
  16. If one short files from others - others continue to copies files. */
  17.  
  18.  
  19. #include<stdio.h>
  20. #include<stdlib.h>
  21. #define N 1000
  22. // main() must return an integer
  23. int main(int argc, char *argv[]) //settings function main ()
  24. {
  25. int i, j = 0;
  26. char content[N];
  27. FILE *file1=NULL, *file2=NULL, *file3=NULL, *copyFile=NULL;
  28.  
  29. if (argc > 4) // cheking if a we have not more 3 files
  30. {
  31. printf("Incompatible number of parameters!");
  32. return 1;
  33. }
  34.  
  35. // open a file for write a new content from 3 another files
  36. copyFile = fopen("d:\\homework\\myfile.txt", "w");
  37. // opening 3 files to read a content and whose path is passed as an argument
  38. file1 = fopen("argv[1]", "r"); // can write the same example file1 = fopen("d:\\homework\\myfile2.txt", "r")
  39. file2 = fopen("argv[2]", "r"); // can write the same example file2 = fopen("d:\\homework\\myfile3.txt", "r")
  40. file3 = fopen("argv[3]", "r"); // can write the same example file3 = fopen("d:\\homework\\myfile4.txt", "r")
  41.  
  42. // copyFile returns NULL pointer on failure
  43. if (copyFile == NULL)
  44. printf("Can't create a file to store a data\n");
  45. // fopen returns NULL pointer on failure
  46. if ((file1 == NULL) || (file2 == NULL) || (file3 == NULL))
  47. printf("File can not be open\n");
  48. else
  49. // do the loop for reading the files from file1,file2 and file3 and wrting in to the copyFile
  50. while (!feof(file1) || !feof(file2) || !feof(file3))
  51. {
  52. for (i = 0; i < j + 1; i++) // loop for file1
  53. {
  54. if (fgets(content, sizeof(content), file1) != NULL) // reading lines from a file
  55. puts(content); // output on display
  56. fprintf(copyFile, "%s\n", content);
  57. }
  58. for (i = 0; i < j + 1; i++) //loop for file2
  59. {
  60. if (fgets(content, sizeof(content), file2) != NULL) // reading lines from a file
  61. puts(content); // output on display
  62. fprintf(copyFile, "%s\n", content);
  63. }
  64. for (i = 0; i < j + 1; i++) // loop for file3
  65. {
  66. if (fgets(content, sizeof(content), file3) != NULL) // reading lines from a file
  67. puts(content); // output on display
  68. fprintf(copyFile, "%s\n", content);
  69. }
  70. j++;
  71. }
  72. fclose(file1); // file1 is the file pointer associated with file to be closed
  73.  
  74. fclose(file2) ; // file2 is the file pointer associated with file to be closed
  75.  
  76. fclose(file3); // file3 is the file pointer associated with file to be closed
  77.  
  78. fclose(copyFile); // copyFile is the file pointer associated with file to be closed
  79.  
  80. return 0; // Zero indicates success, while any
  81. // Non-Zero value indicates a failure/error
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement