Copying file lines into an array [ newbie ]

I

inyc163

Hi,

I would like to read a file 1 line at a time and storing the lines
into a char array. (using fgets).
I thought something like the code below would work but I am having
problems.

Any help would be appreciated.

Thanks.

jj

-- code start --
#include <stdio.h>
#include <string.h>
#include <errno.h>

#define LINESIZE 100
#define MAXLINE 100

int
main(int argc, char **argv)
{
/* i would like to store the file there */
char *lineptr[MAXLINE];

FILE *fp;
fp = fopen("testfile", "r");

if ( fp == NULL ) {
printf("Unable to openfile");
exit(1);
}

char line[LINESIZE];
int i;
for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
printf("%s",line);
/* here i would like to do something */
/* like */
/* strcpy(lineptr, line); */
/* or */
/* lineptr = line; */
}

/* to be able to use it like this */
for(i=0; i<MAXLINE;i++)
printf("%s",lineptr);

return 0;
}
-- code end --
 
E

Emmanuel Delahaye

In said:
I would like to read a file 1 line at a time and storing the lines
into a char array. (using fgets).
I thought something like the code below would work but I am having
problems.

Any help would be appreciated.

Thanks.

jj

-- code start --
#include <stdio.h>
#include <string.h>
#include <errno.h>

#define LINESIZE 100
#define MAXLINE 100

int
main(int argc, char **argv)
{
/* i would like to store the file there */
char *lineptr[MAXLINE];

Better to have all the pointers set to NULL before starting. It makes things
clear.

char *lineptr[MAXLINE] = {0};
FILE *fp;
fp = fopen("testfile", "r");

if ( fp == NULL ) {
printf("Unable to openfile");
exit(1);

The standard values for exit() and return from main() are 0, EXIT_SUCCESS and
EXIT_FAILURE.
}

char line[LINESIZE];

Keep in mind that in C90, local variables must be defined a the top of a
block. Not every compiler supports C99.

The good type for an index is size_t.
for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
printf("%s",line);
/* here i would like to do something */
/* like */
/* strcpy(lineptr, line); */
/* or */
/* lineptr = line; */


You need some malloc() + strcpy(). Some implementations have a function
called strdup() (POSIX standard, BTW) that does the job.

You also must be careful not to write after lineptr[MAXLINE - 1]. You need an
extra test.
}

/* to be able to use it like this */
for(i=0; i<MAXLINE;i++)
printf("%s",lineptr);

return 0;
}
 
D

Darrell Grainger

Hi,

I would like to read a file 1 line at a time and storing the lines
into a char array. (using fgets).
I thought something like the code below would work but I am having
problems.

Any help would be appreciated.

Thanks.

jj

-- code start --
#include <stdio.h>
#include <string.h>
#include <errno.h>

#define LINESIZE 100
#define MAXLINE 100

int
main(int argc, char **argv)
{
/* i would like to store the file there */
char *lineptr[MAXLINE];

This is 100 pointers. They have not been initialized so they point at
random memory locations. They need to be assigned a value before you can
use them.
FILE *fp;
fp = fopen("testfile", "r");

if ( fp == NULL ) {
printf("Unable to openfile");
exit(1);
}

char line[LINESIZE];
int i;
for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
printf("%s",line);
/* here i would like to do something */
/* like */
/* strcpy(lineptr, line); */
/* or */
/* lineptr = line; */
}


The variable lineptr is a pointer. The array line exists somewhere in
memory. If you use:

lineptr = line;

then lineptr will point at the memory of line. This is okay but not
what you want. The reason, every time you do the fgets() it will overwrite
the contents of line. All the lineptr variables will point at the same
place. That place will hold the last line read from the file.

Using the:

strcpy(lineptr, line);

is bad. This tells the compiler to copy the contents of line into the
memory location lineptr references. WAIT! You have not assigned a valid
address to lineptr. You are copying the line to a random memory
location. That is not good. You could crash the program.

The solution is to allocate memory for lineptr to reference THEN use
the strcpy() function to copy from line to the newly allocated memory. See
the malloc() function.
/* to be able to use it like this */
for(i=0; i<MAXLINE;i++)
printf("%s",lineptr);


Once you are done with the memory you want to use the free() function
here to release the memory.
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top