0

I have written some code to read each lines of textfile to 2d array.

/* FileProcess.c library */
#define LINE_SIZE 128 /* Max line's length = 256 characters */
extern ulong
File_ReadLine (FILE *fptr,
               char **result)
{
  char buff_line[LINE_SIZE], *p; 
  ulong nLines = 0UL;

  /* Check if fptr is readable */
  if (fptr == NULL) {
    printf("File not found.\n");
    return -1;
  }

 /*get number of lines; from http://stackoverflow.com/a/3837983 */
 while (fgets(buff_line, LINE_SIZE, fptr))
    if (!(strlen(buff_line) == LINE_SIZE-1 && buff_line[LINE_SIZE-2] != '\n'))
      nLines++;

  /* Allocating memory for result */
  result = malloc(nLines * sizeof(char *)); //

  /* Pointer return to begin of file */
  rewind(fptr);

  /* Getting lines */
  int i = 0;
  while (!feof(fptr)) {
    /* Get current line to buff_line */
    fgets(buff_line, LINE_SIZE, fptr);
    /* Replace '\n' at the end of line */
    char *c = strchr(buff_line, '\n');
    if (c != NULL)
      *c = '\0';

    /* Handle '\n' at the end of file */
    if (feof(fptr))
      break;
    /* Memory allocate for p */
     result[i] = malloc (LINE_SIZE * sizeof(char));

    /* Copy buff_line to p */
    strcpy(result[i], buff_line);
    i++;
  }
  return (nLines);
}

main program:

int main () 
{
  char **Phone;
  FILE *fptr;
  fptr = fopen("phone.na.txt", "r");
  ulong nLines = File_ReadLine(fptr, Phone);
  printf("%ld\n", nLines);

  int i;  
  for (i = 0; i < nLines; i++) {
    printf("%s", Phone[i]);
  }

fclose(fptr);
return 1;
}

Using gdb, running line by line, program return segmentation fault after printf("%s", Phone[i]); So I can't understand why segmentation fault here? Are there any errors with malloc() ?

  • Well, either `Phone` is an invalid pointer, or `Phone[i]` is an invalid pointer, or it's not null-terminated. You should use the debugger to discover which of these is the case. – Oliver Charlesworth Feb 26 '12 at 16:04
  • 1
    are you sure about the first define and the comment? #define LINE_SIZE 128 /* Max line's length = 256 characters */ – Jörg Beyer Feb 26 '12 at 16:05
  • @JörgBeyer: Sorry, that comment is my mistake. – huypn12 Feb 26 '12 at 16:12
  • @OliCharlesworth: File_ReadLine() function work correctly, but when I pass Phone to it, something was wrong. I can't understand why it doesn't work. – huypn12 Feb 26 '12 at 16:16
  • @HuyDt: Ok, so take a look at your variables when you're running in the debugger. That will be much more productive than asking strangers to spot errors in your code. – Oliver Charlesworth Feb 26 '12 at 16:20

1 Answers1

0

I haven't compiled or run the code, but I think the problem is in your line counter:

 while (fgets(buff_line, LINE_SIZE, fptr))
    if (!(strlen(buff_line) == LINE_SIZE-1 && buff_line[LINE_SIZE-2] != '\n'))
      nLines++;

What you're saying here is unless "the string length of buff_line is equal to LINE_SIZE -1 and the character at buff_line[LINE_SIZE-1] is not equal to '\n'", increment nLines.

So... whenever you read a line out of your text file which ends with '\n', and that line is 127 characters long, you're not going to increment nLines. You malloc spaces for nLines, but you're probably going to read more than nLines of data from your file... at that point, you're writing more into **result than you have allocated, and bad things are going to happen.

Barton Chittenden
  • 4,238
  • 2
  • 27
  • 46
  • The problem is: I've tried to change value storage by pointer-to-pointer, so the parameter of File_ReadLine must be (char ***result), in main program, call function with &Phone parameter. Works like a charm. Thanks for your attention. – huypn12 Feb 27 '12 at 14:11