Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Copying file lines into an array [ newbie ]

Reply
Thread Tools

Copying file lines into an array [ newbie ]

 
 
inyc163
Guest
Posts: n/a
 
      05-25-2004
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[i], line); */
/* or */
/* lineptr[i] = line; */
}

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

return 0;
}
-- code end --
 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-25-2004
In 'comp.lang.c', http://www.velocityreviews.com/forums/(E-Mail Removed) (inyc163) wrote:

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

> int i;


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[i], line); */
> /* or */
> /* lineptr[i] = 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[i]);
>
> return 0;
> }


--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
 
 
 
Darrell Grainger
Guest
Posts: n/a
 
      05-25-2004
On Tue, 25 May 2004, inyc163 wrote:

> 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[i], line); */
> /* or */
> /* lineptr[i] = line; */
> }


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

lineptr[i] = line;

then lineptr[i] 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[i] variables will point at the same
place. That place will hold the last line read from the file.

Using the:

strcpy(lineptr[i], line);

is bad. This tells the compiler to copy the contents of line into the
memory location lineptr[i] references. WAIT! You have not assigned a valid
address to lineptr[i]. 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[i] 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[i]);


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

> return 0;
> }
> -- code end --
>


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to (E-Mail Removed)
 
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
Reading lines from a file into an array Jim Burgess Ruby 2 05-01-2010 01:32 PM
reading file and copying it into array... Shuch C++ 15 04-01-2006 11:55 PM
Copying file lines into an array [ newbie ] jj C Programming 4 05-24-2004 05:19 PM
Reading Blank Lines into StringTokens into Vector Array Row Java 0 04-14-2004 07:12 AM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments