C
Clunixchit
How can i read lines of a file and place each line read in an array?
for exemple;
array[0]=line1
array[1]=line2
...
for exemple;
array[0]=line1
array[1]=line2
...
How can i read lines of a file and place each line read in an array?
for exemple;
array[0]=line1
array[1]=line2
here is my code :
if ( !(phrase.T[phrase.N] = malloc(sizeof (phrase.T[phrase.N]) ))) {
in the endProgram has been terminated receiving signal 6 (Aborted)
im having a *** glibc detected *** corrupted double-linked list:
0x0804b error.
here is my code :
typedef struct new_phrase {
int N; // number of words
char **T; // words stored in an array
} new_phrase;
new_phrase file_array (new_phrase phrase) {
phrase.N=0;
FILE *stream;
char tmp[LINE_LENGTH];
/* Open the file. If NULL is returned there was an error */
if ( !(stream = fopen( MYAIRC , "r"))) {
perror("fopen");
_exit(EXIT_FAILURE);
}
(void) fseek(stream,0,SEEK_SET);
if ( !(phrase.T = malloc ( sizeof (phrase.T)))) {
perror("malloc");
_exit(EXIT_FAILURE);
}
// creating array > phrase.T
while ( fgets ( tmp , LINE_LENGTH-1 , stream ) != NULL
) {
size_t l = strlen(tmp);
if (l > 0 && tmp[l-1] == '\n')
tmp[l-1] = '\0';
if ( !(phrase.T[phrase.N] = malloc ( sizeof
(phrase.T[phrase.N]) ))) {
perror("malloc");
_exit(EXIT_FAILURE);
}
phrase.T[phrase.N]=tmp;
printf("phrase.T %s
%d\n",phrase.T[phrase.N],phrase.N);
phrase.N++;
}
fclose (stream);
if ( phrase.N == 0 ) {
printf("%s is empty\n",MYAIRC);
_exit(EXIT_FAILURE);
}
return phrase;
}
ive used the fgets mentioned above.
this code allows to fill the array but return the error
*** glibc detected *** corrupted double-linked list: 0x0804b168 ***
any suggestions? or improvements?
Clunixchit said:i can't find whats going wrong with it
here is the full source code:
Just in case anyone cares to bother, here is a legal standard CClunixchit said:i can't find whats going wrong with it
here is the full source code:
here is my code :
[snippage - some slight editing for space]
if ( !(phrase.T[phrase.N] = malloc(sizeof (phrase.T[phrase.N]) ))) {
This line is obviously wrong.
A call to malloc() should always have the general form:
p = malloc(N * sizeof *p)
Clunixchit said:[...]Martin Ambuhlwrote:
This code compiles and with a copy of itself named
"myAIc" runs without error.
have you tested the code at your place? coz im still have the same
glib error
Martin said:[...]
if (!((*phrase).T[(*phrase).N] = malloc(strlen(tmp)))) {
[...]
strcpy((*phrase).T[(*phrase).N], tmp);
Eric said:Martin said:[...]
if (!((*phrase).T[(*phrase).N] = malloc(strlen(tmp)))) {
[...]
strcpy((*phrase).T[(*phrase).N], tmp);
Off-by-one error.
Barry said:On Sat, 18 Jun 2005 12:58:09 +0000 (UTC),
im having a *** glibc detected *** corrupted double-linked list:
0x0804b error.
here is my code :
typedef struct new_phrase {
int N; // number of words
char **T; // words stored in an array
} new_phrase;
new_phrase file_array (new_phrase phrase) {
phrase.N=0;
FILE *stream;
char tmp[LINE_LENGTH];
/* Open the file. If NULL is returned there was an error */
if ( !(stream = fopen( MYAIRC , "r"))) {
perror("fopen");
_exit(EXIT_FAILURE);
What is _exit? Is there some reason the standard function won't work
for you so others could test your code?
new_phrase *phrase = malloc (sizeof *phrase);//8
(*phrase).N = EMPTY;
(*phrase).T = malloc ( sizeof (*(*phrase).T) );
I got my code working only if i create another "while".
As u can see i have made modifications with respect to your
suggestions. Ill be happy to receive more
concerning the non-standard libraries, this is because im using fork
and the exec family.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILLED 1
#define EMPTY 0
#define LINE_LENGTH 20
#define MYAIRC "myAIrc"
typedef struct new_phrase {
int N;
char **T;
} new_phrase;
/* initialise my structure by first loading MYAIRC into a
bidimensional array*/
new_phrase * file_array () {
int i=0 , j=0;
char tmp[LINE_LENGTH][LINE_LENGTH];
new_phrase *phrase = malloc (sizeof *phrase);//8
(*phrase).N = EMPTY;
FILE *stream = fopen( MYAIRC , "r" );
if ( !stream ) {
perror("fopen");
exit(EXIT_FAILURE);
}
fseek(stream,0,SEEK_SET);
(*phrase).T = malloc ( sizeof
(*(*phrase).T) );
while ( fgets ( tmp , LINE_LENGTH , stream )
) {
(*phrase).N=FILLED;
size_t l = strlen(tmp);
if (l > 0 && tmp[l-1] ==
'\n')
tmp[l-1] = '\0';
i++;
}
fclose (stream);
while ( j < i ) {
(*phrase).T[j] = malloc (
strlen(tmp[j]));
strcpy((*phrase).T[j],tmp[j]);
printf("phrase.T %s
%d\n",(*phrase).T[j],j);
j++;
}
return phrase;
}
int main ( ) {
new_phrase *phrase = malloc(sizeof(*phrase));
phrase = file_array();
return 0;
}
Bad comment. It doesn't add any useful information and is wrong innew_phrase *phrase = malloc (sizeof *phrase);//8
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.