1

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 line[LINE_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(lines[num_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

3
  • you should really include the header #include <string.h> 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. Commented May 4, 2009 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. Commented May 5, 2009 at 0:53
  • @RBerteig. I agree, error checking is far from present. Commented May 5, 2009 at 2:05

4 Answers 4

5

Try:

FILE *f;
char line[LINE_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);
        lines[num_righe-1] = strdup(line);
}

fclose(f);
Sign up to request clarification or add additional context in comments.

5 Comments

isn't sizeof(char) always identical to 1 byte?
Yes. But my code uses sizeof(char*) which in general is 4 bytes, not 1.
Oh, didn't notice that *. I apologize.
Don't worry about it. Can happen to anyone. :)
Actually, strdup CAN return NULL. You might want to take that into account. In addition to that, you should remember to free that memory once you are done (strdup uses malloc)
2

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(lines[num_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, dont 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*/

4 Comments

Aren't these statements identical?
No. lines[num_righe-1] is a char, and lines + (num_righe-1) is a pointer to the same char. One could also use &lines[num_right-1].
There is no need to cast when using realloc.
@Neil. I was dubitative about that one. It's gone.
1

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;

Comments

1

You can use fscanf instead to do what you want.

fscanf(f, "%s\n", line[index]);
index++;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.