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