3 Replies - 14825 Views - Last Post: 17 January 2007 - 02:24 PM Rate Topic: -----

#1 warchilo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-January 07

read from a file into an array

Posted 17 January 2007 - 06:30 AM

Hello again, hoping for a bit more help on this project. I have finally managed to read from a file and display the information to the screen. however i only need to do this for the list command in my code, for the load command i need to load the each line of a .txt file into an array so it can accessed and shown one at a time using the play command, the code is below can anyone help or point me in the right direction?

#include <iostream>			// input/output objects such as cin and cout
#include <string>			// the string data type
#include <ctime>			// includes system time functions
#include <fstream>			// read and write from files
using namespace std;

// function prototypes
void loadData(string filename);


int main()
{
string command;


	// initial information for user
	cout << "Question Master" << endl;
	cout <<	"This program tests your knowledge in your chosen category." << endl;
	cout <<	"Use the load command to load up your set of questions. Type" << endl;
	cout <<	"help if you wish to see the available commands." << endl;
	cout << "\n" << endl;

	do
	{
		cout << "Enter command: ";
		cin  >> command;
			
		if ( command == "help" )
		{
			cout << "There are four options available to you. load, play, help and list." << endl;
			cout << "\n";
			cout << "load: this option will load the quiz. " << endl;
			cout << "animals.txt" << endl;
			cout << "\n";
			cout << "play: this option will begin the quiz" << endl;
			cout << "\n";
			cout << "list: this option will list questions and answers for the quiz" << endl;
			cout << "\n";
			cout << "finish: this option will end the program" << endl;

		}
		else if ( command == "load" )
		{
			loadData("animals.txt");
		}
		else if ( command == "play" )
		{
			cout << "save command: code not yet written ..." << endl;
		}
		else if ( command == "list" )
		{
			loadData("animals.txt");
		}
		else if ( command == "finish" )
		{
			return 0;
		}
		else
		{
			cout << "I don't recognise the command: " << command << endl;
		}

	}while( command != "finish" );

	cout << "End of Question master" << endl;

	return 0;
}

void loadData(string filename)
{
string  value;
ifstream filein; 					


	filein.open( filename.c_str() ); 		
	if (filein.good())
	{

		//filein >> value;	
		getline(filein, value);			
		while ( filein.good() )			
		{// 
			cout << value << endl;	
			//filein >> value;
			getline(filein, value);
		}
		filein.close();				
		filein.clear(0);				
	}
	else
	{
		cout << "filename not recognised" << endl;
	}

}


This post has been edited by warchilo: 17 January 2007 - 06:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: read from a file into an array

#2 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: read from a file into an array

Posted 17 January 2007 - 09:15 AM

Does it have to be an array, or can you use vectors?

EDIT: If it is an array you are only allowed to use, you are going to have to read the file and count how many questions you have in it, then allocate the memory for storing the questions. Then read the file into the array. The reason i asked if you are allowed to use vectors is because you don't need to allocate the memory for it. You can just read all the data into the vector, all the memory allocation is done for you.

This post has been edited by BitByte: 17 January 2007 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#3 cdk  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 17-January 07

Re: read from a file into an array

Posted 17 January 2007 - 01:26 PM

If you want to use vectors, here is an example you can use:

  std::vector<std::string> file;
  std::string line;
  file.clear();
  std::ifstream infile ("animals.txt", std::ios_base::in);
  while (getline(infile, line, '\n'))
  {
	file.push_back (line);
  }



The original code is part of a complete example program available at http://www.decompile...e_to_vector.htm

Best regards,

cdk
Was This Post Helpful? 0
  • +
  • -

#4 bluesuus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 26-December 05

Re: read from a file into an array

Posted 17 January 2007 - 02:24 PM

WEll.................this is where Dynamic memory allocation comes to play..Since basically Your file contains characters or numbers.........i suggest u use linkedList.....You wont have to worry about the space at the begining of ur program.......................But the storage of ur characters is ur choice, u cud decide to store 4 characters in one node or more....................WEll guuud luk
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1