Copying file lines into an array [ newbie ]

J

jj

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

Thomas Matthews

jj said:
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>
#include said:
#define LINESIZE 100
#define MAXLINE 100

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

You are creating an array of pointers here.
Not necessarily a huge block of memory to store the
content of the strings in.
FILE *fp;
unsigned int lines_in_memory = 0;
fp = fopen("testfile", "r");

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

char line[LINESIZE];
int i;

These definitions need to be moved up.
Remember that "line" is a temporary.
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 to allocate space from dynamic memory for the
new string, then copy the data from the temporary
variable, "line" into the new string:
lineptr = malloc(strlen(line) + 1);
if (!lineptr)
{
/* Handle memory allocation error here. */
}
strcpy(lineptr, line);
lines_in_memory = i;
/* to be able to use it like this */
for(i=0; i<MAXLINE;i++)
printf("%s",lineptr);

for (i = 0; i < lines_in_memory; ++i)
{
printf("%5d: %s\n", i, lineptr);
}
return 0;
/* A proper program cleans up after itself. */
for (i = 0; i < lines_in_memory; ++i)
{
free(lineptr); /* every malloc needs a free. */
}
return EXIT_SUCCESS;
}
-- code end --


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
A

Al Bowers

jj said:
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.

For a simple, quick fix:
-- code start --

#include <stdio.h>
#include <string.h>
Remove string.h. You will not need it.
#include <errno.h>
Remove errno.h. Your code does not require it.
Add header stdlib.h for function exit.
#define LINESIZE 100
#define MAXLINE 100

int
main(int argc, char **argv)
int main(void) would do since you are not using
any command line arguments in the code shown.
{
/* i would like to store the file there */
char *lineptr[MAXLINE];

Make this:
char lineptr[MAXLINE][MAXSIZE];
FILE *fp;
fp = fopen("testfile", "r");

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

char line[LINESIZE];
You will not need and the array line. You can copy, using fgets,
directly into the lineptr array. So you also be able to remove
strcpy from the code.
Add a variable to keep track of the number of lines copied.
int numlines;
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; */
}

Replace the above for expression with one that copies
directly into lineptr. ie.
for(numlines = 0; numlines < MAXLINE &&
(fgets(lineptr[numlines],LINESIZE,fp)); numlines++) ;

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


for(i = 0; i < numlines; i++)
puts(lineptr);
return 0;
}
-- code end --

Corrected, the code becomes.

#include <stdio.h>
#include <stdlib.h>

#define LINESIZE 100
#define MAXLINE 100

int main(void)
{
char lineptr[MAXLINE][LINESIZE];
int i, numlines;
FILE *fp;

fp = fopen("test.txt", "r");
if ( fp == NULL )
{
printf("Unable to openfile");
exit(1);
}
for (numlines = 0; numlines < MAXLINE &&
(fgets(lineptr[numlines], LINESIZE, fp)); numlines++) ;
fclose(fp);
for(i=0; i<numlines;i++)
puts(lineptr);
return 0;
}
 
L

Luis Diego

char *lineptr[MAXLINE];

With this declaration you have a pointer to an array and each field of the
array is an "char *"

The problem is that each field in lineptr array must be allocated (because
they are pointers).
So you need to change your code:
char *lineptr[MAXLINE];
...
...
...

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; */
}



char lineptr[MAXLINE][LINESIZE];

for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++)
{
printf("%s",line);
strcpy(lineptr, line);

/*The following commente line can't be used, would be a big mistake*/
/* lineptr = line; */
}

I don't promise it would works AS IS.

I strongly recommend "The C Programming Language" book from BW Kernighan and
DM Ritchie, great book to start with C.


Ing. Luis Diego Bolanos Quiros
NPY & PE Dept.
(e-mail address removed)
Inet 8-256-6536
Office(506) 298-8939
Cell (506) 817-6566


jj said:
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 --
 
L

Luis Diego

Upps, I almost forget it.

It is a very ineficent solution, there is a better way to do this.
First
Like using malloc function and eliminate the matrix of char's.

Again, read the bible of C

Luis Diego said:
char *lineptr[MAXLINE];

With this declaration you have a pointer to an array and each field of the
array is an "char *"

The problem is that each field in lineptr array must be allocated (because
they are pointers).
So you need to change your code:
char *lineptr[MAXLINE];
...
...
...

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; */
}



char lineptr[MAXLINE][LINESIZE];

for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++)
{
printf("%s",line);
strcpy(lineptr, line);

/*The following commente line can't be used, would be a big mistake*/
/* lineptr = line; */
}

I don't promise it would works AS IS.

I strongly recommend "The C Programming Language" book from BW Kernighan and
DM Ritchie, great book to start with C.


Ing. Luis Diego Bolanos Quiros
NPY & PE Dept.
(e-mail address removed)
Inet 8-256-6536
Office(506) 298-8939
Cell (506) 817-6566


jj said:
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 --

 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top