Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Problem with files - Urgent (http://www.go4expert.com/forums/files-urgent-t8738/)

kartouss 9Feb2008 21:19

Problem with files - Urgent
 
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();
}


technosavvy 12Feb2008 10:35

Re: Problem with files - Urgent
 
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();
}



All times are GMT +5.5. The time now is 11:52.