Thread: program that reads text from a file and encodes the file by.....

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    program that reads text from a file and encodes the file by.....

    I just dont understand this file thing at all. Can someone tell me what I am doing wrong? I am no good at this.


    1.Read the text file one line at a time into a string.
    2.Change each character of the string by adding 5 to it.
    3.Write each encoded string to a second file, such as plain.code
    4.The encodeed file should have the same structure as the original file, if the second line of the original file has 24 characters (including spaces) then there should be 24 characters on the second line of the encoded file.

    insert
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	ifstream dataFile;
    	cout << "Opening File....\n";
    	dataFile.open("plain.txt",ios::out);
    	cout << "Now writing data to file.\n";
    
        string s;
    
        cout << "Enter a string> ";
        if (!getline(cin, s)) {
            cerr << "Input failure" << endl;
            return 1; 
        }
        for (string::size_type i = 0; i < s.length(); 5*i++)
            ++s[i];
        cout << s <<endl;
    
    	system("PAUSE");
        return 0;
    
    	
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (string::size_type i = 0; i < s.length(); 5*i++)
    This changes every 5th character (or maybe not, it isn't an assignment, so perhaps the 5* has no effect at all).

    > ++s[i];
    This adds 1 to that character (you wanted 5?)
    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
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salem View Post
    > for (string::size_type i = 0; i < s.length(); 5*i++)
    This changes every 5th character (or maybe not, it isn't an assignment, so perhaps the 5* has no effect at all).
    No "perhaps" about it. The 5* has no effect at all.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Code:
        //assumes you have also opened an output file [encpFile]
        while(std::getline(dataFile, s, '\n')) //retrieve full line from input file 
         {
            for(int x = 0; x<s.length(); x++)
             {
             s[x]+=5;
            encpFile<<s[x]; //encrypted character out
             }
        
        encpFile<<"\n"; //line has ended, start new line
         }

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    idk

    I thought that is how you make it add 5 to each value. i guess i just give up. I just dont get it.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    From your instructions, you want to add the value '5' to each character within your string. To decrypt, you'd simply do a -=5 in lieu of the +=5. You're encoding each character within the string, at least that's how I'm reading the problem. As example, a file such as;

    I like apples.
    I like pears.
    I don't like carrots.

    would encrypt to;

    N%qnpj%fuuqjx3
    N%qnpj%ujfwx3
    N%its,y%qnpj%hfwwtyx3



    Code:
    int main()
    {
    	ifstream dataFile;
    	ofstream encpFile;
    	std::cout << "Opening File....\n";
    	dataFile.open("plain.txt",ios::in);
    
        std::string s;
        encpFile.open("encrypt.txt", ios::out);
        
        while(std::getline(dataFile, s, '\n'))
         {
            for(int x = 0; x<s.length(); x++)
             {
             s[x]+=5;
            encpFile<<s[x];
             }
        
        encpFile<<"\n";
         }
          
        std::cout<< "we're done.";
        std::cin.get();
        
        dataFile.close();
        encpFile.close();
        
        return 0;
         }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. File I/O Encode & Decode with XOR
    By DarrenY in forum C# Programming
    Replies: 2
    Last Post: 04-12-2007, 04:31 AM
  3. File IO / help please
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2002, 04:10 AM