五分之三的文件流不会打开,我认为这是我的ifstreams的问题 [英] three out of five file streams wont open, i believe its a problem with my ifstreams

查看:66
本文介绍了五分之三的文件流不会打开,我认为这是我的ifstreams的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
using namespace std;
int main()
{
    ifstream in_stream; // reads itemlist.txt
    ofstream out_stream1; // writes in items.txt
    ifstream in_stream2; // reads pricelist.txt
    ofstream out_stream3;// writes in plist.txt
    ifstream in_stream4;// read recipt.txt
    ofstream out_stream5;// write display.txt
    int wrong=0;
    in_stream.open("ITEMLIST.txt", ios::in); // list of avaliable items
         if( in_stream.fail() )// check to see if itemlist.txt is open
            {
                   wrong++;
               cout << " the error occured here0, you have " << wrong++ << " errors"  << endl;
               cout << "Error opening the file\n" << endl;
               exit(1);
            }
         else{
             cout << " System ran correctly " << endl;

    out_stream1.open("ITEMLIST.txt", ios::out); // list of avaliable items
         if(out_stream1.fail() )// check to see if itemlist.txt is open
            {
                   wrong++;
               cout << " the error occured here1, you have " << wrong++ << " errors"  << endl;
               cout << "Error opening the file\n";
               exit(1);
            }
         else{
                cout << " System ran correctly " << endl;
            }

    in_stream2.open("PRICELIST.txt", ios::in);           
        if( in_stream2.fail() )
            {
                   wrong++;
               cout << " the error occured here2, you have " << wrong++ << " errors"  << endl;
               cout << "Error opening the file\n";
               exit (1);
            }
            else{
                cout << " System ran correctly " << endl;
            }

    out_stream3.open("PRICELIST.txt", ios::out);             
        if(out_stream3.fail() )
            {
               wrong++;
               cout << " the error occured here3, you have " << wrong++ << " errors"  << endl;
               cout << "Error opening the file\n";
               exit (1);
            }
            else{
                cout << " System ran correctly " << endl;
            }

    in_stream4.open("display.txt", ios::in);
        if( in_stream4.fail() )
            {
                   wrong++;
               cout << " the error occured here4, you have " << wrong++ << " errors"  << endl;
               cout << "Error opening the file\n";
               exit (1);
            }
            else{
                cout << " System ran correctly " << endl;
            }


    out_stream5.open("display.txt", ios::out);
        if( out_stream5.fail() )
            {
                   wrong++;
               cout << " the error occured here5, you have " << wrong++ << " errors"  << endl;
               cout << "Error opening the file\n";
               exit (1);
            }
            else{
                cout << " System ran correctly " << endl;
            }

推荐答案

您的代码有效.我认为您当前的目录不是您想的那样.

Your code works. I think your current directory is not what you think it is.

这些文件存储在哪里?您当前的目录是存储可执行文件的Debug/Release目录还是类似的文件?

Where are these files stored? Is your current directory the Debug/Release directory where the executable is stored or something like that?

打开文件进行写入之前,您需要先调用close.

You need to call close before opening the files for writing.

避免使用exit(0)函数,因为它不会给C ++运行时提供优雅清理的机会.而是抛出std :: runtime_error.

Avoid using the exit(0) function, as it doesn't give the C++ runtime the chance to cleanup gracefully. Throw a std::runtime_error instead.

这篇关于五分之三的文件流不会打开,我认为这是我的ifstreams的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆