file I/O problem

This is a discussion on file I/O problem within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm helping my friend with this program. Two problems: 1. Is there an escape character in C++ to ignore ...

  1. #1
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26

    file I/O problem

    Hey, I'm helping my friend with this program. Two problems:
    1. Is there an escape character in C++ to ignore a \ in a path?
    for example in Perl I would just use another \ as in C:\\Progra~1\
    2. I need to copy all contents from one file and output to another, but the .get and .put aren't working. Is there a better way to do this or does the syntax need to be corrected?

    Code:
    #include <iostream>   //i/o library
    #include <string>     // string library
    #include <fstream>       // both input/output files
    #include <cstdlib>
    
    
    using namespace std;
    
    int main()  // starting main function
    {
        ifstream myinputfile; // declaring input file
        ofstream myoutfile;  // declaring output file
        cout << "Begin file Copy\n"; // print to screen
        
        myinputfile.open ("C:|myinputfile.txt"); //opening input file
        
        if (!myinputfile)  // testing success
        {
          cout << "Error!!\n";  // if unsuccessful
          }
        
        myoutfile.open ("C:|myoutfile.txt"); //opening output file
        
        if (!myoutfile) //testing success
        {
          cout << "Error!!\n"; // if unsucessful
          exit (100);
                        
        }
        
        int line;  // declaring a variable
        
        while (myinputfile.get (line)) {
        myoutfile.put (line);
    } 
        
        myinputfile.close (); // closing input file
        myoutfile.close (); // closing output file
        cout << "End file copy\n"; // print to screen
        exit (102);
        
        return 0; // end of main function
    }
    Thanks for your help
    Last edited by cDev; 04-10-2007 at 09:04 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It's
    Code:
    \\

  3. #3
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26
    Oh it is...thanks ::embarrassed::

    So, what's the best way to parse the input file and output each line to the output file?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    getline()

  5. #5
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26
    Ok, cool thnx

  6. #6
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Your variable 'line' is misnamed, but your old code is a better way to copy a text file isn't it

  7. #7
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26
    My old code didn't compile...

    I wasn't familiar with get and put, so I asked what's the best way to copy the text file...
    With getline() I have gotten farther:

    Code:
    int MAX_LENGTH = 100;
      char line[MAX_LENGTH];
        
        while ( myinputfile.getline(line, MAX_LENGTH )) {
              cout << "read line: " << line << endl;
       myoutfile.put(line, MAX_LENGTH);
    }
    but this line:
    Code:
    myoutfile.put(line, MAX_LENGTH);
    doesn't work. I can now get the text from the one file, but what is the best syntax to throw it in the other?

  8. #8
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    char c; while(myinputfile.get(c)) myoutfile.put(c);

  9. #9
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26
    Perfect ::embarrassed again::

    That's what I had before, but I forgot to change the variable to parse for from int to char... (no wonder)

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 08:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 03:32 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21