Click here to Skip to main content
Licence CPOL
First Posted 13 Sep 2007
Views 20,167
Downloads 87
Bookmarked 9 times

Read any length of line from file and count the total number of lines in the file

By | 13 Sep 2007 | Article
You can read any length of line from the source file. No restrictions.

Introduction

In the past, if you wanted to read files, there was a restriction. You can not predict how long a line can be present at the read time. So the user has to predict the maximum length of a line that a file could store. But now, using this code, you can read any length of line from your file.

Background

During some programming situation, I needed to read a single line from a text file. At that time, I predicted that the maximum line length would be 1024 or 2048 characters long.

Using the code

Here is the ReadLine function code:

//

int ReadLine(char** pszBuffer, FILE * pFilePtr)
{
    char szBuffer[1024] = {0};   //Temporary Line Buffer
    char *pcRes         = NULL;  //Result of Line Reading
    int   nNl           = 0;     //Length of Line


    //If original buffer is already contains some content, then free it.

    if(*pszBuffer != NULL){
        free(*pszBuffer);
    }

    //allocate some memory 

    *pszBuffer = (char *) malloc(sizeof(szBuffer) + 1);
    **pszBuffer = '\0';
    //Read until new line character is not found in line

    while((pcRes = fgets(szBuffer, sizeof(szBuffer), pFilePtr))  != NULL){
        //Realloc buffer to adjust buffer size

        *pszBuffer = realloc(*pszBuffer, strlen(szBuffer) + 
                                         strlen(*pszBuffer) + 1);
        //if allocation fails

        if(*pszBuffer == NULL){
            printf("\n Memory error");
            return 0;
        }
        //append string to line buffer

        strcat(*pszBuffer, szBuffer);
        strcpy(szBuffer, "");
        nNl = strlen(*pszBuffer) - 1;
        //if end of line character is found then exit from loop

        if((*pszBuffer)[nNl] == '\n'){
            break;
        }
    }
    return 1;
}
//

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Chetan Raiyani

Software Developer (Senior)
Acty System India Pvt. Ltd. - Mumbai
India India

Member

Hi,
I am Chetan Raiyani from Rajkot, Gujarat.
I am working on C/C++/SDK/MFC/.Net Tech. in a Japanese Company.
 
COMPUTER is my first and only LOVE that's why I spend more than 14hrs per day in front of Computer. It makes me CRAZY but Now I am used to it.
 
You can contact me on chetan.raiyani@gmail.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionDoes it really work? Pinmemberpani682:00 14 Sep '07  
AnswerRe: Does it really work? PinmemberChetan Raiyani17:31 26 Jul '09  
GeneralC++ solves this Pinmemberowillebo0:32 14 Sep '07  
GeneralRe: C++ solves this Pinmembertimvw1:57 14 Sep '07  
GeneralRe: C++ solves this PinmemberChetan Raiyani9:48 30 Jan '09  
GeneralRe: C++ solves this Pinmvptoxcct2:00 14 Sep '07  
GeneralRe: C++ solves this PinmemberChetan Raiyani9:55 30 Jan '09  
GeneralRe: C++ solves this Pinmembersrikanth_xl9:46 24 Jul '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 14 Sep 2007
Article Copyright 2007 by Chetan Raiyani
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid