Hi everybody!

I must make program in c++ that reads unknown number of words from a text file to char arr which after that i will have to sort the words by the first letter by alphabet in a stack.
can any body help me please?

txt file:
den rock sky apple cycle fruit van sun

i know how to read file with

sting line;
ifstream in("file.txt", ios::out);
getline(in,line);
in.close ();

but I have to learn to program in the lower level - char arr's not by using string that are just linked char arr's

Recommended Answers

All 2 Replies

Here is something to get you started :

int main(){
 const int MAX_WORD_ALLOWED= 1000; //1000 words allowed
 const int MAX_WORD_LENGTH = 256; //words length cannot exceed this amount

 char listOfWords[MAX_WORD_ALLOWED][MAX_WORD_LENGTH] = {};

 ifstream fileInput("test.txt");
 unsigned wordNumber = 0;
 
 while(fileInput.get(listOfWords[wordNumber++],MAX_WORD_LENGTH) && wordNumber < MAX_WORD_ALLOWED ){
   //blah blah blah
 }
}

Its not tested, but you should get the idea.

nope.. i know this is a two-dimensial array but this does not fit because I must be able to read unlimited number words to a char arr list.

i found somewhere that fscanf could be useful, but I do not understand the parameters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.