#include <stdio.h>#include <stdlib.h>#include<string.h>int main(void) { FILE *file; char msg[3]="12"; file=fopen("/tmp/test/test","a+"); if(file==NULL) printf("get file eroor\n"); fseek(file,0,SEEK_SET); printf("offset=%ld\n",ftell(file)); fwrite(msg,sizeof(char),strlen(msg),file); fclose(file); return EXIT_SUCCESS;}linux下用c写的一个文件操作函数,目的是追加方式打开文件,在文件头写入部分数据,而原来的数据继续保留。但是测试发现数据还是写入了文件末尾?
这是因为:file=fopen("/tmp/test/test","a+"); 模式 “a+” 中的 a 规定以只写打开,【原文件数据】保留。因此你不能读,只能写。写的时候只能在【原文件数据】的末尾开始写。即使你将【文件位置指针】重新定位过,在执行写操作的时候,【文件位置指针】【自动】回到【原文件数据】的末尾了。也就是【原文件数据】被保护起来,你是无法操作的。【原文件数据】无法操作的定义是:不能再对【原文件数据】执行添加,删除,插入等。因此,你要在【原文件数据】的开头插入数据,可能要绕个弯子:1.用只读模式("a") 先读出【原文件数据】,保存到内存2.用只写模式(“w”)清除【原文件数据】3.文件开头插入的信息。4.再追加【原文件数据】(保存在内存)当然如果在文件末尾添加就方便多了,用 "a" 模式一步到位。下面使用这种思路进行插入信息的示例代码:#include <stdio.h>#include <stdlib.h>#include<string.h>const int MAX_FILE_SIZE = 100;//读取文件内容 int fileRead(char fileName[],char content[],int* content_len){ FILE *infile; char ch; infile=fopen(fileName,"r"); *content_len=0; if(infile==NULL) { printf("get infile eroor\n"); return 0; } else { rewind(infile); while(EOF!=(ch=fgetc(infile))) { content[(*content_len)++]=ch; } } fclose(infile); return 1;}//在文件末尾追加内容 int fileAppend(char fileName[] ,char content[],int content_len){ FILE *outfile; outfile=fopen(fileName,"a"); fwrite(content,content_len,1,outfile); fclose(outfile);}int fileClear(char fileName[]){ FILE *file = fopen(fileName,"w"); fclose(file);}//在文件开头插入内容 int insetMessageInfile(char fileName[] ,char message[],int message_len){ char content[MAX_FILE_SIZE]; int content_len=0; fileRead(fileName,content,&content_len); fileClear(fileName); fileAppend(fileName,message,strlen(message)); fileAppend(fileName,content,content_len); }int main(void) { char fileName[] = "data.txt"; char msg[3]="12"; //文件内容缓存区 char content[MAX_FILE_SIZE]; int content_len=0; int i; fileRead(fileName,content,&content_len); for(i=0;i<content_len;i++) printf("%c",content[i]); printf("\n"); insetMessageInfile("data.txt",msg,strlen(msg)); fileRead(fileName,content,&content_len); for(i=0;i<content_len;i++) printf("%c",content[i]); printf("\n"); return EXIT_SUCCESS;}
如果使用“a+”方式打开文件,就是要求系统把写入内容补充到文件的末尾,如果希望打开重新设置文件内容,应该使用“w”方式打开文件。或者你在linux下试一下,用“a+”打开文件,用fseek(File,0,0)定位一下,然后再做写入操作。
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>int main(int argc, char **argv){ if (argc != 3) { printf("Usage: <app> <filepath> <write message>\n"); return 0; } FILE *fp; long nOffset; char *pszFileBuff; fp = fopen(argv[1], "r+"); if (!fp) { printf("fopen %s error: %s\n", argv[1], strerror(errno)); return 0; } fseek(fp, 0, SEEK_END);//去文件末尾 nOffset = ftell(fp);//获取文件长度 fseek(fp, 0, SEEK_SET);//回到头部 pszFileBuff = (char*)malloc(sizeof(char) * nOffset + 1);//开辟文件长度加1长度空间,用于存放文件内容 if (!pszFileBuff) { printf("malloc error:%s\n", strerror(errno)); goto End; } fread(pszFileBuff, nOffset, 1, fp); fseek(fp, 0, SEEK_SET);//回到头部 fprintf(fp, "%s\n", argv[2]); fprintf(fp, "%s", pszFileBuff); free(pszFileBuff);End: fclose(fp); return 0;}这样就可以了。可以加我MSN:wy030@sina.com