How to store strings from a file into an array? Newbie

Discussion in 'C Programming' started by Randy Lovelace, May 21, 2005.

  1. while (fgets(buffer,256,fp)){
    epd[x]= buffer;
    x++;}

    This does not work. Thx in advance

    Randy Lovelace
     
    Randy Lovelace, May 21, 2005
    #1
    1. Advertising

  2. Randy Lovelace

    Michael Mair Guest

    Randy Lovelace wrote:
    > while (fgets(buffer,256,fp)){
    > epd[x]= buffer;
    > x++;}
    >
    > This does not work. Thx in advance
    >
    > Randy Lovelace


    Please provide a _complete_ minimal example if you do not understand
    your problem -- otherwise people round here have to look for their
    crystal balls or have to extract the information they need to help
    you from you in a time-consuming way.
    Apart from that: It is a good idea to write your question also in
    the body of your message...

    Okay, let's guess that the above belongs to some code that compiles:
    char buffer[BUF_SIZE];
    FILE *fp;
    char *epd[NUM_BUFS];
    int x;

    Now, you read in up to 256-1 characters and store the address of
    buffer[0] in epd[x]. Then you increment x.
    Unfortunately, you overwrite the contents of the buffer, so
    you get all elements of epd to contain the same address pointing
    to the same buffer which means that you can only access the information
    which is stored in buffer after the last unsuccessful call to fgets().

    If you want to get the contents of the file into some large char-Buffer,
    consider using fread() (or getc() in a loop). Be careful with buffer
    overruns.

    If something else is your problem, give us a minimal example or
    at least a description of what you want to achieve.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, May 21, 2005
    #2
    1. Advertising

  3. Randy Lovelace

    -berlin.de Guest

    Randy Lovelace <> wrote:
    > while (fgets(buffer,256,fp)){
    > epd[x]= buffer;
    > x++;}


    > This does not work. Thx in advance


    What does not work? And what's epd[x]? If epd is an array of
    char pointers than all of them get set to to the address of
    buffer (if buffer is an array) or what buffer is pointing to
    (if buffer is a pointer), which I guess is not what you want.
    If epd is an array of char arrays then you need to use strcpy()
    instead of an assignment. And what happens if there are more
    lines in your file than epd as elements?

    Show the smallest compilable program that still exhibits your
    problem and describe what you think is the problem. Otherwise
    it's going to be impossible to help you.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, May 21, 2005
    #3
  4. Randy Lovelace

    Simon Biber Guest

    Randy Lovelace wrote:
    > while (fgets(buffer,256,fp)){
    > epd[x]= buffer;
    > x++;}
    >
    > This does not work. Thx in advance


    Try:
    while(fgets(buffer,256,fp)){
    epd[x]= copystr(buffer);
    x++;}

    Where copystr is:

    #include <stdlib.h>
    #include <string.h>

    char *copystr(const char *s)
    {
    char *d = malloc(strlen(s) + 1);
    if(d) strcpy(d, s);
    return d;
    }

    This allocates memory and makes a _copy_ of each input string, so you
    don't just store a bunch of pointers to the same buffer.

    --
    Simon.
     
    Simon Biber, May 21, 2005
    #4
  5. Randy Lovelace

    Guest

    Randy.

    Your example should work, assuming that the code around your try is
    ok. But since there is no code included no one can help you. What is
    buffer? What is fp? One could assume that buffer is declared:
    char buffer[256];
    and that fp is
    FILE * fp
    and that there is a line opening it somewhere. But assumptions don't
    help to debug code.

    Who knows why it doesn't work. Post some code!

    --
    Cheers!
    Rick.
     
    , May 21, 2005
    #5
  6. Randy Lovelace wrote on 21/05/05 :
    > while (fgets(buffer,256,fp)){
    > epd[x]= buffer;
    > x++;}
    >
    > This does not work. Thx in advance


    If you meant this snippet, yes, it doesn't ever compile for zillions of
    reasons.

    Please post the exact compilable code you have a problem with and
    explain what the problem is (better than 'This does not work').

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Clearly your code does not meet the original spec."
    "You are sentenced to 30 lashes with a wet noodle."
    -- Jerry Coffin in a.l.c.c++
     
    Emmanuel Delahaye, May 22, 2005
    #6
    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. =?Utf-8?B?UnVkeQ==?=

    to store or not to store an image

    =?Utf-8?B?UnVkeQ==?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    644
    =?Utf-8?B?UnVkeQ==?=
    Mar 30, 2005
  2. Replies:
    12
    Views:
    697
    Michael Wojcik
    May 19, 2005
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
  4. Sintek
    Replies:
    1
    Views:
    485
    Sintek
    May 3, 2009
  5. Wybo Dekker
    Replies:
    1
    Views:
    185
    Charles Steinman
    Jul 23, 2005
Loading...

Share This Page