奇怪的fwrite [英] Strange fwrite

查看:84
本文介绍了奇怪的fwrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小程序,它做了类似tftp的事情 - 转移了

文件和一些聊天,无论如何我遇到了fwrite的问题,这里是我的一个

片段。代码:


while(length> 0){

putchar(''。''); //这些用于错误检查

if(length< = bsize){//缓冲区大于剩余文件

realloc(缓冲区,长度* sizeof(char) ); //调整大小缓冲区

保持

rec = recv(袜子,缓冲区,长度,0); //收到数据

p = buffer; //将p设置为缓冲区

i = length; //设置我保持

而(i> 128){//分解成128字节块

putchar(''。'');

fwrite(p,sizeof(char),128,writefile);

p = p + 128; //转到下一个128字节

i = i - 128;

}

fwrite(p,sizeof(char),i,writefile) ;

} else {

rec = recv(sock,buffer,bsize,0); // bsize是1024

putchar(''。'');

p =缓冲区;

for(i = 0; i< 8 ; i ++){

fwrite(p,sizeof(char),128,writefile);

putchar(''。'');

p = p + 128;

}

}


长度是要传输的整个文件的长度,bsize是

接收缓冲区的大小,sock是一个socket,p是指针,rec和

i是整数。


现在来了我的问题我编译了这个代码并在Windows下运行它

2000(作为接收者)它挂在fwrite调用(因为我

见2点)然后我将程序复制到我的WinXP机器上,它很快地运行了

!!!


有没有人有什么想法那里有什么问题?我花了我6个小时直到

i发现这不是我的错(之后我在winXP上尝试过)


要添加的一件事是最新的那个奇怪的分解成128块
块。我运行了一个小测试程序,用fwrite一次增加了写入的
字节的数量,我得到一个大约140的错误,所以我认为
一次认为128个字节应该是保存。


非常感谢你。

I wrote a small program which does something like tftp - transfering
files and some chat, anyway i got a problem with fwrite, here is a
snippet of my code:

while(length > 0) {
putchar(''.''); //These were for error checking
if(length <= bsize) { //Buffer is bigger than remaining File
realloc(buffer,length * sizeof(char)); //resize buffer
to remain
rec = recv(sock,buffer,length,0); //recieve Data
p = buffer; //set p to buffer
i = length; //set i to remain
while(i > 128) { //break down into 128byte blocks
putchar(''.'');
fwrite(p,sizeof(char),128,writefile);
p = p + 128; //go to next 128 bytes
i = i - 128;
}
fwrite(p,sizeof(char),i,writefile);
} else {
rec = recv(sock,buffer,bsize,0); //bsize is 1024
putchar(''.'');
p = buffer;
for(i=0;i<8;i++) {
fwrite(p,sizeof(char),128,writefile);
putchar(''.'');
p = p + 128;
}
}

length is the length of the whole file to be transmitted, bsize is the
size of the recieve buffer, sock is a socket, p is as pointer, rec and
i are integers.

And now comes my Problem i compiled this code and ran it under Windows
2000 (being the reciever) and it hangs at the fwrite calls (since I
see 2 dots) then i copied the programm to my WinXP machine and it ran
happily!!!

Has Anybody got any Idea whats wrong there??? I costed me 6 hours till
i figured out that it wasn''t my fault (after that i tried it on winXP)

One thing to add is whats up with that strange breaking down into 128
Blocks. I ran a small test programm ever increasing the number of
bytes written at once with fwrite, and i got an Error around 140 so i
thought 128 bytes at once should be save.

Thank you soo much.

推荐答案



" SW1" < SC ********** @ yahoo.de>在消息中写道

news:79 ************************** @ posting.google.c om ...

"SW1" <sc**********@yahoo.de> wrote in message
news:79**************************@posting.google.c om...
我写了一个小程序,它做的事情就像tftp - 转移文件和一些聊天,无论如何我遇到了fwrite的问题,这里是我的代码片段:

while(长度> 0){
putchar(''。''); //这些用于错误检查
if(length< = bsize){//缓冲区大于剩余文件
realloc(缓冲区,长度* sizeof(char)); //调整大小缓冲区
保留
I wrote a small program which does something like tftp - transfering
files and some chat, anyway i got a problem with fwrite, here is a
snippet of my code:

while(length > 0) {
putchar(''.''); //These were for error checking
if(length <= bsize) { //Buffer is bigger than remaining File
realloc(buffer,length * sizeof(char)); //resize buffer
to remain




在哪里存储realloc的返回值?



Where do you store the return of realloc?


没有,但它确实没问题,我唯一的问题是

fwrite !!!即使我没有重新分配,fwrite也不起作用!!
Nowhere, but it does it alright, the only problem i have is with
fwrite!!! fwrite doesn''t work even if i don''t realloc!!




" SW1" < SC ********** @ yahoo.de>在消息中写道

news:79 ************************** @ posting.google.c om ...

"SW1" <sc**********@yahoo.de> wrote in message
news:79**************************@posting.google.c om...
无处,但它没关系,我唯一的问题是用
fwrite !!!即使我没有realloc,fwrite也行不通!!
Nowhere, but it does it alright, the only problem i have is with
fwrite!!! fwrite doesn''t work even if i don''t realloc!!




NO!


修复你的所有问题调试之前的第一个!


了解F'' CODE!


GO HOME!



NO!

FIX ALL YOUR PROBLEMS FIRST BEFORE DEBUGING!

LEARN TO F''ING CODE!

GO HOME!

这篇关于奇怪的fwrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆