Thread: Problem with file I/O

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Problem with file I/O

    Im working on this little text encrypter/decrypter in C++, but im having some trouble with the encrypting part.

    Here is what i have so far:

    Encrypter:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        ofstream algorithm1("Algorithm1.txt", ios::app);
        
        char message[40];
        int index = 0;
        
        if(!algorithm1)
    	{
    		cout <<"Couldn't Open File" << endl;
    		cin.get();
    		exit(1);
    	}
        
        cout << "Please enter a word with no whitespaces: ";
        cin >> message;
        
        for( index = 0 ; index < 40 ; index++)
        {
                  algorithm1 << (int)message[index] << " ";
        }
    	
    	algorithm1 << endl;
           algorithm1.close();
    }
    So as you can see, the progam accepts input, casts it to ASCII numeric values, so that it is not easily readable, and outputs it to a text file. No security revolution, im aware of that, but that's irelevant

    I'm also aware that C-Style arrays are outdated, but im just trying to get this to work, will probably use real strings once i figure out how i do.

    The code compiles and runs beatifully, but the output in the text file is way off.

    For example, if i entered "C++" in the Encrypter. The text file contains:

    "67 43 43 0 -112 22 -11 119 -128 55 61 0 78 22 -11 119 -24 6 61 0 106 22 -11 119 0 0 0 0 -120 55 61 0 104 35 36 0 72 -1 34 0 "

    The first 3 numbers are actually correct, and will display "C++" when decrypted, but the rest is utter nonsense?

    Im guessing this is because im printing a mostly empty array, am i right? I'm not really into Vectors at all, so if i HAVE to use vectors in my program, could you please show me a complete example of how i could do it. Although i would prefer if there was some other way to fix it.

    Thanks in advance
    Neo

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
        for( index = 0 ; index < 40 ; index++)
        {
                  algorithm1 << (int)message[index] << " ";
        }
    I think you got the point that you're printing out a mostly empty array. Just limit it to length of the string.

    Code:
        for( int i = 0, e = strlen(message); i < e ; index++ )
        {
                  algorithm1 << (int)message[i] << " ";
        }

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    That did work fine actually, i get the correct output from the program now, thanks Tonto.

    Why is it that the array is full of all these random values, when it's empty?

    Now i'm facing another problem though, the decrypter isn't doing what it's supposed to either.

    Decrypter:
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        ifstream encrypted("Algorithm1.txt");
        ofstream decrypted("Algo1 - Decrypted.txt");
        
        int index = 0;
        int length = 0;
        int message[40];
        
        length = 40;
        
        for(index = 0 ; index<length ; index++)
        {
                  encrypted >> message[index];
        }
        
        for(index = 0 ; index<length ; index++)
        {
                  decrypted << (char)message[index];
        }
        encrypted.close();
        decrypted.close();
    }
    As you can see, i declared length to be 40, and the output is totally off like before, clearly the same problem. But how would i go about finding out how long length is actually supposed to be? I can't do it like before because:

    strlen(); doesn't work with int arrays.
    and i can't print the file to the array to measure it, because i dont know how long the file is.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, you can just read until you hit the end of the file or walk past the bounds of your array.

    Code:
        for(index = 0 ; index<length ; index++)
        {
                  if( !(encrypted >> message[index]) )
                  {
                                // file reading error, end of file
                  }
        }
    That syntax is a little funky, but what the operation (encrypted >> blah) returns the encrypted stream over again. This allows you to say stuff like encrypted >> blah >> blah >> blah; because the operation chains itself. The class ifstream implements an operator ! also, which returns the status of the stream.

  5. #5
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Tonto
    Well, you can just read until you hit the end of the file or walk past the bounds of your array.

    Code:
        for(index = 0 ; index<length ; index++)
        {
                  if( !(encrypted >> message[index]) )
                  {
                                // file reading error, end of file
                  }
        }
    That syntax is a little funky, but what the operation (encrypted >> blah) returns the encrypted stream over again. This allows you to say stuff like encrypted >> blah >> blah >> blah; because the operation chains itself. The class ifstream implements an operator ! also, which returns the status of the stream.
    Im not quite sure if i'm understanding this correctly.

    In that code above, the "!" and ">>" operators isn't functioning like normal? The (encrypted >> message[index]) returns "message[index]", and so does the "!" in the IF statement?

    How exactly would i go about implementing the above syntax in my program, sorry if it sounds like i want you to do the job for me, but i just don't get what you said in that post.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
        for(index = 0 ; index<length ; index++)
        {
                  if( !(encrypted >> message[index]) )
                  {
                                // file reading error, end of file
                                break;
                  }
        }
    The operation (encrypted >> message[index]) returns "encrypted" actually, and the ! checks the status of the "encrypted" stream.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    or you can write the length of the encripted string before you start writing it.
    then reading the length during decription you will know exactly what is the number of integers to be read
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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, 09:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04: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, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM