Link to home
Start Free TrialLog in
Avatar of sudhakar_koundinya
sudhakar_koundinya

asked on

Splitting the Huge file into multiple files

Hi All,

    For some reason, I am splitting the binary file into multiple files.    But I think I made a mistake in below code and I am not able to figure it out. Please help, by knowing me where I made a mistake in the code,

If you feel below code is not a right idea, I would like to have an alternate solution in sp;itting the files

Thanks,
Sudhakar  


       long x=0;
      while(true)
      {
            BYTE* buf=new BYTE[200000];
            long n=file.Read(buf,200000);
             x=x+n;
            if(n<=0)
            {
                  
                  break;
            }
            CString str;
            str.Format("%ld",n);
            CString strFile;
            strFile.Format("C:\\Files\\%d.msg",count);
            CFile file1;
            file1.Open(strFile,CFile::modeCreate|CFile::modeReadWrite|CFile::shareDenyNone|CFile::typeBinary);
            file1.Write(buf,n);
            count++;
            file1.Close();
            
            file.Seek(x,CFile::begin);
            Sleep(2);
      }
      file.Close();
SOLUTION
Avatar of Barthax
Barthax
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sudhakar_koundinya
sudhakar_koundinya

ASKER

Hi

Thank You for your comments.

I change the code as you mentioned. But I am still getting wrong set of data.

Actually the file is of size 35 mb. When I try my code/your code I am getting total of 968 KB of data only i.e. it is able to split 10 files only. Actually it should split arround 185 files. I used both read and readHuge methods. but no use. I need your help more

Thanks
Sudhakar

                int count=0;
            BYTE* buf=new BYTE[200000];
            long n=file.ReadHuge(buf,200000);
            CString strFile;
            CString str;
            CFile file1;
      while(n>0)
      {
            str.Format("%ld",n);            
            strFile.Format("C:\\DerexlFiles\\%d.txt",count);                     file1.Open(strFile,CFile::modeCreate|CFile::modeReadWrite|CFile::shareDenyNone|CFile::typeBinary);
            file1.WriteHuge(buf,n);
            AfxMessageBox(strFile+" "+str);
            count++;
            file1.Close();            
            Sleep(2);
            n=file.ReadHuge(buf,200000);            
      }
      delete []buf;
You need to start adding some error handling to the code.  The CFile::Open returns a false if it fails.  If it does fail, then the Write/WriteHuge will throw an exception - you may be inadvertently capturing the exception elsewhere.  I'd also try stepping through the code - especially if you are not getting enough message boxes from your AfxMessageBox call confirming that each write has occured.
Hi,

I was able to open the file perfectly. for 10 iterationsof while loop, it has created 10 files and I am getting the data. But at 11 th iteration ReadHuge(buf,200000) is returning 0. So it is coming out of loop. And also I am not getting any exception while reading from file or writing to the file

Thanks
Sudhakar
Is the last file of the correct size (200000) or is it smaller?  If it is smaller, then that would suggest you did hit the end of the file and your posted code is correctly working - but your unposted code is probably setting the file pointer to a latter location than you might expect.
>> Is the last file of the correct size (200000) or is it smaller??

It is 200000 then in next iteration ReadHuge is returning 0 bytes
n=file.ReadHuge(buf,200000);

to

n=file.ReadHuge(buf,2000);

Does it get past 11 iterations?
If so does it read the entire file?

This may provide some insight as to why it is failing.
>>Does it get past 11 iterations?
No. At 11 th iteration it is returning out from the method
>>If so does it read the entire file?
No

Hmmm.. what happens on a smaller file?  How about a 1k file (where it should read everything in with the first call to ReadHuge)?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial