Copying one text file to another

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gactimus

    Copying one text file to another

    I wrote a C++ program that is supposed to copy one text file to another.
    It works fine if I specify the files to be opened and copied but when I
    try to have the file names inputted by the user it doesn't work and I am
    stumped. Can anyone clue me in to what I am missing?

    Here is my code:


    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    using namespace std;

    int main()
    {
    string source;
    string clone;

    cout << "Enter the source file: ";
    cin >> source;
    cout << "Enter the name of the new file: ";
    cin >> clone;

    ifstream in(source.c_str ); // Open for reading
    ofstream out(clone.c_str ); // Open for writing
    string s;
    while(getline(i n, s))
    out << s << "\n";

    return 0;
    }
  • ES Kim

    #2
    Re: Copying one text file to another

    "Gactimus" <gactimus@xrs.n et> wrote in message
    news:1100651820 .dZSrLYW+T41iUY lgGChJng@bubban ews...[color=blue]
    > I wrote a C++ program that is supposed to copy one text file to another.
    > It works fine if I specify the files to be opened and copied but when I
    > try to have the file names inputted by the user it doesn't work and I am
    > stumped. Can anyone clue me in to what I am missing?
    >
    > Here is my code:
    >
    > ifstream in(source.c_str ); // Open for reading
    > ofstream out(clone.c_str ); // Open for writing[/color]

    ifstream in(source.c_str ());
    ofstream out(clone.c_str ());

    --
    ES Kim


    Comment

    • Gactimus

      #3
      Re: Copying one text file to another

      "ES Kim" <no@spam.mail > wrote in news:cne9gj$g83 $1@news1.kornet .net:
      [color=blue]
      > "Gactimus" <gactimus@xrs.n et> wrote in message
      > news:1100651820 .dZSrLYW+T41iUY lgGChJng@bubban ews...
      >[color=green]
      >> I wrote a C++ program that is supposed to copy one text file to
      >> another. It works fine if I specify the files to be opened and copied
      >> but when I try to have the file names inputted by the user it doesn't
      >> work and I am stumped. Can anyone clue me in to what I am missing?
      >>
      >> Here is my code:
      >>
      >> ifstream in(source.c_str ); // Open for reading
      >> ofstream out(clone.c_str ); // Open for writing[/color]
      >
      > ifstream in(source.c_str ());
      > ofstream out(clone.c_str ());[/color]

      D'oh! It's always the stupid mistakes that get you. Thanks.

      Comment

      • Someonekicked

        #4
        Re: Copying one text file to another

        a faster way would be reading all the file at once ( not looping throughout
        lines), maybe it would not be a remarquable difference, anyway you can use
        getline(in,s,ch ar(0));
        char(0) is the null character, so the delimiter is the null character ( when
        reading no more characters).


        "Gactimus" <gactimus@xrs.n et> wrote in message
        news:1100651820 .dZSrLYW+T41iUY lgGChJng@bubban ews...[color=blue]
        >I wrote a C++ program that is supposed to copy one text file to another.
        > It works fine if I specify the files to be opened and copied but when I
        > try to have the file names inputted by the user it doesn't work and I am
        > stumped. Can anyone clue me in to what I am missing?
        >
        > Here is my code:
        >
        >
        > #include <iostream>
        > #include <fstream>
        > #include <cstdlib>
        > #include <string>
        > using namespace std;
        >
        > int main()
        > {
        > string source;
        > string clone;
        >
        > cout << "Enter the source file: ";
        > cin >> source;
        > cout << "Enter the name of the new file: ";
        > cin >> clone;
        >
        > ifstream in(source.c_str ); // Open for reading
        > ofstream out(clone.c_str ); // Open for writing
        > string s;
        > while(getline(i n, s))
        > out << s << "\n";
        >
        > return 0;
        > }[/color]


        Comment

        • Gactimus

          #5
          Re: Copying one text file to another

          "Someonekic ked" <someonekicked@ comcast.net> wrote in
          news:JeKdnd7Tnb XcKgfcRVn-uA@comcast.com:
          [color=blue]
          > "Gactimus" <gactimus@xrs.n et> wrote in message
          > news:1100651820 .dZSrLYW+T41iUY lgGChJng@bubban ews...
          >[color=green]
          >> I wrote a C++ program that is supposed to copy one text file to
          >> another. It works fine if I specify the files to be opened and copied
          >> but when I try to have the file names inputted by the user it doesn't
          >> work and I am stumped. Can anyone clue me in to what I am missing?
          >>
          >> Here is my code:
          >>
          >>
          >> #include <iostream>
          >> #include <fstream>
          >> #include <cstdlib>
          >> #include <string>
          >> using namespace std;
          >>
          >> int main()
          >> {
          >> string source;
          >> string clone;
          >>
          >> cout << "Enter the source file: ";
          >> cin >> source;
          >> cout << "Enter the name of the new file: ";
          >> cin >> clone;
          >>
          >> ifstream in(source.c_str ); // Open for reading
          >> ofstream out(clone.c_str ); // Open for writing
          >> string s;
          >> while(getline(i n, s))
          >> out << s << "\n";
          >>
          >> return 0;
          >> }[/color]
          >
          > a faster way would be reading all the file at once ( not looping
          > throughout lines), maybe it would not be a remarquable difference,
          > anyway you can use getline(in,s,ch ar(0));
          > char(0) is the null character, so the delimiter is the null character (
          > when reading no more characters).[/color]

          Thanks for the info. On a side note, do you know how I would go about
          encrypting the file that is copied? I want to do it very simply, as in
          adding 1 to every ASCII character.

          Comment

          • Ney André de Mello Zunino

            #6
            Re: Copying one text file to another

            Gactimus wrote:
            [color=blue]
            > Thanks for the info. On a side note, do you know how I would go about
            > encrypting the file that is copied? I want to do it very simply, as in
            > adding 1 to every ASCII character.[/color]

            Then read it character by character. Something like (untested):

            char c;
            while (source.get(c))
            clone.put(c);

            Hope that helps,

            --
            Ney André de Mello Zunino

            Comment

            • Ney André de Mello Zunino

              #7
              Re: Copying one text file to another

              Ney André de Mello Zunino wrote:
              [color=blue]
              > Then read it character by character. Something like (untested):
              >
              > char c;
              > while (source.get(c))
              > clone.put(c);[/color]

              Of course, you can add '1' or do whatever you like with the character
              before you write it to the output file stream.

              --
              Ney André de Mello Zunino

              Comment

              • Gactimus

                #8
                Re: Copying one text file to another

                Ney André de Mello Zunino <zunino@inf.ufs c.br> wrote in
                news:2vvsu9F2pn plmU2@uni-berlin.de:
                [color=blue]
                > Ney André de Mello Zunino wrote:
                >[color=green]
                >> Then read it character by character. Something like (untested):
                >>
                >> char c;
                >> while (source.get(c))
                >> clone.put(c);[/color]
                >
                > Of course, you can add '1' or do whatever you like with the character
                > before you write it to the output file stream.[/color]

                Thanks. I'll try it out.

                Comment

                • Gactimus

                  #9
                  Re: Copying one text file to another

                  Ney André de Mello Zunino <zunino@inf.ufs c.br> wrote in news:2vvsu9F2pn plmU2@uni-berlin.de:
                  [color=blue]
                  > Ney André de Mello Zunino wrote:
                  >[color=green]
                  >> Then read it character by character. Something like (untested):
                  >>
                  >> char c;
                  >> while (source.get(c))
                  >> clone.put(c);[/color]
                  >
                  > Of course, you can add '1' or do whatever you like with the character
                  > before you write it to the output file stream.[/color]

                  Ney André de Mello Zunino <zunino@inf.ufs c.br> wrote in news:2vvsu9F2pn plmU2@uni-berlin.de:
                  [color=blue]
                  > Ney André de Mello Zunino wrote:
                  >[color=green]
                  >> Then read it character by character. Something like (untested):
                  >>
                  >> char c;
                  >> while (source.get(c))
                  >> clone.put(c);[/color]
                  >
                  > Of course, you can add '1' or do whatever you like with the character
                  > before you write it to the output file stream.[/color]

                  It put the code in the program but I get an error:

                  error C2039: 'get' : is not a member of 'basic_string<c har,struct std::char_trait s<char>,class std::allocator< char> >'

                  Any ideas? Maybe I just don't know how to implement your code.

                  Comment

                  • John Harrison

                    #10
                    Re: Copying one text file to another


                    "Gactimus" <gactimus@xrs.n et> wrote in message
                    news:7Fzmd.34$O c.1@tornado.tam pabay.rr.com...[color=blue]
                    > Ney André de Mello Zunino <zunino@inf.ufs c.br> wrote in
                    > news:2vvsu9F2pn plmU2@uni-berlin.de:
                    >[color=green]
                    >> Ney André de Mello Zunino wrote:
                    >>[color=darkred]
                    >>> Then read it character by character. Something like (untested):
                    >>>
                    >>> char c;
                    >>> while (source.get(c))
                    >>> clone.put(c);[/color]
                    >>
                    >> Of course, you can add '1' or do whatever you like with the character
                    >> before you write it to the output file stream.[/color]
                    >
                    > Ney André de Mello Zunino <zunino@inf.ufs c.br> wrote in
                    > news:2vvsu9F2pn plmU2@uni-berlin.de:
                    >[color=green]
                    >> Ney André de Mello Zunino wrote:
                    >>[color=darkred]
                    >>> Then read it character by character. Something like (untested):
                    >>>
                    >>> char c;
                    >>> while (source.get(c))
                    >>> clone.put(c);[/color]
                    >>
                    >> Of course, you can add '1' or do whatever you like with the character
                    >> before you write it to the output file stream.[/color]
                    >
                    > It put the code in the program but I get an error:
                    >
                    > error C2039: 'get' : is not a member of 'basic_string<c har,struct
                    > std::char_trait s<char>,class std::allocator< char> >'
                    >
                    > Any ideas? Maybe I just don't know how to implement your code.[/color]

                    source and clone are not strings, they are the file streams. What you called
                    in and out in your original code.

                    Incidentally I would not use the method that Someonekicked recommended, it
                    does not work if your file contains a null byte. Nor would I use the method
                    you had originally because that adds an extra newline when the original file
                    does not end in a newline. Reading and writing one character at a time as
                    Andre recommended is the simplest.

                    john


                    Comment

                    • Karl Heinz Buchegger

                      #11
                      Re: Copying one text file to another

                      Gactimus wrote:[color=blue]
                      >[color=green]
                      > >[color=darkred]
                      > >> Then read it character by character. Something like (untested):
                      > >>
                      > >> char c;
                      > >> while (source.get(c))
                      > >> clone.put(c);[/color]
                      > >
                      > > Of course, you can add '1' or do whatever you like with the character
                      > > before you write it to the output file stream.[/color]
                      >
                      > It put the code in the program but I get an error:
                      >
                      > error C2039: 'get' : is not a member of 'basic_string<c har,struct std::char_trait s<char>,class std::allocator< char> >'
                      >
                      > Any ideas? Maybe I just don't know how to implement your code.[/color]


                      Look at the error message. What does it talk about?

                      It talks about basic_string. Hmm. basic_string. That has something
                      to do with string! It says that 'get' is not a member of some string
                      class. But why string? get is something you want to apply to a stream
                      not a string. So chances are high that you used the wrong variable and
                      applied get to it. You want to get from the stream and not the string.

                      I know. Those error messages are cryptic and sometimes even
                      misleading. But you definitly *must* learn to read them and
                      draw your conclusions from them.


                      --
                      Karl Heinz Buchegger
                      kbuchegg@gascad .at

                      Comment

                      • Francis Glassborow

                        #12
                        Re: Copying one text file to another

                        In article <2vvsraF2pnplmU 1@uni-berlin.de>, Ney André de Mello Zunino
                        <zunino@inf.ufs c.br> writes[color=blue]
                        >Gactimus wrote:
                        >[color=green]
                        >> Thanks for the info. On a side note, do you know how I would go about
                        >> encrypting the file that is copied? I want to do it very simply, as in
                        >> adding 1 to every ASCII character.[/color]
                        >
                        >Then read it character by character. Something like (untested):
                        >
                        >char c;
                        >while (source.get(c))
                        > clone.put(c);
                        >
                        >Hope that helps,[/color]

                        Actually there is a subtle problem waiting to bite if you open the files
                        in text mode; sometimes the encryption mechanism will generate a control
                        character which will cause problems with reading the encrypted file. It
                        is, therefore, much safer when encrypting/decrypting a file to open the
                        file in binary mode.


                        --
                        Francis Glassborow ACCU
                        Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
                        For project ideas and contributions: http://www.spellen.org/youcandoit/projects

                        Comment

                        • Chris \( Val \)

                          #13
                          Re: Copying one text file to another


                          "Gactimus" <gactimus@xrs.n et> wrote in message
                          news:7Fzmd.34$O c.1@tornado.tam pabay.rr.com...
                          | Ney André de Mello Zunino <zunino@inf.ufs c.br> wrote in
                          news:2vvsu9F2pn plmU2@uni-berlin.de:
                          |
                          | > Ney André de Mello Zunino wrote:
                          | >
                          | >> Then read it character by character. Something like (untested):
                          | >>
                          | >> char c;
                          | >> while (source.get(c))
                          | >> clone.put(c);
                          | >
                          | > Of course, you can add '1' or do whatever you like with the character
                          | > before you write it to the output file stream.
                          |
                          | Ney André de Mello Zunino <zunino@inf.ufs c.br> wrote in
                          news:2vvsu9F2pn plmU2@uni-berlin.de:
                          |
                          | > Ney André de Mello Zunino wrote:
                          | >
                          | >> Then read it character by character. Something like (untested):
                          | >>
                          | >> char c;
                          | >> while (source.get(c))
                          | >> clone.put(c);
                          | >
                          | > Of course, you can add '1' or do whatever you like with the character
                          | > before you write it to the output file stream.
                          |
                          | It put the code in the program but I get an error:
                          |
                          | error C2039: 'get' : is not a member of 'basic_string<c har,struct
                          std::char_trait s<char>,class std::allocator< char> >'
                          |
                          | Any ideas? Maybe I just don't know how to implement your code.

                          One method I can think of to copy a file is to open
                          the file in binary mode, and use the 'rdbuf()' member:

                          out << in.rdbuf();

                          Cheers.
                          Chris Val


                          Comment

                          Working...