在c中替换文件中的字符
我正在编写一个程序,它应该用“A”替换文件中的第10、11和12个字符。问题是,我的程序只会在文件末尾写这些A。你能帮帮我吗?提前谢谢在c中替换文件中的字符,c,file,C,File,我正在编写一个程序,它应该用“A”替换文件中的第10、11和12个字符。问题是,我的程序只会在文件末尾写这些A。你能帮帮我吗?提前谢谢 #include <fcntl.h> #include <stdio.h> #include <errno.h> #include <string.h> int main(int argc, char **argv) { int des,l,k; char buf; des = open("tmp",O_CRE
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(int argc, char **argv)
{
int des,l,k;
char buf;
des = open("tmp",O_CREAT|O_APPEND|O_RDWR,S_IRUSR|S_IWUSR);
if (l = lseek(des,10L,SEEK_SET)== -1)
{
perror("lseek()");
return (des);
} //get to the position 10
else
{
for (k=0;k<3;k++)
{
write(des,"A",1);
}
} //write AAA
read(des,&buf,1);
printf("%c",buf); //checking
return 0;
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
整数des,l,k;
焦炉;
des=开放(“tmp”,O|U创建| O|U附加| O|U RDWR,S|U IRUSR | S|U IWUSR);
如果(l=lseek(des,10L,寻道集)=-1)
{
perror(“lseek()”);
返回(des);
}//到达位置10
其他的
{
对于(k=0;k您使用了O_APPEND
,因此数据被追加到文件的末尾
将其从open
的参数中删除是有效的。您使用了O_APPEND
,因此数据被追加到文件的末尾。是的,就是这样。非常感谢