|
-
Nov 7th, 2002, 02:28 PM
#1
Thread Starter
Addicted Member
problem when reading from large binary file
do you see anything wrong this this code. sSend is a class that encapsulates the winsock API.
Code:
ifstream openfile((const char *)FileName.c_str(),ios::in | ios::binary);
char tmpContent[10000] = {0};
int left = 0;
if(openfile.is_open())
{
//int length = openfile.tellg();
while(!openfile.eof())
{
left = GetFileLen((char *)FileName.c_str()) - openfile.gcount();
if(left < 2048)
{
openfile.read(tmpContent, left);
}
else
{
openfile.read(tmpContent, 2048);
}
//openfile.read(tmpContent, left);
sSend.SendData(tmpContent);
}
sSend.Close();
}
Code:
int GetFileLen(char *path)
{
ifstream TheFile((const char *)path, ios::in | ios::ate | ios::binary);
return TheFile.tellg();
}
-
Nov 7th, 2002, 03:01 PM
#2
You may get problems with file locking. Usually ifstream will lock the file so that it can't be opened another time.
Why do you recalculate the file size every iteration? Calculate it once and be done with it.
Why do you cast the return of c_str? It's already of type const char * and every useless cast is dangerous as it might occlude real errors.
Why is the buffer 10000 bytes large when you never read more than 2048 bytes?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 7th, 2002, 03:11 PM
#3
Thread Starter
Addicted Member
ok i fixed the things u said the function now looks like this:
Code:
ifstream openfile(FileName.c_str(),ios::in | ios::binary);
char tmpContent[2048] = {0};
int left = 0;
int size = GetFileLen((char *)FileName.c_str());
if(openfile.is_open())
{
//int length = openfile.tellg();
while(!openfile.eof())
{
left = size - openfile.gcount();
if(left < 2048)
{
openfile.read(tmpContent, left);
}
else
{
openfile.read(tmpContent, 2048);
}
//openfile.read(tmpContent, left);
sSend.SendData(tmpContent);
}
sSend.Close();
}
-
Nov 8th, 2002, 07:42 AM
#4
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 8th, 2002, 12:59 PM
#5
Thread Starter
Addicted Member
unfortunately no, it worked fine for a small text file with about 15 characters in it. But then i tried it on a 24KB executable and it didn't work. It sent the first line of code correctly, but after that it just sent a couple long lines of weird looking ÿ's along with some other weird characters. The final file was only 4KB instead of 24KB. I have no clue why. It shouldn't matter whether tmpContent is a char or unsigned char right?
-
Nov 8th, 2002, 12:59 PM
#6
Thread Starter
Addicted Member
by the way this function is a member of a class and takes place inside a thread.
-
Nov 8th, 2002, 08:02 PM
#7
You open it binary, is it supposed to contain printable data?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 8th, 2002, 10:56 PM
#8
Thread Starter
Addicted Member
no, but I opened its output in notepad.
-
Nov 9th, 2002, 06:59 AM
#9
Then it's no surprise it's strange characters. So the only strange thing is that it doesn't copy the full amount of data. Hav you tried doing a debug run?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 9th, 2002, 01:52 PM
#10
Thread Starter
Addicted Member
it is a suprise that its strange characters since it is different strange characters then the original exe. I havent tried a debug run, but I did put a MessageBox before the part where I send the data and it shows the same strange characters that end up in the output file.
-
Nov 9th, 2002, 02:05 PM
#11
Fanatic Member
try
char *tmpContent = new char[2048];
strcpy(tmpContent, " ");
Maybe the memory it allocated has garbage in it, and if its less than 2048, itll fill whatever amount, but leave the rest with garbage, that happens to me sometimes
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Nov 9th, 2002, 02:20 PM
#12
Thread Starter
Addicted Member
the code is now this and it produces the same result with the same problem:
Code:
ifstream openfile(FileName.c_str(),ios::in | ios::binary);
char *tmpContent = new char[2048];
strcpy(tmpContent, " ");
int left = 0;
int size = GetFileLen((char *)FileName.c_str()) + 1;
if(openfile.is_open())
{
while(!openfile.eof())
{
left = size - openfile.gcount();
if(left < 2048)
{
openfile.read(tmpContent, left);
}
else
{
openfile.read(tmpContent, 2048);
}
MessageBox(NULL,tmpContent, "tmpContent", MB_OK);
sSend.SendData(tmpContent);
}
sSend.Close();
}
-
Nov 9th, 2002, 02:27 PM
#13
Monday Morning Lunatic
Your "SendData" function -- how does it know how big the data block is?
Also, I'd recommend using memset() rather than strcpy() -- set all of tmpContent to 0 first.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 9th, 2002, 02:35 PM
#14
Thread Starter
Addicted Member
The code now looks like this and produces the same result with the same problem, I have commented the SendData function in case it was creating any problems.
Code:
ifstream openfile(FileName.c_str(),ios::in | ios::binary);
char *tmpContent = new char[2048];
memset(tmpContent, 0, 2048);
int left = 0;
int size = GetFileLen((char *)FileName.c_str()) + 1;
if(openfile.is_open())
{
while(!openfile.eof())
{
left = size - openfile.gcount();
if(left < 2048)
{
openfile.read(tmpContent, left);
}
else
{
openfile.read(tmpContent, 2048);
}
MessageBox(NULL,tmpContent, "tmpContent", MB_OK);
//sSend.SendData(tmpContent);
}
sSend.Close();
}
-
Nov 9th, 2002, 02:44 PM
#15
Monday Morning Lunatic
You can't display binary data, remember.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 9th, 2002, 02:44 PM
#16
Thread Starter
Addicted Member
when u say display, do u mean MessageBox()?
-
Nov 9th, 2002, 03:13 PM
#17
Thread Starter
Addicted Member
ok i just made a big improvement. I fixed the SendData function so it works properly and i fixed a couple other things. Now the output file looks very similiar to the original file. They are both 24.0 KB in Size, but the output file is 28.0 size on disk (according to properties). What is the difference between size and size on disk? Also when I open up the original file and the output file in notepad they seem to have the same content, except in slightly different places. And all the weird characters are different in each file. Would it matter that im using char and not unsigned char?
-
Nov 9th, 2002, 08:39 PM
#18
Lively Member
Size on disk just means how much physical space it's taking. Your hard drive is divided into clusters, that are usually 8k big. Even a 1k file will take up 8k of space on your drive.
-
Nov 9th, 2002, 09:15 PM
#19
Thread Starter
Addicted Member
So how could two files be the same size but different sizes on disk?
-
Nov 10th, 2002, 05:29 PM
#20
It could be more fragmented.
The cluster size is not always the same, it's dependent on the disk and on the file system.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 10th, 2002, 07:43 PM
#21
Thread Starter
Addicted Member
Is there any reason why it would be screwing up the binary characters? are you sure it doenst need to be an unsigned char?
-
Nov 10th, 2002, 09:43 PM
#22
Lively Member
Perhaps the problem is in the function that receives it?
-
Nov 11th, 2002, 05:35 AM
#23
PowerPoster
Hi, CornedBee, how "every useless cast is dangerous as it might occlude real errors"? Just wondering to know. As it might occur in my daily code
-
Nov 11th, 2002, 08:38 AM
#24
Test Arawn's idea by directly writing to another file, then comparing those.
Chris: The compiler disallows some implicit casts because it can't know if it makes sense, e.g. casting a base class pointer to a derived class pointer. Since the compiler can't know if the base class pointer really points to a derived object, and because it would cause huge problems if it didn't you have to explicitly cast the pointer, either with reinterpret_cast, dynamic_cast or C-style cast. Getting into the habit of explicitly casting everything that produces problems could for example lead to the following problems:
1)
Code:
void func(int *some_val)
{
// lots and lots of code
int i = some_val;
}
This creates a compiler casting error. Without really looking you just add the explicit cast.
Code:
void func(int *some_val)
{
// lots and lots of code
int i = (int)some_val;
}
Bingo, you are *****ed.
2)
Code:
#include <string>
using std::string;
string s("Hello there");
// dumdidum
// cast, else compiler error
char * cs = (char *)s.c_str();
If you now modify cs you have problems again. Casting away const is never good and means that something with your code design is wrong. The const modifier is really great.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 11th, 2002, 08:42 AM
#25
Monday Morning Lunatic
Originally posted by CornedBee
Code:
#include <string>
using std::string;
string s("Hello there");
// dumdidum
// cast, else compiler error
char * cs = (char *)s.c_str();
If you now modify cs you have problems again. Casting away const is never good and means that something with your code design is wrong. The const modifier is really great.
I don't think you're allowed that last one, I thought you needed const_cast It might have changed though.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 11th, 2002, 08:46 AM
#26
You were allowed in C, because there was no const_cast, and I don't think they anyhow reduced the abilities of the C-style cast.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 11th, 2002, 02:16 PM
#27
Monday Morning Lunatic
There wasn't originally a const in C, though.
Just testing....
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 11th, 2002, 02:18 PM
#28
Monday Morning Lunatic
You're right.
Code:
#include <iostream>
void yarr(const int *blah) {
int* hmmm = (int*)blah;
*hmmm = 5;
}
int main() {
int weebl;
cout << weebl << endl;
yarr(&weebl);
cout << weebl << endl;
}
That compiles fine (and works, at least on this one...).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 11th, 2002, 06:59 PM
#29
LOLOLOLOL !!!!!
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 11th, 2002, 07:13 PM
#30
Monday Morning Lunatic

Thought I'd add some insanity to the situation
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 11th, 2002, 09:23 PM
#31
Thread Starter
Addicted Member
lol ok i figured out why it wasnt working. You were right the problem was on the receiving end. The program that was receiving it was made in VB and when I checked the source I found that it wasn't writing to the file in binary mode. I fixed that and now it seems to be working fine.
-
Nov 11th, 2002, 09:23 PM
#32
Thread Starter
Addicted Member
P.S. thx for all ur help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|