![]() |
|
#1
|
|||
|
|||
Read file and copy to dinamic arrayI need some help with this function asap please:
char** getWord(const char*, char*, int&); Ok, the first parameter represents the name of the archive where the words are found. The second parameter is the chain where the randomly chosen word is to be placed. It should return the dinamic array of chains. The third parameter should save the size of the chain array. So.. I'm not good with C++, so I want to know how I can open the file and then read it. The text file contains a list of words, one word per line. I need to read the file and then put the words in a dinamic array. Then randomly choose one word for use. *I'm making a hangman game so I need this function so that I can get a word for the user to guess. Also, I don't know how to free the dinamic array at the end of the game and asign NULL to it. |
|
#2
|
|||
|
|||
|
to read files in c++ you need to use the fstream library: #include<fstream>.
if you just want to output to a file, declare the stream as following: ofstream outfile; //outfile is the object name and it can be anything if you just want to input to a file, declare the stream as following: ifstream infile; //infile is the object name and it can be anything if you want to use the same file for both input and output, declare it as following: fstream file; //file is just an object name it could be anything i am giving you a small code snippet for reading in from a file since you require that in your program: CPP / C++ / C Code:
if you need more info on file i/o in c++, just search your fav search engine for fstream and you will find some good websites which show you how to do more things by using the wide variety of functions that are associated with fstream. Also look for getline function associated with reading in from files. It might be of use to you since you need to read a text file with multiple lines |
|
#3
|
|||
|
|||
|
Ok, i'm starting to understand a bit. I'm encountering some problems when using the parameters from this function though:
char** getWord (const char* file, char* cadena) when I use "return Cadena" it says I can't covert from "char *" to "char **" |
|
#4
|
||||
|
||||
|
Quote:
Is CPP / C++ / C Code:
If so, what do you want to return? Right now you have defined it to return a char** which is a pointer to a pointer of chars (perhaps a list of strings?) and when you tell it to return cadena, you are trying to return a pointer to chars (char*). That is the reason that you are getting this error. Before you fix this, you need to determine what it is that you want to return. If it is simply a char * (string) then just define your function to return a char *. HTH, d |
Recent GIDBlog
Halfway done! by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Mozilla Thunderbird | dsmith | Computer Software Forum - Linux | 9 | 01-Mar-2005 11:56 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 10:53 |
| Linux Kernel Upgrade Mini Howto | dsmith | Computer Software Forum - Linux | 3 | 05-Apr-2004 22:10 |
| Re: Programming Techniques | WaltP | C Programming Language | 0 | 09-Mar-2004 23:56 |
| File copy routine error! | janet | C Programming Language | 2 | 06-Mar-2004 11:03 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The