How to load the whole file?

F

Finger.Octopus

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?
 
W

Walter Roberson

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...
 
F

Finger.Octopus

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...


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 ... :'(
 
R

Roland Pibinger

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??
 
W

Walter Roberson

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.)
 
U

user923005

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.
 
M

Malcolm McLean

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.
 
D

Dave Vandervies

user923005 said:
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 <[email protected]>, posted in response to
code I posted that did that.


dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top