4 Replies - 1010 Views - Last Post: 25 April 2012 - 04:46 PM Rate Topic: -----

#1 DevNWF08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-April 12

multidimensional array of strings/ stream from file

Posted 25 April 2012 - 06:39 AM

I have a file with some words like:
Aron
Jack
Angie
Noeh

I have to open the stream and input these as an array, i think it should be a multidimentional array or a pointer but so far i cant make it work..oh the trick is i dont know how many words are going to be in the file it could be 5 or 500. Your help is very appreciated. Here is what I have so far:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>

int main()
{

const int MAX = 1000;
char in_file[25];
char o_file[25];
char word[][MAX];

using namespace std;
//asks the user for the name of the in_stream and the name of the out_stream
			cout << "Please enter the name of the file with the words to process: ";
			cin >> in_file;
			cout << "Please enter the name of the file where the words will be stored: ";
			cin >> o_file;
//declaration of streams
	ifstream in_stream;
	ofstream out_stream;
//test for in_stream to open correctly
				in_stream.open(in_file);
				if (in_stream.fail()){
			cout << "failed to open input file";
				exit(1);
				}
						
	
//test for the outstream to open correctly
				out_stream.open(o_file, ios::app);
				if(out_stream.fail())
				{
				cout << "failed to open output file";
				exit(1);
				}
				
				
						
	in_stream >> word;
										
										
										
	out_stream << word;

//closing both streams
        in_stream.close();
	out_stream.close();
	
	
	


return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: multidimensional array of strings/ stream from file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: multidimensional array of strings/ stream from file

Posted 25 April 2012 - 03:51 PM

Moved from C++ Tutorials. Please do not post help questions in C++ Tutorials.
Was This Post Helpful? 0
  • +
  • -

#3 lifeinbinary  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 158
  • Joined: 15-February 11

Re: multidimensional array of strings/ stream from file

Posted 25 April 2012 - 04:11 PM

I'm wondering why you need a two dimensional array? A one dimensional array would suffice to store a list of strings. If you do not know the size of the word list, using the vector class would be easy. If this is homework and needs to be stored in a that up array then using the max as an array size as you have done is the best you can do. Here is an example of how to use vector:


std::vector<int> numList;

for(int i = 0; i < 10; i++)
{
numList.push_back(i);
}




This will store numbers 0-9 in the vector numList. You can make a vector of any object (like std::string or char* or any class or struct).
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,857
  • Joined: 25-December 09

Re: multidimensional array of strings/ stream from file

Posted 25 April 2012 - 04:26 PM

Quote

A one dimensional array would suffice to store a list of strings

Yes but the OP is using C-strings so they will need a multidimensional array, if they are trying to store an array of C-strings.

The question is has the OP covered vectors, or dynamic memory allocation? Can you use std::strings instead of C-strings?

Jim
Was This Post Helpful? 1
  • +
  • -

#5 DevNWF08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-April 12

Re: multidimensional array of strings/ stream from file

Posted 25 April 2012 - 04:46 PM

i finally solved it using <string> instead of <cstring>
like:

string word[MAX];
for( int i = 0; i < MAX ; i++)
				{
						
	in_stream >> word[i];
				
										
out_stream << word[i]<< "\n" << endl;
										
				}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1