C++ 写文件
int WriteFile(char* ljx,char *url,char* icon,char* name) //写文件操作; { FILE *stream; char lj[128]={}; char l1[256] = "[{000214A0-0000-0000-C000-000000000046}]\r\nProp3=19,11\r\n[InternetShortcut]\r\nIDList=\r\nURL="; char l3[256] = "\r\nIconFile="; char l5[] = "\r\nIconIndex=1\r\n"; strcat(lj,ljx); strcat(lj,"\\"); strcat(lj,name); if ((stream = fopen(lj, "wb")) == NULL) /* open file TEST.$$$ */ { fprintf(stderr, "Cannot open output file.\n"); return -1; } strcat(l1,url); strcat(l3,icon); fwrite(l1, sizeof(l1), 1, stream); fwrite(l3, sizeof(l3), 1, stream); fwrite(l5, sizeof(l5), 1, stream); fclose(stream); /*关闭文件*/ return 0; }
int AddPage(char* url,char* name) // 写书签 { //Win7 及以上目录; char Lj[128] = "C:"; strcat(Lj,getenv("homepath")); char i[]= "\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks"; char j[]= "\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks1"; char Lj1[128]={0}; strcpy(Lj1,Lj); strcat(Lj,i); strcat(Lj1,j); // XP 兼容目录; FILE *fp; if((fp=fopen(Lj,"r"))==NULL) { char Ljx[128] = "C:"; strcat(Ljx,getenv("homepath")); char ix[]= "\\Local Settings\\Application Data\\Google\\Chrome\\User Data\\Default\\Bookmarks"; char jx[]= "\\Local Settings\\Application Data\\Google\\Chrome\\User Data\\Default\\Bookmarks1"; char Lj1x[128]={0}; strcpy(Lj1x,Ljx); strcat(Ljx,ix); strcat(Lj1x,jx); if((fp=fopen(Ljx,"r"))==NULL) { return -1; } strcpy(Ljx,Lj); strcpy(Lj1x,Lj1); } fclose(fp); /////////////////////////////////////////////////////////// ifstream in(Lj); ofstream out(Lj1); string filename; string line; string str="children"; string str1 = "],"; bool tof = true; //创建拷贝文件; while (getline (in, line)) { if (tof && strstr(line.c_str(),str.c_str())) { if (strstr(line.c_str(),str1.c_str())) { out << " \"children\": [ " << endl; out<<" \"date_added\": \"13087947012000000\","<<endl; out << " \"id\": \"2\"," << endl; out << " \"name\": \""<<name<<"\"," << endl; out << " \"type\": \"url\"," << endl; out << " \"url\": \""<<url<<"\"" << endl; out << " } ]," << endl; } else { out << line << endl; out<<" \"date_added\": \"13087947012000000\","<<endl; out << " \"id\": \"2\"," << endl; out << " \"name\": \""<<name<<"\"," << endl; out << " \"type\": \"url\"," << endl; out << " \"url\": \""<<url<<"\"" << endl; out << " }, {" << endl; } tof = false; } else { out << line << endl; } } in.close(); out.close(); //删除重命名; remove(Lj); rename(Lj1,Lj); return 0 ; }