locked
"Decompile" a file in a string and change the string to read it RRS feed

  • Question

  • Hi,

    I finally found how to edit and read text files (I still can't save them -_-').Like usually, I would "compilate" into another file format like ".txtt" it to make it hard to read, when you open it from another application than mine. If I would like to "compile" it, of course I would like to "decompile". Here is an example:

    A text before it is compilated:

    I like to ski.

    A text after it is compilated:

    &I& &l&i&k&e& &t&o& &s&k&i&.&

    The problem is I don't know how to add & for every symbols written or to remove & for every symbols writte. How do I do this? Here is my code:
    // reading a text file
    
    #include <iostream>
    
    #include <fstream> //read
    
    #include <ostream> //write
    
    #include <string>
    
    using namespace std;
    
    
    
    int main () {
    
    	int none;
    
      string line;
    
      
    
      ifstream myfile ("C:\\Users\\andre\\Documents\\example.dat");
    
      if (myfile.is_open())
    
      {
    
        while (! myfile.eof() )
    
        {
    
          getline (myfile,line);
    
          cout << line << endl;
    
        }
    
        myfile.close();
    
      }
    
    
    
      else cout << "Unable to open file"; 
    
      cin >> none;
    
    
    
      return 0;
    
    }
    
    

    Wednesday, January 27, 2010 2:19 AM

Answers

  • StartQuote>
    if (myfile.is_open())
      {
       while (! myfile.eof() )
        {
        getline (myfile,line);
        cout << line << endl;
        }
       myfile.close();
      }
    <EndQuote
     
    That code looks familiar. It has the same bug in it that's
    in the example at the cplusplus tutorial site. I reported
    it long ago to the providers of that site via their contact
    link, but I guess they're too proud to admit their mistake
    and fix the bug.

    That's the *wrong* way to test for the end of file when
    reading. In C and C++ the eof indicator gets set the first
    time you try to read *past* the end of data in the file.
    It does *not* get set at the time you read the final line
    from the file. It will get set the first time the getline
    *fails*. As written, that code will do a cout even when
    the getline fails and returns no data.

    You should do this:

    while(getline(myfile,line))
      {
      cout << line << endl;
      }
    if(!myfile.eof())
      {
      cout << "Error reading the file!\n"; // didn't reach eof
      return -1;
      }
    myfile.close();


    - Wayne
    • Marked as answer by Wesley Yao Tuesday, February 2, 2010 3:29 AM
    Wednesday, January 27, 2010 5:38 AM
  • It is easy isn't it? The std::string type allows you to append a single character to the end of a string or read a string one character at a time.
    So you would use that to your advantage.
    But I doubt anyone would give you a full solution. This looks too much like a learners program that you would get assigned in education. So try to work it out yourself and come back with a solution then people will give you hints on possible problems. Even if you are not in education but in fact are self learning, it would be a good idea to figure this out yourself since it gives you problem solving skills, and working out all kinds of problems is frequent in programming.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    • Marked as answer by Wesley Yao Tuesday, February 2, 2010 3:29 AM
    Wednesday, January 27, 2010 6:21 AM

All replies

  • StartQuote>
    if (myfile.is_open())
      {
       while (! myfile.eof() )
        {
        getline (myfile,line);
        cout << line << endl;
        }
       myfile.close();
      }
    <EndQuote
     
    That code looks familiar. It has the same bug in it that's
    in the example at the cplusplus tutorial site. I reported
    it long ago to the providers of that site via their contact
    link, but I guess they're too proud to admit their mistake
    and fix the bug.

    That's the *wrong* way to test for the end of file when
    reading. In C and C++ the eof indicator gets set the first
    time you try to read *past* the end of data in the file.
    It does *not* get set at the time you read the final line
    from the file. It will get set the first time the getline
    *fails*. As written, that code will do a cout even when
    the getline fails and returns no data.

    You should do this:

    while(getline(myfile,line))
      {
      cout << line << endl;
      }
    if(!myfile.eof())
      {
      cout << "Error reading the file!\n"; // didn't reach eof
      return -1;
      }
    myfile.close();


    - Wayne
    • Marked as answer by Wesley Yao Tuesday, February 2, 2010 3:29 AM
    Wednesday, January 27, 2010 5:38 AM
  • It is easy isn't it? The std::string type allows you to append a single character to the end of a string or read a string one character at a time.
    So you would use that to your advantage.
    But I doubt anyone would give you a full solution. This looks too much like a learners program that you would get assigned in education. So try to work it out yourself and come back with a solution then people will give you hints on possible problems. Even if you are not in education but in fact are self learning, it would be a good idea to figure this out yourself since it gives you problem solving skills, and working out all kinds of problems is frequent in programming.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    • Marked as answer by Wesley Yao Tuesday, February 2, 2010 3:29 AM
    Wednesday, January 27, 2010 6:21 AM