473,404 Members | 2,179 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,404 software developers and data experts.

Replacing fgets Problem solved.

Hello comp.lang.c

Couple of years ago I created a topic as to how I can use the function
fgets so that I can read a line of text from a source of data.

After a long time I have finally got all of my initial questions
answered accordingly.

The code below is hereby allowed to be used anybody and modified
without any legal strings attached... Simple.

#ifndef _READLINES_
#define _READLINES_

#include <stdio>
//#include <iostream>
#include <stdlib>
#include <string>

extern "C" short readLPostLine(FILE &rSource,char** content,int
default_length,signed int non_wanted_linefeeds);
extern "C" short readLine(FILE* pSource,char **content);
//using namespace std;

#endif

short readLPostLine(FILE &rSource,char** content,int
default_length,signed int non_wanted_linefeeds)
{
short ret=EXIT_SUCCESS;
int return_length;return_length = 0;
char* currentline;currentline = 0;
char* returnline;returnline = 0;
char* duplicate;duplicate = 0;
signed int offset;offset = 0;
int temp_length = 0;
// allocate default sized buffer
currentline = (char*)malloc(default_length);
currentline = strcpy(currentline,"");
do{
//read the the line first
if((currentline=fgets(currentline,default_length,& rSource))!=NULL)
{
//allocate memory temporarily
returnline = (char*)malloc(return_length+default_length);
returnline = strcpy(returnline,"");
offset = (strlen(currentline) 0 ?
currentline[strlen(currentline)-1]: 0 );
if(offset != non_wanted_linefeeds && offset!=EOF &&offset!
=NULL)
{
temp_length = strlen(returnline);
if(temp_length == (return_length+default_length) ||
temp_length == return_length )
{
return_length = return_length+default_length;
duplicate =(char*)malloc(return_length+default_length);
duplicate = strcpy(duplicate,returnline);
free(returnline);
returnline=0;
return_length = return_length+default_length;
returnline = (char*)malloc(return_length+default_length);
returnline = strcpy(returnline,duplicate);
free(duplicate);
duplicate=0;
}

if(temp_length == 0)
returnline = strcpy(returnline,currentline);
else
returnline = strcat(returnline,currentline);
}else
{
returnline =
strncat(returnline,currentline,strlen(currentline)-1);
ret = EXIT_SUCCESS;
}
} //Main IF
else {ret = EXIT_FAILURE;
}
}while( ret==EXIT_FAILURE && offset!=NULL && offset!=EOF);

if(ret==EXIT_SUCCESS && currentline!="")
{
//what is the current size?
if(*content!=NULL && content!=NULL )
{
if(strlen(returnline) strlen(*content) )
{
if(*content!=NULL){free(content);}
if(content!=NULL)free(content);
*content = (char *)malloc(strlen(returnline)+1);
}
}else{
*content = (char *)malloc(strlen(returnline)+1);
}

*content = strcpy(*content,"\0");
*content = strcpy(*content,returnline);
*content = strcat(*content,"\0");
}
else{
if(*content!=NULL)free(*content);
if(content!=NULL)free(content);
ret = EXIT_FAILURE;
}

if(returnline!=NULL) { free(returnline);returnline=0;}
if(currentline!=NULL){free(currentline); currentline=0;}

return ret;

}

short readLine(FILE* pSource,char **content){
return readLPostLine(*pSource,content,512,0x0D); /* default UNIX
length*/
}

//-------------------------------------------------------------------------------------------------------------------------//
int main(int argc,const char* argv[])
{
FILE* pSourceFile;
FILE* pOutputFile;
const char* ATOMIC_NAME;
char* ret = 0;

if(argv[0] != argv[argc-1]) {

ATOMIC_NAME=argv[argc-1];
pSourceFile = fopen(ATOMIC_NAME,"rb");
if(pSourceFile==NULL)
{
printf("File not found\n");return EXIT_FAILURE; }
else
{
fseek(pSourceFile,SEEK_SET,SEEK_SET);
pOutputFile = fopen("Output.txt","wb+");
fseek(pOutputFile,SEEK_SET,SEEK_SET);

while(readLine(pSourceFile,&ret)==EXIT_SUCCESS)
{
//if(ret[strlen(ret)]=='\n')
fputs(ret,pOutputFile);
//printf("%p = ",ret);
}

fclose(pOutputFile);
if(ret!=NULL||*ret==NULL){ free(ret);ret=0;}
}//ELSE

fclose(pSourceFile);
}else printf("Please specify a file name");
}
//----------------------------------------------------------------------------------------------------------------------//

The above code is fully functional.

Thanks for the help.
Jun 27 '08 #1
3 1620
FireHead wrote:
Hello comp.lang.c

Couple of years ago I created a topic as to how I can use the function
fgets so that I can read a line of text from a source of data.

After a long time I have finally got all of my initial questions
answered accordingly.

The code below is hereby allowed to be used anybody and modified
without any legal strings attached... Simple.

#ifndef _READLINES_
#define _READLINES_
Don't use include guards starting with an underscore and a capital
letter, they are reserved.
#include <stdio>
//#include <iostream>
#include <stdlib>
#include <string>
These are C++ headers.
The above code is fully functional.
It won't compile...

--
Ian Collins.
Jun 27 '08 #2
FireHead wrote:
Hello comp.lang.c
Did you mean to post in comp.lang.c++ instead?
Couple of years ago I created a topic as to how I can use
the function fgets so that I can read a line of text from a
source of data.

After a long time I have finally got all of my initial questions
answered accordingly.

The code below is hereby allowed to be used anybody
and modified without any legal strings attached... Simple.
Thanks, but no thanks.
#ifndef _READLINES_
#define _READLINES_
That identifier is reserved for the implementation in both C
and C++.
#include <stdio>
There is no such header in C (or C++.)
//#include <iostream>
<OT>Ironically, this is the most appropriate header.</OT>
#include <stdlib>
#include <string>
There are no such headers in C (or C++.)
extern "C"...
Syntax error.

<snip>
The above code is fully functional.
This is obviously some new meaning of the term I wasn't
previously aware of.

--
Peter
Jun 27 '08 #3
Peter Nilsson wrote:
FireHead wrote:
...
#include <stdlib>
#include <string>

There are no such headers in C (or C++.)
I tell a lie. One of them is a C++ header.

--
Peter
Jun 27 '08 #4

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

Similar topics

6
by: Tanel | last post by:
Hello, I need to read a result of the first script (that takes some time to run) from the second script so that the first script doesn't block the second. Here is a short example. do_smth_else()...
0
by: Paul Hsieh | last post by:
"Paul D. Boyle" <boyle@laue.chem.ncsu.edu> wrote: > There was a recent thread in this group which talked about the > shortcomings of fgets(). I decided to try my hand at writing a > replacement...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
1
by: Kberg | last post by:
In case anyone is interested, this is how I solved the problem of replacing a back slash with a forward slash (as in being able to access a document on a file share on an intranet via a browser) ...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
9
by: Nezhate | last post by:
Hi all, I've not written c code for many times a go, and now I can't understand why this occur when executing the next program: #include <stdio.h> #include <stdlib.h> #include <string.h> char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.