
Linux 下使用C语言,调用write多次向同一文件写入同一数组,为啥写入内容相同?
一个大一点程序里的一个问题,自己重新写了段揭示问题的代码。乱了点。#include<iostream>#include<unistd.h>#inc...
一个大一点程序里的一个问题,自己重新写了段揭示问题的代码。乱了点。#include <iostream> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <time.h> #include <sys/types.h> #include <sys/stat.h>using namespace std;int fd,s; //打开文件的文件描述字char * filename;char c[3];void chang(char a,char b){c[0]=a;c[1]=b;cout<<"now c is:"<<c;}int main(){ c[2]='\n'; printf("Hello! Create or use old ones? 0/1 "); scanf("%d", &s); printf("\nPlease input the filename:\n"); scanf("%s", filename); if (s == 0) { fd = open(filename, O_CREAT); if (fd >= 0) cout << "Create successfully!" << endl; close(fd);} chang('e','g'); fd = open(filename, O_RDWR); int i=3; while(i>0){ lseek(fd,0,SEEK_END); write(fd,c,3); chang('s'+i,'f'+i); i--; } i=0; while(i<3){ lseek(fd,i*3,SEEK_SET ); read(fd,c,3); write(2,c,3); i++; }return EXIT_SUCCESS;} 运行结果:Hello! Create or use old ones? 0/1 0Please input the filename:fbnow c is:egnow c is:vinow c is:uhnow c is:tgtgtgtg我知道java里的ObjectOutputStream的writeObject()方法是同一个引用只传输一次,不论引用指向的对象是否改变,是故采用的是writeUnshared()方法,传输不共享对象,并且每次使用reset()重置刷新。这里是否是同样的问题?C/C++语言的重置刷新语法又是什么?
展开
展开全部
试试这样: fd = open(filename, O_CREAT|O_APPEND);
参考资料: http://baike.baidu.com/view/26337.htm
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询