473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to load the whole file?

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
8 2520
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
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
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
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
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

<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to...
1
by: Ray in HK | last post by:
What are the differences between LOAD DATA INFILE and LOAD DATA LOCAL INFILE ? I found some web hosting company do not allow using LOAD DATA INFILE but allow LOAD DATA LOCAL INFILE. The reason...
3
by: caroline.fryer | last post by:
I have an xml document which I only want to load part of depending on the element 'Name'. The structure of my xml document looks like this: <FileDefaults> <File> <Name>Name 1</Name>...
12
by: Sharon | last post by:
I’m wrote a small DLL that used the FreeImage.DLL (that can be found at http://www.codeproject.com/bitmap/graphicsuite.asp). I also wrote a small console application in C++ (unmanaged) that uses...
2
by: Eric Falsken | last post by:
Eric Falsken <eric@db4o.com> wrote on 04 Dec 2005: > craigkenisston@hotmail.com wrote on 19 Nov 2005: > >> I'm working in the migration of an asp.net application in 1.1 to 2.0. >> I'm new to...
1
by: urs | last post by:
Two days ago, I built an ASP.NET 2.0 application and published it on a shared IIS 6 Web server. After publishing, and during the whole day, it worked fine. The server remained untouched since....
4
by: Andy B | last post by:
I have 2 web applications: ParentApp and ChildApp. The ChildApp root folder is setup like this: ParentApp/ChildApp. I set the output folder of the ChildApp under the build section in VS2005...
5
by: DR | last post by:
Why is its substantialy slower to load 50GB of gzipped file (20GB gzipped file) then loading 50GB unzipped data? im using System.IO.Compression.GZipStream and its not maxing out the cpu while...
1
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
The description of the XMLDocument.Load method doesn't quite answer the question. When passing in a FileStream object to the Load method, does it load the entire document into memory? For...
1
by: cnb | last post by:
>>a = parsing.unserialize("C:/users/saftarn/desktop/twok.txt") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python25\Progs\NetflixRecSys\parsing.py", line 91, in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.