Thread: reading and writing to a file

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    62

    reading and writing to a file

    I gotta be close on this. The new file is not created and I can't figure it out.

    Here's what I have.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MaxNameLen 255
    #define MaxLineLen 255
    
    int main(int argc, char** argv)
    {
        char fileName[MaxNameLen + 1];
        char copyName[MaxNameLen + 1];
        FILE *myfile;
        FILE *newfile;
    
        printf( "Enter the name of a file to copy from: " );
        fgets( fileName, sizeof( fileName ), stdin );
    
        if( fileName[strlen( fileName ) - 1] == '\n' )
        {
            fileName[strlen( fileName ) - 1] = '\0';
        }
    
        myfile = fopen(fileName, "r");
        if (myfile == NULL)
        {
            printf("Cannot open &#37;s\n", fileName);
            return(1);
        }
    
        fscanf(myfile, "%s", &fileName);
    
        printf( "Enter the name of a file to copy   to: ");
        fgets( copyName, sizeof( fileName ), stdin );
        
         if( copyName[strlen( copyName ) - 1] == '\n' )
        {
            copyName[strlen( copyName ) - 1] = '\0';
        }
    
        newfile = fopen(copyName, "w");
        fprintf( newfile, "%s", myfile);
    
        fclose(myfile);
        fclose(newfile);
        return (0);
    }
    I wonder if I am actually copying anything.

    Any insight?
    Thanks
    Last edited by tikelele; 11-26-2007 at 09:14 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Why don't you apply the same "newline removal" code to the output file name and see what happens.

    Todd

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    fscanf(myfile, "&#37;s", &fileName);
    What is the purpose of this?

    Code:
    fgets( copyName, sizeof( fileName ), stdin );
    Although no issue because they are the same size, this is a bad practice. The second arg should be sizeof( copyName ).

    Also, you should check if newfile is NULL or not.

    Code:
    fprintf( newfile, "%s", myfile);
    This is pure garbage. You're writing a FILE * as if it's a char *. You're going to have issues. You have to actually fgets() or fgetc() the info from the first file and then fprintf(), or fputc() it all into the new file.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    great thank you. I did exactly that and the output file was created.

    It had JUNK in it, but I guess that's what the programmer (me) made it do.

    I know I am either not fscanf or fprintf correctly.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    i changed thing but I am getting a file that goes on forever.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MaxNameLen 255
    #define MaxLineLen 255
    
    int main(int argc, char** argv)
    {
        
        char fileName[MaxNameLen + 1];
        char copyName[MaxNameLen + 1];
        FILE *myfile;
        FILE *newfile;
        
    
        printf( "Enter the name of a file to copy from: " );
        fgets( fileName, sizeof( fileName ), stdin );
    
        if( fileName[strlen( fileName ) - 1] == '\n' )
        {
            fileName[strlen( fileName ) - 1] = '\0';
        }
    
        myfile = fopen(fileName, "r");
        if (myfile == NULL)
        {
            printf("Cannot open &#37;s\n", fileName);
            return(1);
        }
        printf( "Enter the name of a file to copy   to: ");
        fgets( copyName, sizeof( copyName ), stdin );
    
        if( copyName[strlen( copyName ) - 1] == '\n' )
        {
            copyName[strlen( copyName ) - 1] = '\0';
        }
    
        newfile = fopen(copyName, "w");
        if (newfile == NULL)
        {
            printf("Cannot create %s\n", copyName);
            return(1);
        }
    
        while (1)
        {
            fgets (fileName , sizeof( fileName ), myfile);
            if (fileName == EOF)
            {
                break;
            }
            else
            {
                fprintf( newfile, "%s", fileName);
            }
        }
    
        fclose(myfile);
        fclose(newfile);
        return (0);
    }
    Last edited by tikelele; 11-26-2007 at 10:17 PM.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    i beleive i have it.

    Code:
    while (!feof(myfile))
        {
            fgets (fileName , sizeof( fileName ), myfile);
            fprintf( newfile, "%s", fileName);        
        }
    thanks everyone

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    That's an incorrect way to do it. Don't use feof() to control a loop. Use fgets() as your loop condition.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    i did but i cant get it to stop at the end of the file

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tikelele View Post
    i did but i cant get it to stop at the end of the file
    The following doesn't work?

    Code:
    while (fgets (fileName , sizeof( fileName ), myfile))
        {
            fprintf( newfile, "%s", fileName);        
        }

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    yes it does. at first it didn't because i had a typo

    Thanks all!

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    @MacGyver (or anyone else who knows)

    I wanted to know what the consequesces of using feof() as the loop control. I changed it to the proper fgets() but I am curious because feof() worked. I am guessing that when it works great...but when it doesn't then its real bad.

    I am just wondering what kind of dynamite I was toying with
    Thanks for any reply

  12. #12

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    FAQs
    shudda known!

    Thank you
    MacGyver

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmetation fault (reading and writing file in c)
    By tasosa in forum C Programming
    Replies: 5
    Last Post: 04-13-2009, 06:04 AM
  2. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM