一次练习 发现的问题,malloc free 无效;findfirstfile失败,writefile失败的原因
#include <windows.h>
#include <stdio.h>
#include <shlwapi.h> #pragma comment(lib,"shlwapi.lib") int myRemoveDirectory(char *szPath)
{
if(!PathIsDirectory(szPath))
{
return GetLastError();
}
BOOL bRes = SetCurrentDirectory(szPath);
if(!bRes)
{
return GetLastError();
}
char * buf =(char *)malloc(MAX_PATH);
ZeroMemory(buf,sizeof(buf)); strcpy(buf,szPath); PathAddBackslash(buf); strcat(buf,"*.*"); WIN32_FIND_DATA findData = {0};
HANDLE hFile = FindFirstFile(buf,&findData);
int nRes = DeleteFile(findData.cFileName);
while(FindNextFile(hFile,&findData))
{
DeleteFile(findData.cFileName);
}
SetCurrentDirectory("D:\\");
FindClose(hFile);
if(!RemoveDirectory(szPath))
{
int nRes = GetLastError();
return nRes;
}
return 0;
} char * MyCreateFile()
{
HANDLE hFile = NULL;
char * cPath = "d:\\temp________111111111111";
if(!myRemoveDirectory(cPath))
{ }
BOOL bRes = CreateDirectory(cPath,NULL);
int nRes = GetLastError();
if(!bRes)
{
MessageBox(NULL,"Failed to create files","Alert",MB_OK);
return NULL;
}
bRes = SetCurrentDirectory(cPath);
char * pszFilePath = (char *)malloc(1024);
strcpy(pszFilePath,cPath);
PathAppend(pszFilePath,"text.txt");
hFile = CreateFile(pszFilePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile == NULL)
{
return NULL;
}
int i,j;
char *buff = (char *)malloc(1024);
char * mallocHeap = buff; // 记录新创建的地址。
if(hFile == INVALID_HANDLE_VALUE)
{
return NULL;
}
DWORD nBytes = 0;
char temp[MAX_PATH]={0};
ZeroMemory(buff,1024);
for(i=1;i<10;i++)
{
for(j=1;j<=i;j++)
{
sprintf(temp,"%d*%d = %d \t",j,i,i*j); //改变temp的指向,temp 不在指向原先的内存地址。
strcat(buff,temp);
}
strcat(buff,"\r\n");
WriteFile(hFile,buff,strlen(buff)+1,&nBytes,NULL);
ZeroMemory(buff,1024);
} CloseHandle(hFile);
free(mallocHeap); return pszFilePath; }
int MyReadFile()
{
char * pszFilePath = MyCreateFile(); HANDLE hFile = CreateFile(pszFilePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile == NULL)
{
return 0;
}
char *buf = (char * )malloc(1024*1024);
char * mallocHeap = buf;
ZeroMemory(buf,sizeof(buf));
DWORD nBytes = 0;
ReadFile(hFile,buf,1024,&nBytes,NULL);
if(nBytes < 1024 )
{
while(nBytes)
{
printf("%s",buf);
int nTemp = strlen(buf);
buf = buf + nTemp +1;
nBytes = nBytes- nTemp-1;
}
}
else
{
while(nBytes)
{ printf("%s",buf);
ZeroMemory(buf,sizeof(buf));
ReadFile(hFile,buf,1024,&nBytes,NULL);
} }
CloseHandle(hFile);
free(mallocHeap);
return 1;
}
int main()
{
if(MyReadFile())
{
MessageBox(NULL,"Success","Success",MB_OK);
}
getchar();
getchar();
return 0;
}
总结:
一次练习 发现的问题,malloc free 无效;findfirstfile失败,writefile失败的原因的更多相关文章
- 早期malloc分配时,如果内存耗尽分配不出来,会直接返回NULL。现在分配不出来,直接抛出异常(可使用nothrow关键字)
今天和同事review代码时,发现这样的一段代码: Manager * pManager = new Manager(); if(NULL == pManager) { //记录日志 return f ...
- malloc实现原理
记得早一段时间,看到一本书上写过delete的一个..今天突然找啦一下资料: malloc()是C语言中动态存储管理 的一组标准库函数之中的一个.其作用是在内存的动态存储区中分配一个长度为size的连 ...
- 最小重组缓冲区和路径MTU发现
概括: 主要来源于unp,可参考:http://blog.csdn.net/ysu108/article/details/7764461 疑惑: 1. 最小重组缓冲区大小: ipv4为576,ipv6 ...
- malloc函数分配内存失败的常见原因
malloc()函数分配内存失败的常见原因: 1. 内存不足. 2. 在前面的程序中出现了内存的越界访问,导致malloc()分配函数所涉及的一些信息被破坏.下次再使用malloc()函数申请内存 ...
- C++异常处理
引言 异常,让一个函数可以在发现自己无法处理的错误时抛出一个异常,希望它的调用者可以直接或者间接处理这个问题.而传统错误处理技术,检查到一个局部无法处理的问题时: 1.终止程序(例如atol,atoi ...
- Linux Programe/Dynamic Shared Library Entry/Exit Point && Glibc Entry Point/Function
目录 . 引言 . C/C++运行库 . 静态Glibc && 可执行文件 入口/终止函数 . 动态Glibc && 可执行文件 入口/终止函数 . 静态Glibc & ...
- C输入输出函数与缓冲区
#转 对C语言输入输出流和缓冲区的深入理解C语言缓冲区(缓存)详解缓冲区又称为缓存,它是内存空间的一部分.也就是说,在内存空间中预留了一定的存储空间,这些存储空间用来缓冲输入或输出的数据,这部分预留的 ...
- Java编程思想读书笔记
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CPPUTest 单元测试框架(针对 C 单元测试的使用说明)
CPPUTest 虽然名称上看起来是 C++ 的单元测试框架, 其实它也是支持测试 C 代码的. 本文主要介绍用CPPUTest来测试 C 代码. (C++没用过, 平时主要用的是C) C++相关的内 ...
随机推荐
- Lab 1-2
Analyze the file Lab01-02.exe. Questions and Short Answers Upload the Lab01-02.exe file to http://ww ...
- android -------- Lint优化工具
什么是 Lint Lint 是Android Studio 提供的 代码扫描分析工具,它可以帮助我们发现代码结构/质量问题,同时提供一些解决方案,而且这个过程不需要我们手写测试用例. Lint 发现的 ...
- 『TensorFlow』第十弹_队列&多线程_道路多坎坷
一.基本队列: 队列有两个基本操作,对应在tf中就是enqueue&dequeue tf.FIFOQueue(2,'int32') import tensorflow as tf '''FIF ...
- phpmyadmin getshell
执行SQL语句:SET GLOBAL general_log = ON
- rancher2.x添加node的坑。
启动rancher server后,添加一台新节点为k8s的节点.设置如下 ps:worker:k8s的agent端 control:k8s的第二个master etcd:第二个etcd 坑1:节点上 ...
- python-django rest framework框架之视图
视图 :常用 1和4 两种 1. 原始的APIView class IndexView(views.APIView): def get(self, request, *args, **kwargs): ...
- 用swoole简单实现MySQL连接池
MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...
- Git:多人推送/抓取分支事项
1.推送分支 1.1使用命令符git push origin branch-name,推送自己已修改的分支 例如git push origin master,git push origin dev. ...
- Object对象的浅拷贝与深拷贝方法详解
/* ===================== 直接看代码 ===================== */ <!DOCTYPE html> <html> <head& ...
- Hadoop---Exception in thread "main" java.lang.NullPointerException问题的解决
Exception in thread "main" java.lang.NullPointerException问题的解决 解决: 首先我们来分析下这个问题,我是使用debug ...