2 Replies - 1881 Views - Last Post: 20 April 2011 - 03:07 AM Rate Topic: -----

#1 tektonik   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-March 11

Read file one line at a time.

Posted 17 March 2011 - 08:43 PM

Description: Set a char pointer equal to get_next_line() and then use it however you would use any other character string. You must do your own end of file checking, although get_next_line() will return NULL when it reaches EOF. Don't forget to free your pointer after using it to prevent a memory leak.Read a file one line at a time and store it as a character string.

I wrote this function as part of an HTML parser where I needed to read a line in and then do further parsing and editing of the text.
#include <stdio.h>
#include <stdlib.h>

/* The function get_next_line() */
char *get_next_line(FILE* fp)
{
    int ch;
    int count = 0;
    int index = 0;

    if ((ch = fgetc(fp)) == EOF)
        return '�';
    count++;

    while (ch != EOF) {
        if(ch == 'n')
          break;
        ch = fgetc(fp);
        count++;
    }

    char *buffer = (char *)malloc((sizeof(char)) * (count));
    if (buffer == NULL) {
        printf("nmalloc error!");
        exit(1);
    }
    fseek(fp, -(count), SEEK_CUR);
    for(index = 0; index < (count); index++) {
        ch = fgetc(fp);
        buffer[index] = ch;
    }
    buffer[index-1] = '�';

    return buffer;
}


/* A sample implementation */
int main()
{
    FILE *fp;
    char *line;
    int line_count = 0;

    if(!(fp = fopen("testing_main.c", "r"))) {
        printf("error opening file.n");
        return 1;
    }

    while((line = get_next_line(fp)) != NULL) {
        printf("nLINE %d = %s",line_count, line);
        free(line);
        line_count++;
    }
    fclose(fp);

    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Read file one line at a time.

#2 tektonik   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-March 11

Re: Read file one line at a time.

Posted 17 March 2011 - 08:43 PM

Description: Set a char pointer equal to get_next_line() and then use it however you would use any other character string. You must do your own end of file checking, although get_next_line() will return NULL when it reaches EOF. Don't forget to free your pointer after using it to prevent a memory leak.Read a file one line at a time and store it as a character string.

I wrote this function as part of an HTML parser where I needed to read a line in and then do further parsing and editing of the text.
#include <stdio.h>
#include <stdlib.h>

/* The function get_next_line() */
char *get_next_line(FILE* fp)
{
    int ch;
    int count = 0;
    int index = 0;

    if ((ch = fgetc(fp)) == EOF)
        return '�';
    count++;

    while (ch != EOF) {
        if(ch == 'n')
          break;
        ch = fgetc(fp);
        count++;
    }

    char *buffer = (char *)malloc((sizeof(char)) * (count));
    if (buffer == NULL) {
        printf("nmalloc error!");
        exit(1);
    }
    fseek(fp, -(count+1), SEEK_CUR);  //EDIT: added +1 to count to prevent missing first character
    for(index = 0; index < (count); index++) {
        ch = fgetc(fp);
        buffer[index] = ch;
    }
    buffer[index-1] = '�';

    return buffer;
}


/* A sample implementation */
int main()
{
    FILE *fp;
    char *line;
    int line_count = 0;

    if(!(fp = fopen("testing_main.c", "r"))) {
        printf("error opening file.n");
        return 1;
    }

    while((line = get_next_line(fp)) != NULL) {
        printf("nLINE %d = %s",line_count, line);
        free(line);
        line_count++;
    }
    fclose(fp);

    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#3 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Read file one line at a time.

Posted 17 April 2011 - 10:41 AM

Note: the memory allocation for each line isn't very efficient. I would allocate once at the beginning, fill the block and then each iteration return the next line.
Was This Post Helpful? 0
  • +
  • -

#4 tektonik   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-March 11

Re: Read file one line at a time.

Posted 20 April 2011 - 03:07 AM

I'm not quite sure what you mean. Can you explain in more detail? The reason I reallocate each time is that the lines in the text file are assumed to be of unknown and variable length. Sorry if I misunderstand what you are saying.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1