Click here to Skip to main content
15,886,788 members
Articles / Desktop Programming / MFC

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

Rate me:
Please Sign up or sign in to vote.
2.40/5 (8 votes)
13 Sep 2007CPOL 46.1K   247   11   8
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:

C++
//

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)


Written By
Technical Lead
India India
Hi,
I am working...

Comments and Discussions

 
QuestionDoes it really work? Pin
pani6814-Sep-07 2:00
pani6814-Sep-07 2:00 
AnswerRe: Does it really work? Pin
Chetan Raiyani26-Jul-09 17:31
Chetan Raiyani26-Jul-09 17:31 
GeneralC++ solves this Pin
owillebo14-Sep-07 0:32
owillebo14-Sep-07 0:32 
GeneralRe: C++ solves this Pin
timvw14-Sep-07 1:57
timvw14-Sep-07 1:57 
GeneralRe: C++ solves this Pin
Chetan Raiyani30-Jan-09 9:48
Chetan Raiyani30-Jan-09 9:48 
GeneralRe: C++ solves this Pin
toxcct14-Sep-07 2:00
toxcct14-Sep-07 2:00 
GeneralRe: C++ solves this Pin
Chetan Raiyani30-Jan-09 9:55
Chetan Raiyani30-Jan-09 9:55 
GeneralRe: C++ solves this Pin
srikanth_xl24-Jul-09 9:46
srikanth_xl24-Jul-09 9:46 

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

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