0

I've read everything about this and still i haven't figured out how to do this. I'm trying to read a .txt line by line and put that in an array. I think my code kinda works but when i try to print the array it doesn't let me.

#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
int main(){
    char s[20],*list[196][20];
    int i;
    FILE *lista;
    lista=fopen("lista.txt","r");
    i=0;
    while(feof==0){
        getline(list[i],0,lista);
        i=i+1;
        printf("%s\n",list[i]);
    }
    for(i=0;i<195;i++){
        //printf("%s\n",list[i]);
    }
    fclose(lista);
}

Reason why is there 2 prints is because i tried both ways to see if i could fix it. Any idea on what am i doing wrong? The error i get is "format '%s' expects argument of type 'char *' but argument 2 has type 'char**'

  • We need your definition of `getline`. Ah.. I see. Linux non-standard library extension. About identical to `fgets` except for the optional memory allocation. – Paul Ogilvie Nov 14 '16 at 00:40
  • @PaulOgilvie [`geline()` is POSIX.](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html) – Andrew Henle Nov 14 '16 at 01:32

3 Answers3

0

What is your reason to declare list as array 196 of array 20 of pointer to char? The code seems more reliable.

int main ( void )
{
   static const char filename[] = "lista.txt";
   FILE *file = fopen ( filename, "r" );
   char list[196][20];
   int i = 0;
   if ( file != NULL )
   {
      char line [ 128 ]; /* or other suitable maximum line size */
      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
         strcpy( list[i], line );  /* copy the line into list */
         fputs ( line, stdout ); /* write the line */
         ++i;
      }
      fclose ( file );
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
   }
   return 0;
}
  • Thank you so much!! your code works. Altough, when i print the array it prints two times, i don't know what's up with that. – user7151467 Nov 14 '16 at 00:00
  • I used an array [196][20] because my .txt has 196 words. I used *list because getline needed it – user7151467 Nov 14 '16 at 00:02
  • When you want it as pointer, you have to hold some place to put your stuff in it. So, no space has been allocated. I don't know why it prints twice however likely about implementation of getline function. En passant, if an answer helps you, you should accept it by clicking the tique. – Soner from The Ottoman Empire Nov 14 '16 at 08:16
0

getline() is a standard POSIX function.

There are several problems with your code.

First, you're using getline() wrong. By passing the address of your fixed-size array element and zero as its size, getline() will likely try to realloc() your pointer, and that won't work.

This will work, as long as there are enough lines in the file:

#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
int main(){
    char *list[196];
    size_t bytes[196];

    FILE *lista;
    lista=fopen("lista.txt","r");
    if ( null == lista )
    {
        return( -1 );
    }

    // while(feof==0) is *so* wrong...
    for ( int i = 0; i < 196; i++){
        list[i] = NULL;
        bytes[i] = 0;
        // note that the address of list[i] and bytes[i] are
        // passed - if you examine them after the call, you'll
        // see they changed value.  Since C is pass-by-value,
        // the address must be passed to allow the variable in
        // main to be modified by the getline() call
        ssize_t bytes_read = getline(&list[i],&bytes[i],lista);
        if ( bytes_read <= 0 )
        {
            break;
        }

        printf("%s\n",list[i]);
    }

    fclose(lista);
    return(0);
}

And while(feof==0) is wrong on many levels. The bare feof evaluates to the address of the feof() function, and that will never be equal to zero. Assuming you meant while (feof(lista)==0), that's wrong, too. See Why is “while ( !feof (file) )” always wrong? for why.

Community
  • 1
  • 1
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
-1

With Your approach try sth like:

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

#define MAX_LINES_NO 4096
int main(){
    char *list[MAX_LINES_NO];
    int i = 0;
    FILE *lista;
    lista=fopen("lista.txt","r");
    size_t len = 0;
    for(i=0; i< MAX_LINES_NO; ++i) list[i] = 0;
    i=0;
    while(-1 != getline(&list[i],&len,lista)){
    printf("%s\n",list[i]);
    ++i;
}

    for(i=0; i< MAX_LINES_NO; ++i) free(list[i]);

fclose(lista);
}

In this approach You can change MAX_LINES_NO to dynamicaly allocate array of pointers. Note that this approach rely on getline specific behaviour when buffer and len is 0, (it dynamically allocates memory: http://man7.org/linux/man-pages/man3/getdelim.3.html).

Mateusz Wojtczak
  • 1,621
  • 1
  • 12
  • 28