LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2011, 05:09 PM   #1
lalhad21
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Rep: Reputation: 0
Tried to open a text file and program stops working


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();
};
 
Old 02-08-2011, 05:56 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
	ReadInitialise; // it was ok until I called this function
That line doesn't call the function, you need

Code:
	ReadInitialise();
There is nothing wrong with having two files open at once, by the way.

Last edited by ntubski; 02-08-2011 at 05:56 PM. Reason: to -> two
 
Old 02-08-2011, 05:58 PM   #3
lalhad21
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Hmm, it doesn't work even with the brackets.

I'm almost sure it breaks when it tries to open the second file
 
Old 02-08-2011, 07:37 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
You need to say what you mean by "stopped working" and "breaks". Are you getting any kind of error messages?

Since you are using raw arrays with hard coded lengths your code will crash if the input files don't have the correct number of lines.
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Prompt the user for a file to open, extract the XML and write to another text file. richiep Linux - Newbie 7 10-22-2010 03:34 PM
in Pascal: how to exec a program, discard text output or send to text file Valkyrie_of_valhalla Programming 6 05-02-2007 09:50 AM
Text File to Audio File Program ? rvijay General 5 04-10-2006 03:50 PM
mouse stops working when GTK program lauches (Debian sarge) arthur_kalm Linux - General 3 03-13-2006 09:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration