Why are my files not opening?

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
ifstream in_stream;
ifstream in_stream2;
ofstream out_stream;
int num1;
int num2;



in_stream.open("LabInput3-3.txt");
if(in_stream.fail())
{
cout << "File would not open.";
exit(1);
}
in_stream2.open("LabInput3-4.txt");
if(in_stream2.fail())
{
cout << "File would not open.";
exit(1);
}
out_stream.open("LabOutput.txt");
if(out_stream.fail())
{
cout << "File would not open.";
exit(1);
}
while(!in_stream.eof())
{
bool smaller = false;
in_stream >> num1;
while(!in_stream2.eof())
in_stream2 >> num2;
if(num1 < num2)
{
out_stream << num1;
smaller = true;
}
if(smaller == true);
{
out_stream << num2;
}
}
in_stream.close();
in_stream.clear();
in_stream2.close();
in_stream2.clear();
out_stream.close();




return 0;
}
Hi,

Please always use code tags :

http://www.cplusplus.com/articles/z13hAqkS/


Are your files in the same directory as the code ?

Some other things:

Don't loop on eof, just use the stream:

1
2
while(in_stream) {
}


And this:
if(smaller == true); // <----remove semicolon
Hi,

Sorry, I'm a beginner.
Fixed the semicolon and the loop.
I just put the files in the src folder where the code is, but it still doesn't open.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
int main() 
{
	ifstream in_stream;
	ifstream in_stream2;
	ofstream out_stream;
	int num1;
	int num2;



	in_stream.open("LabInput3-3");
	if(in_stream.fail())
	{
	    cout << "File would not open.";
	    exit(1);
	}
	in_stream2.open("LabInput3-4");
	if(in_stream2.fail())
	{
	    cout << "File would not open.";
	    exit(1);
	}
	out_stream.open("LabOutput");
	if(out_stream.fail())
	    {
	        cout << "File would not open.";
	        exit(1);
	    }
	while(in_stream)
	{
	    bool smaller = false;
	    in_stream >> num1;
	while(in_stream2)
	    in_stream2 >> num2;
	if(num1 < num2)
	{
	    out_stream << num1;
	    smaller = true;
	}
	if(smaller == true)
	{
	    out_stream << num2;
	}
	}
	in_stream.close();
    in_stream.clear();
    in_stream2.close();
    in_stream2.clear();
    out_stream.close();
    return 0;
}
You should be able to do this:

ifstream in_stream("LabInput3-3.txt");

Also, does the while on line 33 need braces for lines 34 to 43?

Edit: When posting code, post all of it - the include files etc. This is so we can compile it with the cpp.sh gear icon on the top right of the code.
Last edited on
I put braces around the lines and now it builds but instead of displaying "File would not open," the whole program terminates. I checked the output file for any characters but it's empty.
Are you using windows? The terminal is probably closing before you see anything, try commenting out the exit statements.

Not sure why the files not opening. I will have a try on my system and see what happens.
Oh ok, the output file had some characters OUTSIDE of the src file.

Inside of the 3-3 file is:
6 10 15 22 33 77 88 99
Inside of the 3-4 file is:
30 40 50 60 62 89

but the output file is:
630640650660662689689

How could I change my code so it arranges both the 3-3 and the 3-4 file so it is smallest to largest?

Ok, The input files need to be in the same directory as the executable file (my bad earlier), then it should work. I managed to get it working on my system doing this :+)
Topic archived. No new replies allowed.