I opened a text file and read some stuff.
Then I (think) I closed it.
Then I tried to open another one
and it stopped working.
It seemed to compile fine though.
Why would this be?
Code:
#include <iostream> // Libraries included
#include <string>
#include <fstream>
#include <sstream>
#include <list>
#include <cctype>
#include <cstdlib>
using namespace std;
//------------------------------------------------------------------------------//
struct Commands // This is the structure which holds the commands //
{ //
string command; //
} //
commands[20]; //
//------------------------------------------------------------------------------//
//------------------------------------------------------------------------------//
struct dataValues // This is the structure which holds the names and //
{ // and ages from the file //
string line; //
string name; //
string number; //
int age; //
} //
numbers[10]; //
//------------------------------------------------------------------------------//
void Instructions(); // Initialise the function "instructions"
void AssignName(); // Initialise the function "Assign name"
void ReadInitialise (); // Reads file
char filename[25], filenamee[25]; // CURRENTLY GLOBAL VARIABLES- DO SOMETHING ABOUT THIS!
string Name2Number, file, file2;
int n, i; // i is used for the function "fileread"
int main () // This is the main program
{
n = 0;
//---------Opening the file------------
//-------------------------------------
cout << "Enter File Name: ";
cin >> filename; // use "commands.txt". Should be saved in the same directory as code.exe.
fstream file; // ifstream is used for reading files.
file.open(filename);
if (!file) // If we couldn't open the output file stream for reading
{
cerr << filename << " could not be opened for reading!" << endl; // Print an error
exit(1);
}
//--------------The following lines are used to read in the text file line by line, assign---------------
//------------the line to a string, and assign the string to the data structure numbers.line-------------
while (file) // While there's still stuff left to read the code inside the brackets loops.
{
n++;
std::string strInput;
file >> strInput;
commands[n].command = strInput;
cout << "\n" << commands[n].command;
}
Instructions();
file.close();
file.clear(); // should clear the problem of having two files open at once. clears state flags
ReadInitialise; // it was ok until I called this function
return 0;
}
//---------------The function "Instructions" assigns each instruction to the data structure--------------
//-------------------------------------------------------------------------------------------------------
void Instructions()
{
for (i = 1 ; i <=20; i++)
{
if (commands[i].command == "r")
cout << "read in file\n";
else if (commands[i].command == "s")
cout << "sort data\n";
else if (commands[i].command == "w")
cout << "write data to output\n";
else if (commands[i].command == "i")
{
cout << "insert a new element in the structure WITHOUT destroying the ordering\n";
AssignName();
}
else if (commands[i].command == "d")
cout << "delete an element in the structure WITHOUT destroying the ordering\n";
else if (commands[i].command == "x")
cout << "reverse order of linked list\n";
else if (commands[i].command == "a")
cout << "calculate average valu\ne";
else if (commands[i].command == "m")
cout << "calculate maximum value\n";
}
}
void AssignName()
{
string newName;
newName = commands[i+1].command;
cout << newName;
}
void ReadInitialise () // This reads the data file, and
{ // puts the data from it into the
// data structure dataValues
n = 0;
//---------Opening the file------------
//-------------------------------------
cout << "Enter File Name: ";
cin >> filenamee; // use "example.txt". Should be saved in the same directory as code.exe.
fstream file2; // ifstream is used for reading files.
file2.open(filenamee);
if (!file2) // If we couldn't open the output file stream for reading
{
cerr << filenamee << " could not be opened for reading!" << endl; // Print an error
exit(1);
}
//--------------The following lines are used to read in the text file line by line, assign---------------
//------------the line to a string, and assign the string to the data structure numbers.line-------------
while (file2) // While there's still stuff left to read the code inside the brackets loops.
{
n++;
std::string strInput;
file2 >> strInput;
numbers[n].line = strInput;
}
//-------------Set alternate lines from the names of the input file (names are put on odd lines)-----------
//----------------------and initialises name values in data structure--------------------------------------
for ( int a = 1; a <= n; a++ ) // loops until it reaches the end of the file.
{ // n depends on size of data structure.
int y;
y= (2*a)-1;
numbers[a].name = numbers[y].line;
}
//--------------Set alternate lines from the ages of the input file (ages are put on even lines)-----------
//--------------------------and initialises age values in data structure-----------------------------------
for ( int b = 1; b <= n; b++ )
{
int z;
z= (2*b);
numbers[b].number = numbers[z].line;
};
//--------------------------Sets age values to integers for working with-----------------------------------
//---------------------------------------------------------------------------------------------------------
for ( int v = 1; v <= 9; v++ )
{
std::string myString = numbers[v].number;
int value = atoi(myString.c_str()); // converts string to integer
numbers[v].age = value;
}
//-------------------Prints all data from structure (for reference only)-----------------------------------
//---------------------------------------------------------------------------------------------------------
for ( int i = 1; i <= 9; i++ )
cout << numbers[i].line << "\n";
for ( int p = 1; p <= 9; p++ )
cout << numbers[p].name << "\n";
for ( int q = 1; q <= 9; q++ )
cout << numbers[q].number << "\n";
for ( int u = 1; u <= 9; u++ )
cout << numbers[u].age <<"\n";
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
file2.close();
file2.clear();
};