473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Reading a line of text from a stream


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...)

Mar 5 '08 #1
0 1354

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
3
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant...
1
by: bbepristis | last post by:
Hey all I have a wired issue I have a csv file with , seperated values I have some code to read the file and put the record into an array then into textboxes. the problem is it seems to read...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.