打开文件时异常 [英] Exception during opening file

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

问题描述

亲爱的所有人,

函数(_mlock)有什么作用?我不手动调用该函数.我发现许多功能(new, _mbschr,_mbscmp,etc)都会使用它.我不知道为什么我的程序会给出错误(运行时错误).

我将创建一个FTP,客户端将在其中打开文件并读取日期并将数据发送到服务器.服务器打开一个文件并将该数据写入文件,但是当服务器打开该文件时,将出现运行时错误.

Dear all,

What does the function (_mlock) do? I don''t call the function manually. I found that many functions (new, _mbschr,_mbscmp,etc) will use it. I don''t know why my program gives the error (runn time error).

I''m going to create an FTP where a client will open a file and read a date and send data toward to the server. The server opens a file and writes that data to the file but when the server goes to open the file it will get a run time error.

void reciveMsg(unsigned int av_sock);
void reciveFile(unsigned int av_sock);
void reciveMsg(unsigned int av_sock)
{
	int lv_ret;
	char			*lp_buff		= NULL;
		
	while(1)
	{
		lp_buff = (char *)malloc(sizeof(513));
		memset(lp_buff,0,512);
		lv_ret = recv(av_sock,lp_buff,512,0);
		printf("\n%s",lp_buff);
		delete(lp_buff);
		if(lv_ret == -1)
		{
			closesocket(av_sock);
			FD_CLR(av_sock, &master);
			printf("\nSocket closed");
			return;
		}
	}
}
void reciveFile(unsigned int av_sock)
{
	FILE		*fp		= NULL;
	int		lv_ret;
	char		*lp_buff	= NULL;
	char		lv_bufer[512]	= {""}; "//added for color correction
	int		lv_handle	= -1;
	
	
	lv_handle = _open("D:\\xyz\\test.txt", _O_RDWR);
	
	 if(lv_handle == -1)
	 {
		printf("\nError In Opening File");
		return;
	 }
	while(1)
	{
		lp_buff = (char *)malloc(sizeof(1025));
		memset(lp_buff,0,1024);
		lv_ret = recv(av_sock,lp_buff,1024,0);
		lv_ret = _write(lv_handle,lp_buff,sizeof(lp_buff));
		if(lv_ret != sizeof(lp_buff))
		{
			printf("\nTotal Bytes Write [%ld] ",lv_ret);
			return;
		}
		printf("\nWriting In File Please Wait");
	}
	_close(lv_handle); 

}



[添加了双引号以对代码进行颜色校正]



[Added a double-quote to color-correct the code]

推荐答案

lp_buff = (char *)malloc(sizeof(1025)); 
allocates 4 bytes of memory, sizeof(1025) is the same as sizeof(int). 


下一条语句:memset(lp_buff,0,1024);将1024字节设置为零,这很可能破坏了堆.

问候
Espen Harlinn


The next statement: memset(lp_buff,0,1024); sets 1024 bytes to zero, this is probably corrupting your heap.

Regards
Espen Harlinn


这篇关于打开文件时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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