c++ ofstream判断文件是否存在后,创建成功,写入失败

xuedingyue 2021-04-08 05:22:38
#include "stdafx.h"
#include<vector>
#include<string>
#include<fstream>
#include<iostream>
#include<sstream>


using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
ofstream outFile;
outFile.open("D:\\myfile.csv",ios::in);
if(!outFile){
outFile.open("D:\\myfile.csv",ios::out);
outFile << "Name" << "," << "age" << "," << "height" << endl;
outFile.close();
}
else{
outFile.close();
outFile.open("D:\\myfile.csv",ios::out|ios::ate|ios::app);
outFile << "Tom" << "," << 21 << "," << 172.8 << endl;
outFile << "John" << "," << 25 << "," << 189.5 << endl;
outFile.close();
}
return 0;
}
文件不存在时,if执行,文件创建成功,但不会写入数据

各位大佬帮忙看一下哪个地方出了问题,谢谢了
...全文
2122 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
qzjhjxj 2021-04-10
  • 打赏
  • 举报
回复
楼主的需求:我想解决的是重复打开程序向文件写入数据,不会重复写入数据名。楼主代码不能实现的原因,是对文件的打开及写入数据过程和用代码怎么写,有点出入,这段代码已经实现楼主的需求,供参考(后附两次运行后的图片):
//楼主的需求:我想解决的是重复打开程序向文件写入数据,不会重复写入数据名。
//这段代码实现楼主的需求
#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;
}
QtHalcon 2021-04-10
  • 打赏
  • 举报
回复

#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();
qzjhjxj 2021-04-09
  • 打赏
  • 举报
回复
引用 6 楼 xuedingyue 的回复:
[quote=引用 3 楼 qzjhjxj 的回复:]供参考:
#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
感谢大佬,写的很详细,但我想解决的是重复打开程序向文件写入数据,不会重复写入数据名。搞清楚我的代码为什么不会写入。[/quote] 修改如下,供参考:
#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;
}
xuedingyue 2021-04-09
  • 打赏
  • 举报
回复
引用 3 楼 qzjhjxj 的回复:
供参考:
#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
感谢大佬,写的很详细,但我想解决的是重复打开程序向文件写入数据,不会重复写入数据名。搞清楚我的代码为什么不会写入。
xuedingyue 2021-04-09
  • 打赏
  • 举报
回复
引用 1 楼 ggglivw 的回复:
很简单,因为你根本就没有判断文件是否打开成功. 你open以后应该用is_open这个函数来判断,而不是用局部变量
加了is_open函数判断,返回的是ture,文件打开成功
qzjhjxj 2021-04-09
  • 打赏
  • 举报
回复
补充楼上:
 if(!outFile){    //打开文件失败则结束运行,不是用于检查文件是否存在
qzjhjxj 2021-04-09
  • 打赏
  • 举报
回复
供参考:
#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
  • 打赏
  • 举报
回复
ggglivw 2021-04-09
  • 打赏
  • 举报
回复
很简单,因为你根本就没有判断文件是否打开成功. 你open以后应该用is_open这个函数来判断,而不是用局部变量
  • 打赏
  • 举报
回复
无论是事先创建文本文件还是文件不存在都没问题
  • 打赏
  • 举报
回复
打开文件被占用自然无法写入
xuedingyue 2021-04-09
  • 打赏
  • 举报
回复
#include "stdafx.h" #include<vector> #include<string> #include<fstream> #include<iostream> #include<sstream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { ofstream outFile; outFile.open("D:\\myfile.csv",ios::in); if(!outFile){ outFile.open("D:\\myfile.csv",ios::out); //if(outFile.is_open()) if(!outFile.bad()){ cout<<1<<endl; outFile << "Name" << "," << "age" << "," << "height" << endl; return 0; } outFile.close(); } else{ outFile.close(); outFile.open("D:\\myfile.csv",ios::out|ios::ate|ios::app); outFile << "Tom" << "," << 21 << "," << 172.8 << endl; outFile << "John" << "," << 25 << "," << 189.5 << endl; outFile.close(); } return 0; }
xuedingyue 2021-04-09
  • 打赏
  • 举报
回复
引用 2 楼 智者知已应修善业 的回复:
可参
加入判断机制,判断文件被打开,依然没有写入
xuedingyue 2021-04-09
  • 打赏
  • 举报
回复
引用 7 楼 qzjhjxj 的回复:
[quote=引用 6 楼 xuedingyue 的回复:][quote=引用 3 楼 qzjhjxj 的回复:]供参考:
#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
感谢大佬,写的很详细,但我想解决的是重复打开程序向文件写入数据,不会重复写入数据名。搞清楚我的代码为什么不会写入。[/quote] 修改如下,供参考:
#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] 好的,谢谢大佬。

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧