Learning pointers and arrays of pointers With Files

Discussion in 'C Programming' started by ketema@gmail.com, Mar 28, 2005.

  1. Guest

    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 = (getc(FILE *file)) ){//this is
    where i need
    help...
    want to read placing each char into firstNames
    until i hit a space,then
    start putting chars into lastNames until i hit
    the next space then read each int placing
    each one into a spot in scores[0]

    }
    }

    }

    Thanks for the feedback
    Ketema
     
    , Mar 28, 2005
    #1
    1. Advertising

  2. -berlin.de Guest

    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 = (getc(FILE *file)) ){//this is


    I guess you mean "getc(file)" here. But anyway, firstNames is an
    array of pointers, so "firstNames" 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
    > until i hit a space,then
    > start putting chars into lastNames 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.
    8) 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Mar 28, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    390
  2. Hal Vaughan
    Replies:
    7
    Views:
    494
  3. Philipp
    Replies:
    21
    Views:
    1,157
    Philipp
    Jan 20, 2009
  4. Francesco
    Replies:
    2
    Views:
    1,127
    Francesco
    Nov 6, 2009
  5. Andrey Popp

    [I'm learning C]: Learning to use ucontext

    Andrey Popp, Jan 29, 2012, in forum: C Programming
    Replies:
    5
    Views:
    769
    Keith Thompson
    Jan 31, 2012
Loading...

Share This Page