Problem with files - Urgent

kartouss's Avatar, Join Date: Feb 2008
Newbie Member
hello i am new to this forum.. so i need your help to solve my c++ program to run..
i am using files:-
in fact i am reading data from a file name p.txt 16 bytes @ a time and then saving the data in the file c.txt till eof p.txt..
so i have to read the 1st 16 bytes from p.txt and save it in c.txt and then the next 16 bytes are read and then rewritten in c.txt till eof..
i have written the codes but i am having this problem:-

p.txt: aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb
c.txt: aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙

so why ˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙ is copied to the file c.txt it shoule be same as that in the p.txt file
please help me how to solve this problem

the code is:-
Code:
#include<iostream.h>
#include<fstream.h>
void main()
{
int i,j;
char d;
char a[4][4];
ifstream myfile1 ("p.txt");
ofstream myfile3 ("c.txt");
	 	
if (myfile1.is_open())
{
	while(!myfile1.eof())
	{	
		for ( j=0; j<4; j++)
		for ( i=0; i < 4; i++)
		{	
			myfile1.get(d);
			a[i][j] = d; // plaintext				
			cout<<a[i][j];
		 }	

		if (myfile3.is_open())
		{
			for(j=0; j< 4; j ++)
			for ( i=0; i<4; i++)
			  myfile3<<a[i][j];	
			//cout<<a[i][j];
		}
		else 
			 cout << "Unable to open file";	
	}					
	
	myfile3.close();
}	 
else 
cout << "Unable to open file";
	myfile1.close();
}

Last edited by shabbir; 10Feb2008 at 09:42.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code
0
technosavvy's Avatar
Contributor
try this one...just look at it carefully and i believe u will identify the mistake u were making...

Code:
void main() {
    int i,j;
    char d;
    char a[4][4] = {0};
    ifstream myfile1 ("p.txt");
    ofstream myfile3 ("c.txt");
    if (myfile1.is_open()) {
        while(!myfile1.eof()) { 
            for (j = 0; j < 4; j++)
                for (i = 0; i < 4; i++) {   
                    myfile1.get(d);
                    a[i][j] = d; // plaintext               
                    d = 0;
                    cout<<a[i][j];
                }   
            if (myfile3.is_open()) {
                for(j = 0; j < 4; j ++)
                    for ( i = 0; i < 4; i++) {
                        if (a[i][j])
                        myfile3<<a[i][j];   
                    }
            } else 
                cout << "Unable to open file";  
        }
        myfile3.close();
    } else 
        cout << "Unable to open file";
    myfile1.close();
}