three out of five file streams wont open, i believe its a problem with my ifstreams [closed]

僤鯓⒐⒋嵵緔 提交于 2019-12-25 09:42:11

问题


#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;
            }

回答1:


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

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

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

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.




回答2:


I'm guessing you are on windows. You can't read and write to the same file like this as the sharing conventions on windows prevent it.

I'm guessing that you are doing some batch processing on the various files, and updating them as you go. You will need to write your output files to temporary files (eg "ITEMLIST.out.txt") and then copy or rename them to the original filenames once you have closed them.

See also the remove function as well as rename.



来源:https://stackoverflow.com/questions/2672367/three-out-of-five-file-streams-wont-open-i-believe-its-a-problem-with-my-ifstre

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!