trying to read from a file

problem while trying to read from second file

Page 1 of 1

12 Replies - 795 Views - Last Post: 28 November 2008 - 10:17 PM Rate Topic: -----

#1 BujarM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-February 07

trying to read from a file

Post icon  Posted 28 November 2008 - 06:25 PM

SOLVED

This post has been edited by BujarM: 28 November 2008 - 07:29 PM

Is This A Good Question/Topic? 0
  • +

Replies To: trying to read from a file

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: trying to read from a file

Posted 28 November 2008 - 06:34 PM

Have you tried closing the first infile before opening the second?
Was This Post Helpful? 0
  • +
  • -

#3 BujarM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-February 07

Re: trying to read from a file

Posted 28 November 2008 - 06:43 PM

SOLVED

This post has been edited by BujarM: 28 November 2008 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: trying to read from a file

Posted 28 November 2008 - 06:44 PM

You want to close after the while loop not inside. i.e. right before you open your next stream.
Was This Post Helpful? 0
  • +
  • -

#5 BujarM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-February 07

Re: trying to read from a file

Posted 28 November 2008 - 06:47 PM

View PostKYA, on 28 Nov, 2008 - 05:44 PM, said:

You want to close after the while loop not inside. i.e. right before you open your next stream.


Did it, but still an infinite repetition of the TEST word
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: trying to read from a file

Posted 28 November 2008 - 06:50 PM

Updated code listing is always helpful.

 while (!infile1.eof() )     // while end of input file not reached
    {
        infile1.read(buffer, BUFSIZE );  // read in bugger
        count = infile1.gcount();        // count the rad characters
        ENCRYPT_ASM(buffer, count, CIPHER, CIPHER_LENGTH );  // call a function
        outfile1.write(buffer, count);   // write to outfile
        
}

//*******Did you put it right here?******************************

    ifstream infile2( "encrypted_asm.txt", ios::binary ); //open files in binary
    ofstream outfile3( "decripted_asm.txt", ios::binary );


Was This Post Helpful? 0
  • +
  • -

#7 BujarM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-February 07

Re: trying to read from a file

Posted 28 November 2008 - 06:53 PM

SOLVED

This post has been edited by BujarM: 28 November 2008 - 07:29 PM

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: trying to read from a file

Posted 28 November 2008 - 06:55 PM

Let me have the text files you are using so i can perform a more accurate test. And what is the cipher you are using?

This post has been edited by KYA: 28 November 2008 - 06:55 PM

Was This Post Helpful? 0
  • +
  • -

#9 BujarM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-February 07

Re: trying to read from a file

Posted 28 November 2008 - 06:59 PM

SOLVED

This post has been edited by BujarM: 28 November 2008 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

#10 BujarM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-February 07

Re: trying to read from a file

Posted 28 November 2008 - 07:10 PM

Problem Solved!!!

Im sorry i had mixed up the filenames

KYA thanks for your time :)
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: trying to read from a file

Posted 28 November 2008 - 07:13 PM

ok ok, just asking :)

I see exactly what you're talking about now [visually].

I haven't found the root cause yet, but as far as i can tell it successfully reads the input file and successfully sends the encrypted message to the output file. It gets stuck in an infinite loop during the decrypt part.


edit: ok, glad it worked out for you.

I should have looked at that first ><



I added this to avoid your really long char array for the buffer:

//need to get exact size of the input
	infile1.seekg(0, ios::end);
	int length = infile1.tellg();
	infile1.seekg(0, ios::beg);


    char* buffer = new char[length];       //buffer to store the characters from first file
    unsigned int count;            // character count


delete[] buffer; //end of source file


This post has been edited by KYA: 28 November 2008 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: trying to read from a file

Posted 28 November 2008 - 09:19 PM

View PostBujarM, on 28 Nov, 2008 - 05:25 PM, said:

SOLVED


I suspect you are trying to be helpful but it you delete your questions like this then people following you cannot learn from reading your question and the answers given.

If you leave your questions in place it provides a resource for those who follow you and is therefore helpful for everyone.
Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: trying to read from a file

Posted 28 November 2008 - 10:17 PM

/agreed

for educational purposes here is the corrected code listing [including some things I added that he may or may not have implemented]:

#include<iostream>
#include<string>
#include<fstream>

using namespace std;


extern "C" {
void ENCRYPT_ASM(char buffer[],int count, char CIPHER[],int CIPHER_LENGTH);
void DECRYPT_ASM(char buffer[],int count, char CIPHER[],int CIPHER_LENGTH);
}
int main()
{
    string cipher;      //Temporary string to store the cipher
	char CIPHER[50];                      // Put the string in an array
    cout<<"Enter the cipher:";
    getline(cin,cipher);

    cout<<"\nThe cipher is: "<<cipher<<endl;
    int CIPHER_LENGTH= cipher.length();                // get CIPHER_LENGTH
    cout<<"The length of the cipher is: "<<CIPHER_LENGTH<<endl;

	system ("pause");

    for(int i=0; i<CIPHER_LENGTH; i++)
    {
        CIPHER[i]=cipher.at(i);
    }



    ifstream infile1( "input.txt", ios::binary );    // Define/open an input and output file, in binary
    ofstream outfile1( "encrypted_asm.txt", ios::binary );

	//need to get exact size of the input
	infile1.seekg(0, ios::end);
	int length = infile1.tellg();
	infile1.seekg(0, ios::beg);


    char* buffer = new char[length];       //buffer to store the characters from first file
    unsigned int count;            // character count

    while (!infile1.eof() )     // while end of input file not reached
    {
        infile1.read(buffer, BUFSIZE );  // read in bugger
        count = infile1.gcount();        // count the rad characters
        ENCRYPT_ASM(buffer, count, CIPHER, CIPHER_LENGTH );  // call a function
        outfile1.write(buffer, count);   // write to outfile
            
	}
    infile1.close();
    outfile1.close();


                // Are these files opened at all?
    ifstream infile2( "encrypted_asm.txt", ios::binary ); //open files in binary
    ofstream outfile3( "decripted_asm.txt", ios::binary );
    
    // PROBLEM HERE!!! *******************************************
    while(!infile2.eof())           // while end of file2 not reached
    {
        infile2.read(buffer, BUFSIZE );   // read into buffer
        count = infile2.gcount();         // count the chars read
        DECRYPT_ASM(buffer, count, CIPHER, CIPHER_LENGTH );  // call function
        outfile3.write(buffer, count);    // write to outfile3
        
    }
        infile2.close();
        outfile3.close();

		delete[] buffer;

}

void ENCRYPT_ASM(char buffer[],int count,char CIPHER[],int CIPHER_LENGTH )
{
    int t=0;
    for( int i=0; i<count; i++)
    {
        buffer[i]=buffer[i] ^ CIPHER[t % CIPHER_LENGTH];
    t++;
    }
    
}
    

void DECRYPT_ASM(char buffer[],int count,char CIPHER[],int CIPHER_LENGTH )
{
    //cout<<"Hello";
    int t=0;
    for( int i=0; i<count; i++)
    {
        buffer[i]=buffer[i] ^ CIPHER[t % CIPHER_LENGTH];
    t++;
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1