C: Reading a file into an array?

Up vote 0 down vote favorite share g+ share fb share tw.

I have a text file and I want to to read it line by line and put the lines into an array. The snippet behind gives error while compiling: FILE *f; char lineLINE_SIZE; char *lines; int num_righe; f = fopen("spese. Dat", "r"); if(f == NULL) { f = fopen("spese.

Dat", "w"); } while(fgets(line, LINE_SIZE, f)) { num_righe++; lines = realloc(lines, (sizeof(char)*LINE_SIZE)*num_righe); strcpy(linesnum_righe-1, line); } fclose(f); The error is: spese. C:29: warning: assignment makes integer from pointer without a cast spese. C:30: warning: incompatible implicit declaration of built-in function ‘strcpy’ spese.

C:30: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast Any help? Thanks c link|improve this question asked May 4 '09 at 19:32pistacchio4,414638100 62% accept rate.

You should really include the header #include into your program. Good that you haven't done -fno-builtin, in which case it could not have warned you and the program would have silently been compiled by gcc. – Johannes Schaub - litb May 4 '09 at 19:41 I'm dubious about the value of the if (!f) fopen("spese.

Dat", "w"); part. If that condition happens, f is open for write, and fgets() cannot succeed. Also a little more error checking would be a good thing throughout, especially of realloc() where it will return NULL on failure without freeing the old buffer.

– RBerteig May 5 '09 at 0:53 @RBerteig. I agree, error checking is far from present. – Tom May 5 '09 at 2:05.

Try: FILE *f; char lineLINE_SIZE; char **lines = NULL; int num_righe = 0; f = fopen("spese. Dat", "r"); if(f == NULL) { f = fopen("spese. Dat", "w"); } while(fgets(line, LINE_SIZE, f)) { num_righe++; lines = (char**)realloc(lines, sizeof(char*)*num_righe); linesnum_righe-1 = strdup(line); } fclose(f).

– Tom May 4 '09 at 19:44 Yes. But my code uses sizeof(char*) which in general is 4 bytes, not 1. – Emil H May 4 '09 at 19:44 Oh, didn't notice that *.

I apologize. – Tom May 4 '09 at 19:45 Don't worry about it. Can happen to anyone.

:) – Emil H May 4 '09 at 19:47 it worked, thank you very much :) – pistacchio May 4 '09 at 19:50.

I take this is a code snipet, consequently, I guess that you are alredy including string. H strcpy is defined as: char * strcpy ( char * destination, const char * source ); In strcpy(linesnum_righe-1, line); lines num_righe-1 is a char, not a char* So it should be strcpy(lines + (num_righe-1), line); As munificent wrote, it looks like you are trying to make lines an array of strings. If so, your definition of lines is wrong.

Also, don't forget, you should check that realloc doesn't return NULL. Lines = realloc(lines, (sizeof(char)*LINE_SIZE)*num_righe); if (!lines) //MUST HANDLE NULL POINTER! /* string copy code here.

– samoz May 4 '09 at 19:35 1 No. Linesnum_righe-1 is a char, and lines + (num_righe-1) is a pointer to the same char. One could also use &linesnum_right-1.

– Emil H May 4 '09 at 19:36 There is no need to cast when using realloc. – anon May 4 '09 at 19:42 @Neil. I was dubitative about that one.

It's gone. – Tom May 4 '09 at 19:48.

Lines is a pointer to a character, i.e. A single string. You want it to be an array of strings.

For that, it should be char **lines.

You can use fscanf instead to do what you want. Fscanf(f, "%s\n", lineindex); index.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions