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

This is a discussion on program that reads text from a file and encodes the file by..... within the C++ Programming forums, part of the General Programming Boards category; I just dont understand this file thing at all. Can someone tell me what I am doing wrong? I am ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,303
    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%.

  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, 03:10 AM

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