Вопрос

I have these multiple errors and warnings, and I've tried just about everything and can't figure it out. Your help is greatly appreciated it! This is my code:

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

int main()
{   
    /* Create Usable Variables */
    FILE *src_p, *dst_p;
    char src_file[20], dst_file[20];
    char c;

    /* Retrieve Source File Name From User */
    printf("Enter Source File Name:\n");
    fgets(src_file, 19, stdin);

    /* Attempt Opening Source File For Reading */
    if( (src_p = fopen(src_file, "r")) == NULL )
    {
        printf("ERROR: Source File Failed To Open...\n");
        return(-1);
    }

    /* Retrieve Destination File Name From User */
    printf("Enter Destination File Name:\n");
    fgets(dst_file, 19, stdin);

    /* Attempt Opening Destination File For Writing */
    if( (dst_p = fopen(dst_file, "w")) == NULL )
    {
        fclose(src_p);
        printf("ERROR: Destination File Failed To Open...\n");
        return(-2);
    }

    /* Copy Source File Contents Into Destination File */
    while( (c = fgetc(src_p)) != EOF )
        fputc(c, dst_file);

    /* Close Files On Success */
    fclose(src_p);
    fclose(dst_p);

    return 0;
}

and the error when I try to compile it is this:

copyfile.c: In function ‘main’: copyfile.c:44:3: warning: passing argument 2 of ‘fputc’ from incompatible pointer type [enabled by default] In file included from copyfile.c:1:0: /usr/include/stdio.h:573:12: note: expected ‘struct FILE *’ but argument is of type ‘char *’

Your help is greatly appreciated!!

Это было полезно?

Решение 2

You need to realize that when you fgets the filename, you are reading an input line that includes (depending upon operating system) a linefeed, carriage return, or both. So you need a convenient way to remove these extraneous character(s).

Borrowing an idea from perl, et al, I like chomp(), which removes the '\n' and '\r' from the end of the line.

You also need to be careful with your filenames versus file handles, the fputc on the filename rather than the file handle.

And char[20] is rather small for a filename, try 200+; and you might find that "w+" will create the output file when it doesn't exist; and use sizeof(variable) for fgets size, rather than hardcoded sizes.

This works,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* chomp(char* p)
{
    int len;
    if(!p) return(p);
    if( (len=strlen(p))<=0 ) return(p);
    if( p[len-1] == '\n' ) { p[--len] = '\0'; }
    if( p[len-1] == '\r' ) { p[--len] = '\0'; }
    return(p);
}
int main()
{
    /* Create Usable Variables */
    FILE *src_fh, *dst_fh;
    char src_fn[256+1], dst_fn[256+1];

    /* Retrieve Source File Name From User */
    printf("Enter Source File Name:\n");
    fgets(src_fn, sizeof(src_fn), stdin); chomp(src_fn);

    /* Attempt Opening Source File For Reading */
    if( (src_fh = fopen(src_fn, "r")) == NULL )
    {
        printf("ERROR: Source File %s Failed To Open...\n",src_fn);
        return(-1);
    }

    /* Retrieve Destination File Name From User */
    printf("Enter Destination File Name:\n");
    fgets(dst_fn, sizeof(dst_fn), stdin); chomp(dst_fn);

    /* Attempt Opening Destination File For Writing */
    if( (dst_fh = fopen(dst_fn, "w+")) == NULL )
    {
        fclose(src_fh);
        printf("ERROR: Destination File %s Failed To Open...\n",dst_fn);
        return(-2);
    }

    int ch;
    /* Copy Source File Contents Into Destination File */
    while( (ch = fgetc(src_fh)) != EOF )
    {
        fputc(ch, dst_fh);
    }

    /* Close Files On Success */
    fclose(src_fh);
    fclose(dst_fh);
    return 0;
}

Другие советы

In your code dst_file is a char [20] which you use for fopen, obtaining a FILE * which you store in dst_p.

Instead of fputc(c, dst_file) try fputc(c, dst_p).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top