Member Avatar for ragnacrap

This is what my professor provided me in his own words.

"Write a program that copies the contents of a text file specified by the user and copies it to another text file "copy.txt". No line in the file is expected to be longer than 256 characters."

Here is part of the code I devised so far with his info:

int main ( void )
    {
       char filename[256]="";		//Storing File Path/Name of Image to Display
       static const char file2name[] = "C:\\Users\\Rael Jason\\Desktop\\Rael's iPod\\Codeblocks\\copy.txt";
       FILE *file; 
       FILE *write;
       printf("Please enter the full path of the text file you want to copy text: ");
       scanf("%s",&filename);
       file = fopen ( filename, "r" );
       file = fopen (file2name, "r" );
       
       if ( file != NULL )
       {
          char line [ 256 ]; /* or other suitable maximum line size */
          char linec [256]; // copy of line
    
          while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
          {
             
             fputs ( line, stdout ); /* write the line */
             strcpy(linec, line);
             
             
             
             fprintf (write , linec);
             fprintf (write , "\n");
          }
          fclose (write);
          fclose ( file );
       }
       else
       {
          perror ( filename ); /* why didn't the file open? */
       }
       return 0;
    }

I just can't seem to get the file writing done right? Can you please help?

You're opening the same file(handle) for reading and writing.

file = fopen (file2name, "r" );

it should be

write=fopen(file2name,"w")//"w" creates the file 'copy.txt' if it doesn't exist already
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.