Groups > Borland > Borland Turbo C plus plus > Re: fread don't read a null char '\0' ???




fread don't read a null char '\0' ???

fread don't read a null char '\0' ???
Sat, 07 Apr 2007 08:22:47 GMT
I have a problema with fread!
When I try to read a file, the NULL char '\0' is jumped!
If in my file there is:
1234\0\0\01234
I read:
1234\01234 or 12341234
Why?
I can read a file for send with socket?
Post Reply
Re: fread don't read a null char '\0' ???
Mon, 09 Apr 2007 19:24:59 -030
Ciccio escreveu:
> I have a problema with fread!
> When I try to read a file, the NULL char '\0' is jumped!
> If in my file there is:
> 1234\0\0\01234
> I read:
> 1234\01234 or 12341234

One *or* other?

Can you produce the shortest program (compilable) that exhibit that 
behavior?
Post Reply
Re: fread don't read a null char '\0' ???
Mon, 9 Apr 2007 19:41:58 -0400
To read binary data, open the file in binary mode.

.  Ed

> Ciccio wrote in message
> news:rxIRh.1337$uJ5.22140@twister2.libero.it...
>
>I have a problema with fread!
> When I try to read a file, the NULL char '\0' is jumped!
> If in my file there is:
> 1234\0\0\01234
> I read:
> 1234\01234 or 12341234
> Why?
> I can read a file for send with socket?
> Alternative at fread? Thanks!

Post Reply
Re: fread don't read a null char '\0' ???
Wed, 11 Apr 2007 09:01:26 GMT
Cesar Rabak ha scritto:
> Ciccio escreveu:
>> I have a problema with fread!
>> When I try to read a file, the NULL char '\0' is jumped!
>> If in my file there is:
>> 1234\0\0\01234
>> I read:
>> 1234\01234 or 12341234
> 
> One *or* other?
> 
> Can you produce the shortest program (compilable) that exhibit that 
> behavior?
> 
Thanks a Rabak and Ed Mulroy for interest.
My problem is: I send buffer of char and I modify this buffer using 
function for string!
I received with:
recv(LocalSock,buf,1024,0) with buf -> char buf[1024];
In first five char there is my ID, 5 char for identify correct pack.
I must remove this 5 char. I can use memmove or evolution function 
StringCchCopy: StringCchCopy(received,1024,buf+5);
In both I have a problem: the '\0'.

I have search an example: all people used for send a file a cycle FOR 
without ID, without my 5 char for identify the correct pack. Why?
If someone ping my port during the send/receive file, I have error!
Help! It's wrong the algorith? I must do a cycle for send a file or I 
Post Reply
Re: fread don't read a null char '\0' ???
Wed, 11 Apr 2007 13:53:29 +020
Ciccio <ciccio.unico@libero.it> writes:

> Cesar Rabak ha scritto:
>> Ciccio escreveu:
>>> I have a problema with fread!
>>> When I try to read a file, the NULL char '\0' is jumped!
>>> If in my file there is:
>>> 1234\0\0\01234
>>> I read:
>>> 1234\01234 or 12341234
>> One *or* other?
>> Can you produce the shortest program (compilable) that exhibit that
>> behavior?
>>
> Thanks a Rabak and Ed Mulroy for interest.
> My problem is: I send buffer of char and I modify this buffer using
> function for string!
> I received with:
> recv(LocalSock,buf,1024,0) with buf -> char buf[1024];
> In first five char there is my ID, 5 char for identify correct pack.
> I must remove this 5 char. I can use memmove or evolution function
> StringCchCopy: StringCchCopy(received,1024,buf+5);
> In both I have a problem: the '\0'.
>
> I have search an example: all people used for send a file a cycle FOR
> without ID, without my 5 char for identify the correct pack. Why?
> If someone ping my port during the send/receive file, I have error!
> Help! It's wrong the algorith? I must do a cycle for send a file or I
> can send single pack?

IMHO, your description is too complex and not consistent enough to
tell where the problem is.

For example, in your original post, you wrote about NUL characters
read from a file. Now you seem to be receiving the bytes over a
socket.

Assuming that the bytes are received over a socket, my first step
would be to determine whether the correct number of NUL characters is
in buf when recv() returns. What output do you get if you add
something like

for (unsigned int i=0; i!=10; ++i)
  std::cout << static_cast<unsigned int>(buf[i]) << ' ';
std::cout << '\n';

directly after the line that invokes recv()?

Post Reply
about | contact