Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Learning pointers and arrays of pointers With Files

Reply
Thread Tools

Learning pointers and arrays of pointers With Files

 
 
ketema@gmail.com
Guest
Posts: n/a
 
      03-27-2005
Hello, I was wondering if someone could help me with a function I am
trying to write. The purpose of the function is to read in text from a
file in the following format:

FIRSTNAME LASTNAME SCORE SCORE SCORE SCORE

example:

Karen Smith 100 100 100 100
John Oliver 78 90 65 51
Henry Green 44 55 44 44

I basically want to pass the number of lines or records, a file
pointer, a pointer to a char array, a pointer to another char array,
and a pointer to an int array to a function that will read all the file
input up to the first space into the first element of the first char
array, then all the next characters up to the second space into the
first element of the second char array, then all the integers into the
first element of the int array. This is what I have so far:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void readLines(int numLines, FILE *, char *firstNames[], char
*lastNames[], int *scores[]);

//variables
FILE *inputFile;
int numRecords = 0;

...get number of records from user...

char *firstNames[numRecords]; //array of pointers
char *lastNames[numRecords];
int *scores[4]; //We know there are always 4 for each line
scores
inputFile = fopen("input.txt", "r"); //open the input file

readLines(numRecords, inputFile, firstNames, lastNames,
scores);

void readLines(int numLines, FILE *file, char *firstNames[], char
*lastNames[], int *scores[]){
int i = 0;

for(i = 0; i < numLines; i++){
while(firstNames[i] = (getc(FILE *file)) ){//this is
where i need
help...
want to read placing each char into firstNames[i]
until i hit a space,then
start putting chars into lastNames[i] until i hit
the next space then read each int placing
each one into a spot in scores[0]

}
}

}

Thanks for the feedback
Ketema

 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      03-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello, I was wondering if someone could help me with a function I am
> trying to write. The purpose of the function is to read in text from a
> file in the following format:


> FIRSTNAME LASTNAME SCORE SCORE SCORE SCORE


> example:


> Karen Smith 100 100 100 100
> John Oliver 78 90 65 51
> Henry Green 44 55 44 44


> I basically want to pass the number of lines or records, a file
> pointer, a pointer to a char array, a pointer to another char array,
> and a pointer to an int array to a function that will read all the file
> input up to the first space into the first element of the first char
> array, then all the next characters up to the second space into the
> first element of the second char array,


What about people with names like "George W. Bush"?

> then all the integers into the
> first element of the int array. This is what I have so far:


> #include <stdio.h>
> #include <ctype.h>
> #include <string.h>


> void readLines(int numLines, FILE *, char *firstNames[], char
> *lastNames[], int *scores[]);


I guess this was meant to be

int main( void )

or maybe

int main( int argc, char *argv[ ] )

> //variables
> FILE *inputFile;
> int numRecords = 0;


> ...get number of records from user...


It might be a better idea to iterate over the file to find out how
many lines are there instead of believing the user...

> char *firstNames[numRecords]; //array of pointers


This won't work unless you have a C99 compliant compiler - with
C89 there where no variables sized arrays, the size has to be a
compile time constant.

And just creating an array of pointers won't work. All these pointers
are uninitialized, i.e. they point to some random memory locations,
not to memory you can use. Before you can write strings to where
the pointers point to you have to obtain memory from the system
using e.g. malloc().

> char *lastNames[numRecords];
> int *scores[4]; //We know there are always 4 for each line
> scores


But than you would need numRecords times 4 integers, not 4 pointers.

> inputFile = fopen("input.txt", "r"); //open the input file


You forget to check if fopen() failed.

> readLines(numRecords, inputFile, firstNames, lastNames,
> scores);


> void readLines(int numLines, FILE *file, char *firstNames[], char
> *lastNames[], int *scores[]){
> int i = 0;


> for(i = 0; i < numLines; i++){
> while(firstNames[i] = (getc(FILE *file)) ){//this is


I guess you mean "getc(file)" here. But anyway, firstNames is an
array of pointers, so "firstNames[i]" is the i-th pointer, not some-
thing were you could store a character. And getc() returns an int,
not a char, since EOF can't be represented as a char (and 0 is not
a return value that tells you that EOF has been reached as you seem
to assume, (char) '\0' is a valid charcter, but which you won't find
in a text file).

> where i need
> help...
> want to read placing each char into firstNames[i]
> until i hit a space,then
> start putting chars into lastNames[i] until i hit
> the next space then read each int placing
> each one into a spot in scores[0]


Sorry, but all of this is that wrong that I would really recommend
that you start with some simpler things for learning about pointers.
As the next step find out how the functions malloc(), realloc() and
free() are used. When you have mastered that you may be ready for
trying something like this here. I can tell you it's quite tricky
to get it right for all cases, and without a clearer understanding
of pointers and memory allocation I fear you won't be able to do
that.

A task which might help you understand quite a bit would be writing
a function that reads in an arbitraty long line from a file passed
to the function, returning a pointer to the string. Such a function
could be a building block for your program and would get you fami-
liar with the functions for memory allocation. Here's a way it it
might be done:

1) Define a char pointer
2) Allocate a certain amount of memory, e.g. 128 characters (some-
thing long enough for typical lines), using malloc() and assign
the result to the char pointer. Don't forget to check the return
value of malloc().
3) Read characters from the file, putting them into the memory poin-
ted to by the char pointer. Keep track of how many you read and
how long the buffer is the char pointer is pointing to.
4) If there are no more characters go to 6
5) If the memory you got is filled up use realloc() to double the
amount of memory the char pointer is pointing to and continue
with 3. Don't forget to check the return value of realloc()
and make sure you have a backup of the original value of the
pointer in case realloc() fails.
6) Put a '\0' after the last character in the buffer pointed to by
the char pointer. Make sure there's still enough place in the
buffer for the '\0' character.
7) Use realloc() to cut back on the size of the buffer so that only
that much space is used as is absolutely necessary.
Return the char pointer.

Of course, the calling function must later free() the pointer.
Put that prominently into the documentation of the function.
Test the function carefully to check that all corner cases are
treated correctly and you never write past the end of the memory
you allocated - if you get it wrong it might seem to work at first
but then fail mysteriously sometime later or even lead to errors
in seemingly unrelated places!

Once you mastered this I guess you will have a pretty good idea
how to deal with several input lines, each having several entries
(consider using double pointers for the first and last names if
you forego using variable sized arrays). And you could use that
function (perhaps after a few minor modifications) for that.

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
casting pointers/arrays to multidimensional arrays Francesco C++ 2 11-06-2009 09:04 AM
Early beginner learning arrays and for and while loops ... bpascal123@googlemail.com C Programming 33 07-27-2009 07:15 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Learning C and Learning Make/Configure/Building/Linking Hal Vaughan C Programming 7 03-21-2006 05:07 PM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM



Advertisments