Ofstream and Ifstream conflicts

0
down vote
favorite


Why is it that I calling the ifstream and ofstream objects in the same scope causes the program below to misbehave by not reading up to end of file or not writing anything to the output file. The program reads an input file called Smart.txt and it writes something to an output file called Great.txt and if the word NAMES is encountered in the input file, it writes something to a file called Temp.txt and at thesame time under this condition opens the Great.txt as input file in which it reads and searches a line containing the word "people" and displays the line number. But unfortunately, once NAMES is met, then all other words from the Smart.txt would not be read. WHY THIS? PLEASE SOMEONE SHOULD HELP. HERE IS THE CODE BELOW;

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int searchString(ifstream& in_stream, string people);
ifstream fin;
ofstream fout;

int main(){


cout << "Begin editing files.\n";

fin.open("Smart.txt");
if (fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}

fout.open("Great.txt", ios::app);
if (fout.fail())
{
cout << "Output file opening failed.\n";
fin.close();
exit(1);
}
fout << "#include <iostream>" << "\n";
fout << "#include <fstream>" << "\n";
fout << "#include <stdio.h>" << "\n";
fout << "people" << "\n";
string identifier;
char next;
fin.get(next);
while (!fin.eof())
{


identifier += next;
fin.get(next);

while (isspace(next)){

while (next == '\n' && identifier == "Discos"){
fout << "Profit" << "\n";
break;
}
while (next != '\n' && identifier == "NAMES"){
fout.close();
fout.open("Temp.txt", ios::app);
if (fout.fail())
{
cout << "Output file opening failed.\n";
fin.close();
exit(1);
}
fout << "using namespace std; " << "\n";

fin.close();
fin.open("Great.txt", ios::app);


int searchstringline;
searchstringline = searchString(fin, "people");
cout << searchstringline;

break;
}
fout.close();
fin.close();
fin.open("Smart.txt");
if (fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}

fout.open("Great.txt", ios::app);
if (fout.fail())
{
cout << "Output file opening failed.\n";
fin.close();
exit(1);
}
fin.get(next);
identifier = "";
}

}
}



int searchString(std::ifstream& in_stream, std::string people){
std::string input;
int number = 0;
for (int i = 0; getline(fin, input); i++){
number++;
if (input.find(people) != std::string::npos){

break;
}

}
return number;
}

you need to provide the input files that you use, ¿how do you expect for us to test it otherwise?

your code, formatted and with some comments
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int searchString (ifstream& in_stream, string people);
ifstream fin; //¿why are these global?
ofstream fout;

int main ()
{
	cout << "Begin editing files.\n";
	fin.open ("Smart.txt"); // prefer using the constructor
	if (fin.fail ())
	{
		cout << "Input file opening failed.\n";
		exit (1);
	}
	fout.open ("Great.txt", ios::app);
	if (fout.fail ())
	{
		cout << "Output file opening failed.\n";
		fin.close ();
		exit (1);
	}
	fout << "#include <iostream>"
	     << "\n";
	fout << "#include <fstream>"
	     << "\n";
	fout << "#include <stdio.h>"
	     << "\n";
	fout << "people"
	     << "\n";
	string identifier;
	char next;
	fin.get (next);
	while (!fin.eof ()) // don't loop on eof(), loop on the reading operation (you'll stop too late
	                    // and wouldn't detecct other errors
	{
		identifier += next;
		fin.get (next);
		while (isspace (next))
		{
			while (next == '\n' && identifier == "Discos")
			{
				fout << "Profit"
				     << "\n";
				break; //¿? ¿what's the point on an unconditional break, a loop with only 1
				       //iteration is not a loop
			}
			while (next != '\n' && identifier == "NAMES")
			{
				fout.close ();
				fout.open ("Temp.txt", ios::app); //¿why don't just use another variable?
				if (fout.fail ())
				{
					cout << "Output file opening failed.\n";
					fin.close ();
					exit (1);
				}
				fout << "using namespace std; "
				     << "\n";
				fin.close ();
				fin.open ("Great.txt",
				          ios::app); //¿`app' on an input file? Again, just use another variable
				int searchstringline;
				searchstringline = searchString (fin, "people");
				cout << searchstringline;
				break; // Again, ¿why a break?
			}
			fout.close ();
			fin.close ();
			fin.open (
			"Smart.txt"); // so you'll read the file from the beginning again, ¿when do you plan to stop?
			if (fin.fail ())
			{
				cout << "Input file opening failed.\n";
				exit (1);
			}
			fout.open ("Great.txt", ios::app);
			if (fout.fail ())
			{
				cout << "Output file opening failed.\n";
				fin.close ();
				exit (1);
			}
			fin.get (next);
			identifier = "";
		}
	}
}

int searchString (std::ifstream& in_stream, std::string people) // as this is a generic algorithm,
                                                                // `people' is a terrible parameter
                                                                // name
{
	std::string input;
	int number = 0;
	for (int i = 0; getline (fin, input); i++) // you don't use the parameter, but a global
	{
		number++;
		if (input.find (people) != std::string::npos)
		{
			break;
		}
	}
	return number; //¿what if it isn't there?
}

Topic archived. No new replies allowed.