
write/read函数中的cfo指的是什么,是指一行的开始吗
2016-05-16
展开全部
cfo是current file offset的缩写,指示文件当前的操作位置:cfo 通常是一个非负整数,用于表明文件开始处到文件当前位置的字节数。刚打开文件,cfo是0表示偏移0字节,比如读了10个字节后,cfo就是10了,再读2个,就会读出第11和12共2个字节了。写类似。
展开全部
Write函数
用法:
write函数所在的头文件为 <unistd.h>
write有两种用法。一种是:
ssize_twrite(int handle, void *buf, int nbyte);
handle 是文件描述符;
buf是指定的缓冲区,即指针,指向一段内存单元;
nbyte是要写入文件指定的字节数;返回值:写入文档的字节数(成功);-1(出错)
write函数把buf中nbyte写入文件描述符handle所指的文档,成功时返回写的字节数,错误时返回-1.
另一种是:write(const char* str,int n)
str是字符指针或字符数组,用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。
write("string",strlen("string");表示输出字符串常量
程序示例:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <io.h>
#include <string.h>
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; }
用法:
write函数所在的头文件为 <unistd.h>
write有两种用法。一种是:
ssize_twrite(int handle, void *buf, int nbyte);
handle 是文件描述符;
buf是指定的缓冲区,即指针,指向一段内存单元;
nbyte是要写入文件指定的字节数;返回值:写入文档的字节数(成功);-1(出错)
write函数把buf中nbyte写入文件描述符handle所指的文档,成功时返回写的字节数,错误时返回-1.
另一种是:write(const char* str,int n)
str是字符指针或字符数组,用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。
write("string",strlen("string");表示输出字符串常量
程序示例:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <io.h>
#include <string.h>
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; }
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询