一次练习 发现的问题,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++相关的内 ...
随机推荐
- Can't find kernel text map area from kcore
kernel : 4.9.51kexec-tools : 2.0.4 1.~ # ./kexec -p /boot/vmlinuz --initrd=/boot/initrd --command-li ...
- android -------- ConstraintLayout Guideline和Barrier(四)
前面的文章 ConstraintLayout 介绍 (一) ConstraintLayout约束属性 (二) ConstraintLayout 宽高比和偏移量比(三) 此博文主要讲解:Guidelin ...
- 4.1.3 Euclid's Game (POJ 2348)
Problem description: 以辗转相除法为基础,给定两个整数a和b,Stan和Ollie轮流从较大的数字中减去较小数字的倍数(整倍数),并且相减后的结果不能为零.Stan先手,在自己的回 ...
- Linux中磁盘分区——理论篇
Linux中磁盘分区——理论篇 现在主流的分区的方式有两种——MBR分区和GPT分区,本文将着重介绍MBR分区底层原理,及用相关命令验证相关原理 Linux中磁盘分区理论篇 为什么要对磁盘进行分区 M ...
- PAT 1031 Hello World for U
1031 Hello World for U (20 分) Given any string of N (≥) characters, you are asked to form the char ...
- MongoDB 教程(三):MongoDB 的下载、安装和配置
一.下载 下载地址:https://www.mongodb.com/download-center#community(这里是Windows 版,其他版本也可以在该网页进行下载) 版本选择: Mong ...
- ECharts 报表事件联动系列二:柱状图,饼状图添加事件
代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- python中bottle模块的使用
1.简介 2.示例 2.1一个简单的bottle接口 # -*- coding: utf-8 -*- from bottle import route, request, run import jso ...
- Hadoop格式化 From hu-hadoop1/192.168.11.11 to hu-hadoop2:8485 failed on connection exception: java.net.
192.168.11.12:8485: Call From hu-hadoop1/192.168.11.11 to hu-hadoop2:8485 failed on connection excep ...
- Linux下切换使用两个版本的JDK
Linux下切换使用两个版本的JDK 我这里原来已经配置好过一个1.7版本的jdk. 输出命令: java -version [root@hu-hadoop1 sbin]# java -version ...