Thread: copying a file

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    copying a file

    I want to read the filename that is data in one file and open that filename specified as data.

    say, file1="filetoread";
    file2="filetowriteto";

    I want to read the data from file1 (say line 1 of file1 is "c:\myfile.txt"), then I want to open the file listed on line 1 of file1 and copy the data from c:\myfile.txt to file2.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try a board search
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Post the code you attempted to do this with.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    This is what I have so far:
    It works up to: " if ((fnewsource = fopen(sourcefile,"r"))==NULL)"...it's not able to open the file.

    see the attached file...

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    fgets(fileName,256,fold);
    sourcefile=fileName;
    /* ... */
    if ( (fnewsource = fopen(sourcefile,"r"))==NULL )
    If you don't pick off the '\n' that fgets may leave in, it's still there. So if you are intending to open "file.txt", you may be trying to open "file.txt\n". More info.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    Thanks. I will try that and get back to you.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    fgets() retrieves and stores the newline in the buffer - you have to strip it out:

    Code:
    char * get(char * buffer, unsigned max, FILE * stream)
    {
     char * crlf = 0, * line = fgets(buffer, max, stream);
     if(line) crlf = strstr(line, "\n");
     if(crlf) *crlf = 0;
     return line; 
    }

    Also, look at this:

    >> char *scenarioNum;
    >> gets(scenarioNum);

    'scenarioNum' doesn't point to any memory!

    Don't use gets() either, since it doesn't provide buffer-overflow protection.

    Finally, look at the code that copies a file - wouldn't that make a great 'reusable' function? [hint hint]
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    Thank you all. Your suggestions worked! I needed to strip the \n off of the end of the string (thanks for the code for that too).

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Sebastiani fgets() retrieves and stores the newline in the buffer - you have to strip it out:

    Code:
    char * get(char * buffer, unsigned max, FILE * stream)
    {
     char * crlf = 0, * line = fgets(buffer, max, stream);
     if(line) crlf = strstr(line, "\n");
     if(crlf) *crlf = 0;
     return line; 
    }
    How about:
    Code:
    char * get(char * buffer, unsigned max, FILE * stream)
    {
        int i;
        line = fgets(buffer, max, stream);
        i = strlen(line) - 1;
        if(line[i] == '\n') line[i] = '\0';
        return line; 
    }
    This saves the character by character search the strstr() uses. And if there is a return, it must be at the end of the string.

    But then when you think about it, what does strlen() do? Hmmm....

    But I have only one if. That must count for sumtin'
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The extra 'if' is necessary because fgets() might return NULL, in which case you wouldn't want to pass the pointer to a string-handling function anyway.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Sebastiani
    The extra 'if' is necessary because fgets() might return NULL, in which case you wouldn't want to pass the pointer to a string-handling function anyway.
    Good point...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM