GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-May-2004, 15:35
Carlo Carlo is offline
New Member
 
Join Date: May 2004
Posts: 2
Carlo is on a distinguished road
Question

Read file and copy to dinamic array


I 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  
Old 10-May-2004, 16:22
machinated machinated is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
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:
/**********************Reading from a file *****************/

#include<fstream>//file io
#include<iostream>
using namespace std;


int main()
{
	ifstream infile("mytext.txt");/*declare the stream object with the 
file name passed as a paramter to the constructor. mytext.txt contains 10 characters*/
	char *ch;//dynamic array


	ch = new char[11];//assign memory
	infile.read(ch,10);//will read first 10 characters from a text file
	ch[10]=NULL;//Null terminate the char string
	cout<<ch;//cout will display the entire array till null is encountered
	delete[]ch;//free the memory assigned to ch
	return 0;
}

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  
Old 10-May-2004, 17:00
Carlo Carlo is offline
New Member
 
Join Date: May 2004
Posts: 2
Carlo is on a distinguished road
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  
Old 10-May-2004, 19:28
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by Carlo
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 **"

Is
CPP / C++ / C Code:
char** getWord(const char* file, char* cadena)
a function that you are writing?

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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Kernel Upgrade Mini Howto dsmith Computer Software Forum - Linux 4 31-Mar-2011 13:45
Mozilla Thunderbird dsmith Computer Software Forum - Linux 9 01-Mar-2005 12:56
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56
File copy routine error! janet C Programming Language 2 06-Mar-2004 12:03

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:18.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.