int readOptions(char *argv[]){
FILE * infile;
char line_buf[BUFSIZ];
int i = 0, j = 0 ;
infile = fopen("options","r");
if(!infile){
fprintf(stderr,"File Read failure\n");
exit(2);
}
while( i < 10 && fgets(line_buf,sizeof(line_buf),infile)!=0){
printf("Line buf : %s",line_buf);
argv[i] = line_buf;
i++;
}
}
int main(){
int j ;
char *options[10];
for(j = 0 ; j< 10 ; j++){
options[j] = malloc(len * sizeof (char));
}
readOptions(options);
for(j=0; j<10 ; j++)
printf("%s %d\n",options[j], j );
}
The problem is that I always see – the program print only the last line read in the file. Where is the mistake ? and am I missing any important pointer concept with this code ?
Every element of
argvpoints to the sameline_buf. Usestrdup()to create new strings instead (which you will later have to free).