0

Hello i want to dynamically initialize an array based on a text file, but for some reason im doing it wrong. i get an error at line "malloc" that the "texto" is not being initialized.

char nome[] = "partidas.txt";
f = fopen(nome, "rt");
int size = fsize(f);

char **texto;
**texto = (char)malloc(size);

int i = 0;
while ((fgets(texto[i], sizeof(texto), f) != NULL))
{
  printf("%s\n", texto[i++]);
} 
  • 4
    You can't cast malloc to a character because it returns a pointer. Why do you want texto to be a pointer to a character pointer (char **) anyway? Assuming you do you need to make that line `texto = malloc(size);` and then you need a loop to malloc all the pointers in the array you just created. – Jerry Jeremiah May 18 '14 at 14:33
  • 1
    Do you want to read an array of strings from the file? – James M May 18 '14 at 14:33
  • 1
    @JerryJeremiah i use **texto cause i want a dynamic char texto[][], basically i just need to put the size of txt file in texto and then fill it with data. –  May 18 '14 at 14:58
  • 2
    And where exactly do you allocate the actually memory you're reading your *string data* to? You allocate memory to hold a stack of pointers; that's it. No buffer space for each string. and `fsize()` is neither a POSIX nor C standard function so we have no idea whether that even *works* without seeing it. And, of course, [**Don't Cast `malloc` in C programs**](http://stackoverflow.com/a/605858/1322972). – WhozCraig May 18 '14 at 17:08
  • I see what is going on. fsize probably returns the number of characters in the file and he wanted to load the entire file into a char [][] and it might work if the newlines are replaced with null characters except that for a single malloc each line would have to be the same length or for an array of pointers you would need to know how many lines. But we don't know either for sure. – Jerry Jeremiah May 18 '14 at 20:34
  • 1
    So how can i dynamically do it? –  May 19 '14 at 22:35
  • 1
    @JerryJeremiah im really lost here.. is there no way to do it? –  May 19 '14 at 22:35

1 Answers1

0
//remember to include the right header files

#include <stdio.h>
#include <string.h>
#include <errno.h>

#define READ_LENGTH 1024

char*      pFileContents = NULL;
int        iContentsIndex = 0;

long int   sz = 0;
FILE*      pFD = NULL;
int        readCount = 0;
int        stat = 0;

// note: all errors are printed on stderr, success is printed on stdout
// to find the size of the file:
// You need to seek to the end of the file and then ask for the position:

pFD = fopen( "filename", "rt" );
if( NULL == pFD )
{
    perror( "\nfopen file for read: %s", strerror(errno) );
    exit(1);
}

stat = fseek(pFD, 0L, SEEK_END);
if( 0 != stat )
{
    perror( "\nfseek to end of file: %s", strerror(errno) );
    exit(2);
}

sz = ftell(pFD);

// You can then seek back to the beginning
// in preparation for reading the file contents:

stat = fseek(pFD, 0L, SEEK_SET);
if( 0 != stat )
{
    perror( "\nfseek to start of file: %s", strerror(errno) );
    exit(2);
}

// Now that we have the size of the file we can allocate the needed memory
// this is a potential problem as there is only so much heap memory
// and a file can be most any size:

pFileContents = malloc( sz );
if( NULL == pFileContents ) 
{ 
    // handle this error and exit
    perror( "\nmalloc failed: %s", strerror(errno) );
    exit(3); 
}


// then you can perform the read loop
// note, the following reads directly into the malloc'd area

while( READ_LENGTH == 
       ( readCount = fread( pFileContents[iContentsIndex], READ_LENGTH, 1, pFD) ) 
     )
{
    iContentsIndex += readCount;
    readCount = 0;
}

if( (iContentsIndex+readCount) != sz )
{
    perror( "\nfread: end of file or read error", strerror(errno) );
    free( pFileContents );
    exit(4);
}

printf( "\nfile read successful\n" );

free( pFileContents );

return(0); 
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • Hello @user3629249, i have some errors on this code. "too many arguments in function call" strerror(errno)
    "a value of type "void *" cannot be assigned to an entity of type "char *"" pFileContents = malloc( sz );
    –  May 24 '14 at 19:01