chris2656
chris2656

Reputation: 57

copying files in C program?

Im trying to copy a mal file to a text file. So basically I want the contents of the mal file to copy over to the text file. the mal file is name test1.mal and the txt file is name output.txt. This is what I have but it keeps printing out error reading the file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main(void) {

char content[255];
char newcontent[255];

FILE *fp1, *fp2;
fp1 = fopen("test1.mal", "r");
fp2 = fopen("output.txt", "w");

if(fp1 == NULL || fp2 == NULL)
{
printf("error reading file\n");
exit(0);
}
printf("files open correct\n");
while(fgets(content, sizeof (content), fp1) !=NULL)
{
fputs(content, stdout);
strcpy (content, newcontent);
}

printf("%s", newcontent);
printf("text received\n");

while(fgets(content, sizeof(content), fp1) !=NULL)
{
fprintf(fp2, newcontent);
}
printf("file created and text copied");

fclose(fp1);
fclose(fp2);
return 0;
}

Upvotes: 0

Views: 1679

Answers (1)

user3629249
user3629249

Reputation: 16540

The posted code has several problems, many of which are expressed in the comments to the OP's question.

The following code is one way to perform the desired operation.

It cleanly compiles and performs appropriate error checking

Note: the calls to perror() will output, to stderr, the enclosed text and the reason the OS thinks the operation failed.

Note: used open(), close(), read(), write() because there is no guarantee that the input .mal file does not contain embedded NUL characters.

#include <stdio.h>    // perror()
#include <stdlib.h>   // exit(), EXIT_FAILURE


#include <unistd.h>   // read(), write(), close()
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>    // open()

// declare the size of the buffers with a meaningful name
// do not use 'magic' numbers
#define BUFF_SIZE 255

int main(void)
{

    char content[ BUFF_SIZE ];

    int fin;
    int fout;

    if( 0 > (fin = open("test1.mal", O_RDONLY) ) )
    {
        perror( "open for read of test1.mal failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, open successful

    if( 0 > (fout = open("output.txt", O_WRONLY) ) )
    {
        perror( "open for write of output.txt failed");
        close( fin );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    printf("files open correct\n");

    ssize_t readCount;
    while( 0 < (readCount = read( fin, content, sizeof( content) ) ) )
    {
        //fputs(content, stdout);  // are you sure the file contents are printable?
        if( readCount != write( fout, content, (size_t)readCount ) )
        { // then write error occured
            perror( "write of data to output file failed" );
            close( fin );
            close( fout );
            exit( EXIT_FAILURE );
        }

        // implied else, write successful
    }

    if( 0 > readCount )
    { // then read error occurred
        perror( "read of file failed" );
        close( fin );
        close( fout );
        exit( EXIT_FAILURE );
    }

    // implied else, complete file copied

    printf("file created and text copied\n");

    close( fin );
    close( fout );
    return 0;
} // end function: main

Upvotes: 3

Related Questions