size file problem

Hi everybody,

I have a problem with the size of a file. I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(run_tcpserver)
{
bytesRecv = recv(socketInTCP, recvbuf, sizeof(recvbuf), 0);
if (bytesRecv > 0)
{...
 ...
 ofstream outfile(outputfile, ios::out | ios::app);//open
 outfile.write (recvbuf, sizeof(recvbuf));
 outfile.close();
 cont++
 ...
 ...
}
}


recvbuf is 1500 bytes
and I know that I receive 820 times this buffer (cont=820 at the end of the while) that is 820*1500=1230000 bytes.

Wenn I open the file and I check the size, I have more than 1230000 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char *memblock;
int i=0;
int size=0;
ifstream file ("test.txt", ios::in|ios::ate);
if (file.is_open())
{
size = (int) file.tellg();
memblock = new char [size];// allocation of a memory block
file.seekg (0, ios::beg);  // we set the get pointer at the beginning of the file
file.read (memblock, size);
file.close();
}
cout << "Size: " << size;
cout << "\n";


if I work with less data that this case, I mean if I test it with 2 recv buffer the file size is correct, 3000 bytes (1500+1500) but in the other case it doesn't work and I can't see the error.

Can you help me?

Thank you very much

Radeberger
You're writing sizeof(recvbuf) number of bytes rather than bytesRecv. They're not necessarily the same.

In the read program, you should remember that there are restrictions on how much you can read at a time.
Thank you for the answer kbw,

now it works properly and I didn't change anything...but I think I have problems with the read function...I will look for these read restrictions...

Radeberger
Topic archived. No new replies allowed.