Problem with files - Urgent

Discussion in 'C++' started by kartouss, Feb 9, 2008.

  1. kartouss

    kartouss New Member

    Joined:
    Feb 9, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Feb 10, 2008
  2. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    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();
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice