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

R

Randy Lovelace

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

This does not work. Thx in advance

Randy Lovelace
 
M

Michael Mair

Randy said:
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
 
J

Jens.Toerring

Randy Lovelace said:
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
 
S

Simon Biber

Randy said:
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.
 
G

graham.rick

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!
 
E

Emmanuel Delahaye

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

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top