Problem in read line function

This is a discussion on Problem in read line function within the C Programming forums, part of the General Programming Boards category; Hi, I am a beginner in C language and I am developing a function to read lines, but is showing ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Problem in read line function

    Hi,

    I am a beginner in C language and I am developing a function to read lines, but is showing some invalid characters.

    Code:
    char *getNextLine()
    {
        int count = 0;
        char *retVal;
    
    	if(feof(arq)) return NULL;
    
    	free(retVal);
    
    	retVal = (char*)malloc(sizeof(char));
    
    	while((*(retVal + count) = fgetc(arq)) != '\n' && !feof(arq))
    	{
    	    count++;
    
    		retVal = (char*)realloc(retVal, sizeof(char) * count + 1);
    	}
    
    	return retVal;
    }
    Show this.

    ----------------Test in C------------------------1
    ----------------Test in C------------------------2
    ----------------Test in C------------------------3
    ----------------Test in C------------------------4
    ----------------Test in C------------------------5
    ----------------Test in C------------------------6
    ----------------Test in C------------------------7
    ----------------Test in C------------------------8
    ----------------Test in C------------------------9
    ----------------Test in C------------------------10
    x----------------Test in C------------------------11
    x----------------Test in C------------------------12
    x----------------Test in C------------------------13
    x----------------Test in C------------------------14
    x----------------Test in C------------------------15
    @----------------Test in C------------------------16
    The lines 11 to 16 show the characters strange.

    what am I doing wrong?

    thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You are not null terminating the string.

    Realloc() should not be used one byte at a time, this is very inefficient. Also, I would use fgets() to read lines. You can use a temp buffer in the function of sufficient size (say 4k), then malloc a pointer based on the strlen() of that, copy in, and return the pointer.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    3
    Hi MK27, thanks for you help.

    A question, the function 'fgets()' need receive the string size, but i dont know the line size.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's an example of how to use fgets() for something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *getLine (FILE *p) {
    	char buffer[4096];
    	char *r = fgets(buffer,4096,p);
    	if (!r) return NULL;
    	r = malloc(strlen(buffer)+1);
    	strcpy(r,buffer);
    	return r;
    }
    
    int main(int argc, const char *argv[]) {
    	FILE *fp = fopen(argv[1], "r");
    	char *line;
    
    	while ((line = getLine(fp))) {
    		printf("%s",line);
    		free(line);
    	}
            close(fp);
    
    	return 0;
    }
    Couple of notes:
    • - "r" is used to check the return value of fgets and then reassigned some memory for use as the return value.
    • - 4096 characters is several pages of text. If your file may contain lines longer than that, you could adapt this function to use a variable length buffer:
      Code:
      char *getLine (FILE *p, int sz) {
      	char buffer[sz];      
      	char *r = fgets(buffer,sz,p);
    • I just reuse the same pointer in main(), but obviously you could assign to (eg) an array using a for() loop.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    3
    I understand, thank mk27.
    You were a great help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Read Line function
    By SMB3Master in forum Contests Board
    Replies: 4
    Last Post: 05-20-2003, 08:50 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21