65,186
社区成员




//楼主的需求:我想解决的是重复打开程序向文件写入数据,不会重复写入数据名。
//这段代码实现楼主的需求
#include "stdafx.h"
#include<vector>
#include<string>
#include<fstream>
#include<iostream>
#include<sstream>
using namespace std;
int save_file(char *name,int age,float height);
int _tmain(int argc, _TCHAR* argv[])
{
save_file("Tom",21,172.8);
save_file("John",25,189.5);
return 0;
}
int save_file(char *name,int age,float height)
{
ofstream outFile("D:\\myfile.csv",ios::out|ios::ate|ios::app);
//打开文件用于写,若文件不存在就创建它
//ios::app 以追加的方式打开文件,ios::ate 文件打开后定位到文件尾
if(!outFile){//打开文件失败则结束运行,不是用于检查文件是否存在
cerr<<"fail"<<endl;
return -1;
}
else{
if((int)outFile.tellp()==0)//outFile.tellp()为文件指针的偏移量,为0,证明文件为空,为首次进入
outFile << "Name" << "," << "age" << "," << "height" << endl; //首次进入写入一次数据名
outFile << name << "," << age << "," << height << endl;
}
outFile.close();
return 1;
}
#include "stdafx.h"
#include<vector>
#include<string>
#include<fstream>
#include<iostream>
#include<sstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ofstream outFile("D:\\myfile.csv",ios::out|ios::ate|ios::app);
//打开文件用于写,若文件不存在就创建它
if(!outFile){ //打开文件失败则结束运行
cerr<<"fail\n"<<endl;
return -1;
}
else{
outFile << "Name" << "," << "age" << "," << "height" << endl;
outFile << "Tom" << "," << 21 << "," << 172.8 << endl;
outFile << "John" << "," << 25 << "," << 189.5 << endl;
}
outFile.close();
return 0;
}
//outFile.open("D:\\myfile.csv",ios::in);
//outFile.open("D:\\myfile.csv",ios::out|ios::ate|ios::app);
//Open成员函数,利用同一对象对多个文件进行操作时要用到open函数
//outFile.close();
//outFile.open("D:\\myfile.csv",ios::out|ios::ate|ios::app);
//outFile.open("D:\\myfile.csv",ios::out);
//outFile << "Name" << "," << "age" << "," << "height" << endl;
//outFile.close();
#include "stdafx.h"
#include<vector>
#include<string>
#include<fstream>
#include<iostream>
#include<sstream>
using namespace std;
int save_file(char *name,int age,float height);
int _tmain(int argc, _TCHAR* argv[])
{
//outFile << "Tom" << "," << 21 << "," << 172.8 << endl;
//outFile << "John" << "," << 25 << "," << 189.5 << endl;
save_file("Tom",21,172.8);
save_file("John",25,189.5);
return 0;
}
int save_file(char *name,int age,float height)
{
ofstream outFile("D:\\myfile.csv",ios::out|ios::ate|ios::app);
//打开文件用于写,若文件不存在就创建它
//ios::app 以追加的方式打开文件
//ios::ate 文件打开后定位到文件尾,ios:app就包含有此属性
if(!outFile){//打开文件失败则结束运行,不是用于检查文件是否存在
cerr<<"fail"<<endl;
return -1;
}
else{
//outFile.tellp()为文件指针的偏移量, 偏移量为0,证明文件为空,为首次进入
if((int)outFile.tellp()==0)
outFile << "Name" << "," << "age" << "," << "height" << endl;
outFile << name << "," << age << "," << height << endl;
}
outFile.close();
return 1;
}
if(!outFile){ //打开文件失败则结束运行,不是用于检查文件是否存在
#include "stdafx.h"
#include<vector>
#include<string>
#include<fstream>
#include<iostream>
#include<sstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ofstream outFile("D:\\myfile.csv",ios::out|ios::ate|ios::app);
//打开文件用于写,若文件不存在就创建它
if(!outFile){ //打开文件失败则结束运行
cerr<<"fail\n"<<endl;
return -1;
}
else{
outFile << "Name" << "," << "age" << "," << "height" << endl;
outFile << "Tom" << "," << 21 << "," << 172.8 << endl;
outFile << "John" << "," << 25 << "," << 189.5 << endl;
}
outFile.close();
return 0;
}
//outFile.open("D:\\myfile.csv",ios::in);
//outFile.open("D:\\myfile.csv",ios::out|ios::ate|ios::app);
//Open成员函数,利用同一对象对多个文件进行操作时要用到open函数
//outFile.close();
//outFile.open("D:\\myfile.csv",ios::out|ios::ate|ios::app);
//outFile.open("D:\\myfile.csv",ios::out);
//outFile << "Name" << "," << "age" << "," << "height" << endl;
//outFile.close();
参考资料:https://blog.csdn.net/cdl123456/article/details/14142551?ops_request_misc=&request_id=&biz_id=102&utm_term=outFile.open(%22D:%5C%5Cmyfile.csv%22,&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-14142551.pc_search_result_no_baidu_js#include "stdafx.h"
#include<vector>
#include<string>
#include<fstream>
#include<iostream>
#include<sstream>
using namespace std;
int save_file(char *name,int age,float height);
int _tmain(int argc, _TCHAR* argv[])
{
//outFile << "Tom" << "," << 21 << "," << 172.8 << endl;
//outFile << "John" << "," << 25 << "," << 189.5 << endl;
save_file("Tom",21,172.8);
save_file("John",25,189.5);
return 0;
}
int save_file(char *name,int age,float height)
{
ofstream outFile("D:\\myfile.csv",ios::out|ios::ate|ios::app);
//打开文件用于写,若文件不存在就创建它
//ios::app 以追加的方式打开文件
//ios::ate 文件打开后定位到文件尾,ios:app就包含有此属性
if(!outFile){//打开文件失败则结束运行,不是用于检查文件是否存在
cerr<<"fail"<<endl;
return -1;
}
else{
//outFile.tellp()为文件指针的偏移量, 偏移量为0,证明文件为空,为首次进入
if((int)outFile.tellp()==0)
outFile << "Name" << "," << "age" << "," << "height" << endl;
outFile << name << "," << age << "," << height << endl;
}
outFile.close();
return 1;
}
[/quote]
好的,谢谢大佬。