Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Reading a line ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 24815 of 27695
Post > Topic >>

Reading a line of text from a stream

by Nelu <spamahead@[EMAIL PROTECTED] > Mar 5, 2008 at 06:39 PM

I would appreciate some comments on the code below for reading a line of 
text from a stream.


#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

/**
 * Reads a line of text from a stream and returns the string on
 * success and NULL on failure. 
 * fstream - the stream to read from
 * maxRead - a variable that specifies the maximum number of
 * characters to read. If maxRead is NULL then SIZE_MAX-1 is
 * considered as the maximum value. If maxRead is different from NULL
 * then the function will set the value to the length of non-NULL
 * string returned by the function.
 * fullLine - a variable that specifies whether or not the end of line
 * has been reached (a complete line was read). It also returns true if 
EOF
 * has been reached. It can be set to NULL.
 */
char *readStringWithLimit(FILE *fstream, size_t *maxRead, 
        int *fullLine) {
    size_t capacityIncrement=256;
    size_t capacity=capacityIncrement;
    size_t readLimit;
    size_t stringLength=0;
    char *myStr, *tmpStr;
    int localFullLine;
    unsigned char c;
    int ic;

    if(maxRead==NULL) {
        readLimit=SIZE_MAX-1;
    } else {
        if((*maxRead)>SIZE_MAX-1) {
            readLimit=SIZE_MAX-1;
        } else {
            readLimit=*maxRead-1;
        }
    }

    myStr=malloc(capacity);
    if(!myStr) {
        return NULL;
    }
    while(1) {
        ic=fgetc(fstream);
        if(ic==EOF) {
            if(ferror(fstream)) {
                free(myStr);
                return NULL;
            } else {
                localFullLine=1;
                break;
            }
        } else {
            c=(unsigned char)ic;
            if(c=='\n') {
                localFullLine=1;
                break;
            } else {
                if(readLimit>=stringLength) {
                    if(stringLength==capacity) {
                        //calculate the necessary size
                        if(SIZE_MAX-capacityIncrement>capacity) {
                            capacity+=capacityIncrement;
                        } else {
                            capacity=SIZE_MAX;
                        }
                        tmpStr=realloc(myStr,capacity);
                        if(tmpStr) {
                            myStr=tmpStr;
                        } else {
                            free(myStr);
                            return NULL;
                        }
                    }
                    myStr[stringLength++]=c;
                } else {
                    ungetc(ic,fstream);
                    localFullLine=0;
                    break;
                }   
            }
        }
    }
    if(stringLength==capacity) {
        tmpStr=realloc(myStr,capacity+1);
        if(tmpStr) {
            myStr=tmpStr;
        } else {
            free(myStr);
            return NULL;
        }
    }
    myStr[stringLength]='\0';
    if(maxRead) {
        *maxRead=stringLength;
    }
    if(fullLine) {
        *fullLine=localFullLine;
    }
    return myStr;
}


Thank you.

-- 
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
 




 4 Posts in Topic:
Reading a line of text from a stream
Nelu <spamahead@[EMAIL  2008-03-05 18:39:06 
Re: Reading a line of text from a stream
Micah Cowan <micah@[EM  2008-03-05 12:07:46 
Re: Reading a line of text from a stream
Nelu <spamahead@[EMAIL  2008-03-05 20:21:01 
Re: Reading a line of text from a stream
Micah Cowan <micah@[EM  2008-03-05 21:25:40 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 11:39:40 CDT 2008.