关于linux下C语言遇到的问题 希望朋友们帮忙解答一下。。。。。。。。
#include<fcntl.h>#include<unistd.h>#include<stdio.h>#include<stdlib.h>#include<string...
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int fd,fd2,i;
char bf1[2]="ab";
char bf2[1]="c";
char bf3[1];
char bf4[1];
if((fd = open("/home/mark/桌面/test.txt", O_RDWR | O_CREAT |O_APPEND)) < 0){
printf("Creat or open error!\n");
close(fd);exit(1);
}
else printf("open the file with O_APPEND.\n");
if(write(fd,bf1,2) != 2){
printf("write error!\n");
close(fd);exit(1);
}
else printf("write completed.\n");
if((i=lseek(fd,1,SEEK_SET)) < 0){
printf("lseek error!\n");
close(fd);exit(1);
}
else printf("lseek completed.And the current offset is %d\n",i);
if(write(fd,bf2,strlen(bf2)) < 0){
printf("Faild to add buffer in the middle of the txt file.\n");
close(fd);exit(1);
}
else printf("Completed to add buffer in the middle of the txt file.\n");
if((i=lseek(fd,1,SEEK_SET)) < 0){
printf("lseek error!\n");
close(fd);exit(1);
}
else printf("lseek completed.And the current offset is %d\n",i);
close(fd);
if(read(fd,bf3,1) < 0){
printf("Faild to read the buffer in the middle of the txt file.\n");
close(fd);exit(1);
}
else printf("Completed to read the buffer in the middle of the txt file. And the buffer is %s\n",bf3);
close(fd);
fd2 = open("/home/mark/桌面/test.txt", O_RDWR);
lseek(fd2,1,SEEK_SET);
read(fd2,bf4,1);
printf("The buffer in the middle of the txt file is %s\n",bf4);
if(bf4 == bf2){
printf("write OK\n");
if(bf3 == bf2){
printf("read OK\n");
close(fd2);exit(1);
}else{
printf("read Failed\n");
}
}else{
printf("write Failed\n");
if(bf3 == "bbbbbbbbbb"){
printf("read OK\n");
close(fd2);exit(1);
}else{
printf("read Failed\n");
}
}
close(fd2);
return 0;
}
运行后会出现意想不到的乱码 望指正 求速秒
程序主要验证在O_APPEND模式下能否使用lseek写入和读取文件中的内容
第68行的引号里只有一个b 那么多个打错了。。。。。。
第42行的close(fd);可以删了 打错里。。。。。 展开
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int fd,fd2,i;
char bf1[2]="ab";
char bf2[1]="c";
char bf3[1];
char bf4[1];
if((fd = open("/home/mark/桌面/test.txt", O_RDWR | O_CREAT |O_APPEND)) < 0){
printf("Creat or open error!\n");
close(fd);exit(1);
}
else printf("open the file with O_APPEND.\n");
if(write(fd,bf1,2) != 2){
printf("write error!\n");
close(fd);exit(1);
}
else printf("write completed.\n");
if((i=lseek(fd,1,SEEK_SET)) < 0){
printf("lseek error!\n");
close(fd);exit(1);
}
else printf("lseek completed.And the current offset is %d\n",i);
if(write(fd,bf2,strlen(bf2)) < 0){
printf("Faild to add buffer in the middle of the txt file.\n");
close(fd);exit(1);
}
else printf("Completed to add buffer in the middle of the txt file.\n");
if((i=lseek(fd,1,SEEK_SET)) < 0){
printf("lseek error!\n");
close(fd);exit(1);
}
else printf("lseek completed.And the current offset is %d\n",i);
close(fd);
if(read(fd,bf3,1) < 0){
printf("Faild to read the buffer in the middle of the txt file.\n");
close(fd);exit(1);
}
else printf("Completed to read the buffer in the middle of the txt file. And the buffer is %s\n",bf3);
close(fd);
fd2 = open("/home/mark/桌面/test.txt", O_RDWR);
lseek(fd2,1,SEEK_SET);
read(fd2,bf4,1);
printf("The buffer in the middle of the txt file is %s\n",bf4);
if(bf4 == bf2){
printf("write OK\n");
if(bf3 == bf2){
printf("read OK\n");
close(fd2);exit(1);
}else{
printf("read Failed\n");
}
}else{
printf("write Failed\n");
if(bf3 == "bbbbbbbbbb"){
printf("read OK\n");
close(fd2);exit(1);
}else{
printf("read Failed\n");
}
}
close(fd2);
return 0;
}
运行后会出现意想不到的乱码 望指正 求速秒
程序主要验证在O_APPEND模式下能否使用lseek写入和读取文件中的内容
第68行的引号里只有一个b 那么多个打错了。。。。。。
第42行的close(fd);可以删了 打错里。。。。。 展开
展开全部
open the file with O_APPEND.
write completed.
lseek completed.And the current offset is 1
Completed to add buffer in the middle of the txt file.
lseek completed.And the current offset is 1
Faild to read the buffer in the middle of the txt file.
我运行出这个结果,是你想要的吗?
write completed.
lseek completed.And the current offset is 1
Completed to add buffer in the middle of the txt file.
lseek completed.And the current offset is 1
Faild to read the buffer in the middle of the txt file.
我运行出这个结果,是你想要的吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询