问题描述
- 求助,C语言windows编程
-
在windows中,如何对文件进行读写操作,是使用writefile函数吗?
解决方案
既可以用C语言标准库的函数,也可以用windows api函数,随便你。
解决方案二:
看一下谭浩强的c语言
解决方案三:
可以使用标准的C函数库
#include
#include
#include
#include
#include
#include
int main(void)
{
int handle; char string[40];
int length, res;
/ Create a file named "TEST.$$$" in the current directory and write a string to it. If "TEST.$$$" already exists, it will be overwritten. */
if ((handle = open("TEST.$$$", O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE)) == -1)
{
printf("Error opening file.n");
exit(1);
}
strcpy(string, "Hello, world!n");
length = strlen(string);
if ((res = write(handle, string, length)) != length)
{
printf("Error writing to the file.n");
exit(1);
}
printf("Wrote %d bytes to the file.n", res);
close(handle);
return 0;
}
或者基于Windows的API
CreateFile,WriteFile函数实现,具体方式可以参考WindowsAPI手册