Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program that accepts as parameters the names of the three files and copy their contents to a new file as follows:
- One line first file
- One line second file
- One line from a third
- 2 lines of the first file
- 2 rows from a hand
- 2 rows from a third
- .
- .
- .
- i lines of the first file
- i ranks second file
- i rows from a third
- If one short files from others - others continue to copies files. */
- #include<stdio.h>
- #include<stdlib.h>
- #define N 1000
- // main() must return an integer
- int main(int argc, char *argv[]) //settings function main ()
- {
- int i, j = 0;
- char content[N];
- FILE *file1=NULL, *file2=NULL, *file3=NULL, *copyFile=NULL;
- if (argc > 4) // cheking if a we have not more 3 files
- {
- printf("Incompatible number of parameters!");
- return 1;
- }
- // open a file for write a new content from 3 another files
- copyFile = fopen("d:\\homework\\myfile.txt", "w");
- // opening 3 files to read a content and whose path is passed as an argument
- file1 = fopen("argv[1]", "r"); // can write the same example file1 = fopen("d:\\homework\\myfile2.txt", "r")
- file2 = fopen("argv[2]", "r"); // can write the same example file2 = fopen("d:\\homework\\myfile3.txt", "r")
- file3 = fopen("argv[3]", "r"); // can write the same example file3 = fopen("d:\\homework\\myfile4.txt", "r")
- // copyFile returns NULL pointer on failure
- if (copyFile == NULL)
- printf("Can't create a file to store a data\n");
- // fopen returns NULL pointer on failure
- if ((file1 == NULL) || (file2 == NULL) || (file3 == NULL))
- printf("File can not be open\n");
- else
- // do the loop for reading the files from file1,file2 and file3 and wrting in to the copyFile
- while (!feof(file1) || !feof(file2) || !feof(file3))
- {
- for (i = 0; i < j + 1; i++) // loop for file1
- {
- if (fgets(content, sizeof(content), file1) != NULL) // reading lines from a file
- puts(content); // output on display
- fprintf(copyFile, "%s\n", content);
- }
- for (i = 0; i < j + 1; i++) //loop for file2
- {
- if (fgets(content, sizeof(content), file2) != NULL) // reading lines from a file
- puts(content); // output on display
- fprintf(copyFile, "%s\n", content);
- }
- for (i = 0; i < j + 1; i++) // loop for file3
- {
- if (fgets(content, sizeof(content), file3) != NULL) // reading lines from a file
- puts(content); // output on display
- fprintf(copyFile, "%s\n", content);
- }
- j++;
- }
- fclose(file1); // file1 is the file pointer associated with file to be closed
- fclose(file2) ; // file2 is the file pointer associated with file to be closed
- fclose(file3); // file3 is the file pointer associated with file to be closed
- fclose(copyFile); // copyFile is the file pointer associated with file to be closed
- return 0; // Zero indicates success, while any
- // Non-Zero value indicates a failure/error
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement