in the files in_stream2 and 4 they wont open correctly but the rest of them will and i cant move forward until i fix it. i dont even no where to start when trying to fix it could someone help?

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
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("itemnumber.txt", ios::out);
		 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 << " Sys

tem ran correctly " << endl;
}

out_stream3.open("dollarlist.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("proff.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;
			}
in_stream.close(); // closing files.
  out_stream1.close();
  in_stream2.close();
  out_stream3.close();
  in_stream4.close();
  out_stream5.close();
  system("pause");

}

Recommended Answers

All 8 Replies

ifstream in_stream2; // reads pricelist.txt
in_stream2.open(" PRICELIST.txt",ios::in);

Do those filenames match? Nope.....
You have an extra 'space' at the beginning of the filename and you have it in all caps. Delete the space and convert the name to lowercase.

ifstream in_stream4;// read recipt.txt
in_stream4.open("proff.txt", ios::in);

These names don't match either...

i did that and the files still wont open correctly u got any other suggestions

here is the code with the change the ifstreams i believe are making the errors

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

Write a program that opens and closes one of the files that isn't working and does nothing else. See if that works. If THAT works and the full program doesn't, compare the code. If the code is the same, then make sure you aren't:

  1. Deleting the file mid-program.
  2. Opening the file for reading, reading the whole file, having the ifstream fail, then trying to reopen without "cleaning up" (clearing the flags).
  3. Opening the file for output, not closing it, then trying to read that same file for input.
  4. Any other possiblility where two streams are trying to access the same file or a stream has failed.

yes that was correct thanks, i took the error checks out and the program ran. i will have to figure out why

i think i have tried everything wat are other methods to opening a file for reading using ifstream. my files wont open correctly so computer doesnt read the rest of my program, help please.
i think i have tried everything wat are other methods to opening a file for reading using ifstream. my files wont open correctly so computer doesnt read the rest of my program, help please.

You're going to have to post the code. You said that it worked in the last post.

yeah i got it to run without checking to see if the files open correctly im sorry for the confusion

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
using namespace std;
using std::ifstream;
using std::ofstream;
using std::ios;
int main()
{
ifstream in_stream; // reads ITEMSLIST.txt
    ofstream out_stream1; // writes in listWititems.txt
    ifstream in_stream2; // reads PRICELIST.txt
	ofstream out_stream3;//	writes in listWitprices.txt
	ifstream in_stream4;// read display.txt
	ofstream out_stream5;// write showitems.txt

//double p1=0.0,p2=0.0;
int wrong=0;
//int count =0;
//char next;

in_stream.open("C:\Users\mortellis\Documents\ITEMLIST.txt", ios::in); // list of avaliable items
		if( in_stream.fail() )// check to see if itemlist.txt is open
			{
			   wrong++;   // counts number of errors
			   cout << " the error occured here0, you have " << wrong++ << " errors"  << endl;
			   cout << "Error opening the file\n" << endl;
			   cin.clear();
			   exit(1);
		    }
		 else{
			 cout << " System ran correctly " << endl;
			}
out_stream1.open("C:\Users\mortellis\Documents\Visual Studio 2008\Projects\openclose\openclose\listofitems.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" << endl;
			   cin.clear();
			   exit(1);
		    }
		 else{
			 cout << " System ran correctly " << endl;
			}
in_stream2.open("C:\Users\mortellis\DocumentsPRICELIST.txt", ios::in);			 
		if( in_stream2.fail() )// check to see if itemlist.txt is open
			{
			   wrong++;
			   cout << " the error occured here2, you have " << wrong++ << " errors"  << endl;
			   cout << "Error opening the file\n" << endl;
			   cin.clear();
			   exit(1);
		    }
		 else{
			 cout << " System ran correctly " << endl;
			}
out_stream3.open("C:\Users\mortellis\Documents\Visual Studio 2008\Projects\openclose\openclose\listofdollars.txt", ios::out);			 
		if( out_stream3.fail() )// check to see if itemlist.txt is open
			{
			   wrong++;
			   cout << " the error occured here3, you have " << wrong++ << " errors"  << endl;
			   cout << "Error opening the file\n" << endl;
			   cin.clear();
			   exit(1);
		    }
		 else{
			 cout << " System ran correctly " << endl;
			}
in_stream4.open("C:\Users\mortellis\Documents\Visual Studio 2008\Projects\openclose\openclose\display.txt", ios::in);
		if( in_stream4.fail() )// check to see if itemlist.txt is open
			{
			   wrong++;
			   cout << " the error occured here4, you have " << wrong++ << " errors"  << endl;
			   cout << "Error opening the file\n" << endl;
			   cin.clear();
			   exit(1);
		    }
		 else{
			 cout << " System ran correctly " << endl;
			}
out_stream5.open("C:\Users\mortellis\Documents\Visual Studio 2008\Projects\openclose\openclose\printitems.txt", ios::out);
		if( out_stream5.fail() )// check to see if itemlist.txt is open
			{
			   wrong++;
			   cout << " the error occured here5, you have " << wrong++ << " errors"  << endl;
			   cout << "Error opening the file\n" << endl;
			   cin.clear();
			   exit(1);
		    }
		 else{
			 cout << " System ran correctly " << endl;
			}
	  in_stream.close(); // closing files.
out_stream1.close();
  in_stream2.close();
  out_stream3.close();
  in_stream4.close();
  out_stream5.close();
  system("pause");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.