The "r" function works well!

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

int main(void){
FILE *file_r, *file_w;
int c;
char fileread[40];
char filewrite[40];

printf("Enter filename to be copied: ");
gets(fileread, 40, stdin);
fileread[strlen(fileread)-1] = '\0';
file_r = fopen(fileread, "r");
while(file_r == NULL)
    {
    printf("Invalid file, ealnter again!");
    fgets(fileread, 40, stdin);
    fileread[strlen(fileread)-1] = '\0';
    printf("%s\n", fileread);
    file_r = fopen(fileread, "r");
    }

    printf("Enter name of file copy");
    fgets(filewrite, 40, stdin);
    fileread[strlen(fileread)-1] = '\0';
    file_w = fopen(filewrite, "w");


while(file_w == NULL)
    {
    printf("Invalid Filename enter again");
    fgets(filewrite, 40, stdin);
    fileread[strlen(fileread)-1] = '\0';
    file_r = fopen(fileread, "w");

    }


    c = getc(file_r);
    while(c != EOF)
        {
        putc( c, file_w);
        c = getc(file_r);
        }
    fclose(file_r);
    fclose(file_w);
    printf("Files succesfully copied");
    return 0; 
}

Dani AI

Generated

Primary cause: a copy/paste mix-up of variable names. As already spotted, the code repeatedly manipulates fileread instead of filewrite, so the string passed to fopen for the output is wrong. ’s reply fixes most occurrences but still contains a stray reopen that assigns into file_r inside the write-validation loop; that line must assign to file_w. Because the program never successfully assigns a valid FILE* to file_w, the write-mode open appears to "not create a file."

Other important fixes and hardening (concise, practical guidance):

  • Never use gets. Use fgets and remove the trailing newline safely. Example:

    char name[40];
    if (fgets(name, sizeof name, stdin) == NULL) { /* handle EOF */ }
    name[strcspn(name, "\n")] = '\0';   /* removes newline when present */
  • Always check fopen results and report the error. If fopen(...) returns NULL, call perror("fopen") (or inspect errno) to see whether the cause is permission denied, a missing directory, an invalid filename, etc.

  • If copying non-text files, open in binary mode ("rb" / "wb") to avoid platform newline translation.

  • Handle long input lines: when fgets fills the buffer without a newline, discard the rest of the line from stdin before the next prompt.

Quick checklist to resolve the symptom seen in this thread:

  • Replace any remaining uses of fileread where filewrite should be used.
  • In the write-validation loop, assign the reopened file to file_w (not file_r).
  • Use fgets(..., sizeof buf, stdin) and trim with strcspn.
  • Check fopen return value and call perror for diagnostics.
  • Consider "rb"/"wb" for binary-safe copying and ensure the target directory and permissions allow file creation.

Recommended Answers

All 2 Replies

Repeated fileread[strlen(fileread)-1] = '\0'; instead of filewrite..ooops

You've cut and pasted parts of the code but neglected to modify everything. For example, in the file_w part of the code you're still working with fileread rather than filewrite. Compare and contrast with this corrected code:

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

int main(void)
{
    FILE *file_r, *file_w;
    int c;
    char fileread[40];
    char filewrite[40];

    printf("Enter filename to be copied: ");
    fgets(fileread, 40, stdin);
    fileread[strlen(fileread)-1] = '\0';
    file_r = fopen(fileread, "r");

    while(file_r == NULL)
    {
        printf("Invalid file, ealnter again!");
        fgets(fileread, 40, stdin);
        fileread[strlen(fileread)-1] = '\0';
        printf("%s\n", fileread);
        file_r = fopen(fileread, "r");
    }

    printf("Enter name of file copy");
    fgets(filewrite, 40, stdin);
    filewrite[strlen(filewrite)-1] = '\0';
    file_w = fopen(filewrite, "w");

    while(file_w == NULL)
    {
        printf("Invalid Filename enter again");
        fgets(filewrite, 40, stdin);
        filewrite[strlen(filewrite)-1] = '\0';
        file_r = fopen(filewrite, "w");
    }

    c = getc(file_r);

    while(c != EOF)
    {
        putc( c, file_w);
        c = getc(file_r);
    }

    fclose(file_r);
    fclose(file_w);
    printf("Files succesfully copied");

    return 0; 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.