By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

How to load the whole file?

P: n/a
Hi,

Well, I want to read the contents of file in a character array.
Actually, I am doing this for Hangman(game). I want to read the
contents of file in a character array, so that I can pick a word out
of it and then generate dashes(-) at it's place. Can anyone please
guide me on this one?

Mar 23 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In article <11**********************@l75g2000hse.googlegroups .com>,
<Fi************@gmail.comwrote:
>Well, I want to read the contents of file in a character array.
Actually, I am doing this for Hangman(game). I want to read the
contents of file in a character array, so that I can pick a word out
of it and then generate dashes(-) at it's place. Can anyone please
guide me on this one?
You cannot do it all at one time in C, as there is no C input
operation that will allocate memory and read the data at the same time.

What you will need to do is read a chunk of the file, allocate
enough dynamic memory to hold what you just read, and loop back
for more until finally one of the reads tells you that it reached
the end of the file. Watch out for very long words in the input
file! And you will have to figure out how to keep track of all
those chunks of dynamic memory that you allocated...
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Mar 23 '07 #2

P: n/a
On Mar 23, 11:54 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1174673995.436136.134...@l75g2000hse.googlegroups .com>,

<Finger.Octo...@gmail.comwrote:
Well, I want to read the contents of file in a character array.
Actually, I am doing this for Hangman(game). I want to read the
contents of file in a character array, so that I can pick a word out
of it and then generate dashes(-) at it's place. Can anyone please
guide me on this one?

You cannot do it all at one time in C, as there is no C input
operation that will allocate memory and read the data at the same time.

What you will need to do is read a chunk of the file, allocate
enough dynamic memory to hold what you just read, and loop back
for more until finally one of the reads tells you that it reached
the end of the file. Watch out for very long words in the input
file! And you will have to figure out how to keep track of all
those chunks of dynamic memory that you allocated...
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell

Well, I cant even figure out to randomly pick a word out of file, I
tried with fseek() but nope, I can't just go around file
vertically ... :'(

Mar 23 '07 #3

P: n/a
On Fri, 23 Mar 2007 18:54:11 +0000 (UTC), Walter Roberson wrote:
>In article <11**********************@l75g2000hse.googlegroups .com>,
<Finger.Octopus@...comwrote:
>>Well, I want to read the contents of file in a character array.

You cannot do it all at one time in C, as there is no C input
operation that will allocate memory and read the data at the same time.
fseek, ftell, malloc??

Mar 23 '07 #4

P: n/a
In article <46*************@news.utanet.at>,
Roland Pibinger <rp*****@yahoo.comwrote:
>On Fri, 23 Mar 2007 18:54:11 +0000 (UTC), Walter Roberson wrote:
>>In article <11**********************@l75g2000hse.googlegroups .com>,
<Finger.Octopus@...comwrote:
>>>Well, I want to read the contents of file in a character array.

You cannot do it all at one time in C, as there is no C input
operation that will allocate memory and read the data at the same time.

fseek, ftell, malloc??
That presumes that seeking is possible on the input. It also presumes
that the input is a binary stream, as ftell returns an opaque
token on text streams. Of course with binary streams, you have to
do your own line termination conversions if the input was
written as a text stream. And you cannot just fseek to the end of
file on a binary stream (or at least implementations are not
required to support WHENCE_END for binary streams.)

--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Mar 23 '07 #5

P: n/a
On Mar 23, 11:19 am, Finger.Octo...@gmail.com wrote:
Hi,

Well, I want to read the contents of file in a character array.
Actually, I am doing this for Hangman(game). I want to read the
contents of file in a character array, so that I can pick a word out
of it and then generate dashes(-) at it's place. Can anyone please
guide me on this one?
Read the wordlist into an array.
Pick a random number between 1 and the number of items read. The C-
FAQ tells how to do this.
Choose the word at that location in the array.
It will be a pain in the wazonga if you try to seek around in the
file.
If there is too much data to load into memory, you can read the file
once to find out how many things there are.
Then, choose a random number between 1 and the number of items that
you read.
Finally, discard (that number - 1) inputs and keep the next one.

Mar 23 '07 #6

P: n/a

<Fi************@gmail.comwrote in message
news:11**********************@l75g2000hse.googlegr oups.com...
Hi,

Well, I want to read the contents of file in a character array.
Actually, I am doing this for Hangman(game). I want to read the
contents of file in a character array, so that I can pick a word out
of it and then generate dashes(-) at it's place. Can anyone please
guide me on this one?
This will read a file. It is good enough for general use though not up to
the high standards of comp.lang.c, because of the use of ftell to find the
length of the file.

/*
function to slurp in an ASCII file
Params: path - path to file
Returns: malloced string containing whole file
*/
char *loadfile(char *path)
{
FILE *fp;
int ch;
long i = 0;
long size = 0;
char *answer;

fp = fopen(path, "r");
if(!fp)
{
printf("Can't open %s\n", path);
return 0;
}

fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);

answer = malloc(size + 100);
if(!answer)
{
printf("Out of memory\n");
fclose(fp);
return 0;
}

while( (ch = fgetc(fp)) != EOF)
answer[i++] = ch;

answer[i++] = 0;

fclose(fp);

return answer;
}

To pick out a word, look for the first alphabetical character. Then read a
span of alphabetical characters until you hit a non-alphabetical one. Don't
forget to null-terminate your return array. Set a sensible limit such as 64
characters, and don't exceed that, so that some malicious person can't crash
your program by constructing a file with over-long words.
Mar 23 '07 #7

P: n/a
Walter Roberson wrote:
<Fi************@gmail.comwrote:
>Well, I want to read the contents of file in a character array.
Actually, I am doing this for Hangman(game). I want to read the
contents of file in a character array, so that I can pick a word
out of it and then generate dashes(-) at it's place. Can anyone
please guide me on this one?

You cannot do it all at one time in C, as there is no C input
operation that will allocate memory and read the data at the same
time.
Correction - there is no standard library routine to do this.
However, among others, there is ggets (written in purely standard C
and thus portable). See:

<http://cbfalconer.home.att.net/download/>

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 24 '07 #8

P: n/a
In article <11**********************@y80g2000hsf.googlegroups .com>,
user923005 <dc*****@connx.comwrote:
>If there is too much data to load into memory, you can read the file
once to find out how many things there are.
Then, choose a random number between 1 and the number of items that
you read.
Finally, discard (that number - 1) inputs and keep the next one.
See also <39***************@eton.powernet.co.uk>, posted in response to
code I posted that did that.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Why should we believe you are capable of producing valid software in a
book, when you appear incapable of it in Usenet?
--Richard Heathfield in comp.programming
Mar 25 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.