Replacing fgets Problem solved.

F

FireHead

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

Ian Collins

FireHead said:
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...
 
P

Peter Nilsson

FireHead said:
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>

#include <stdlib>
#include <string>

There are no such headers in C (or C++.)
extern "C"...

Syntax error.

The above code is fully functional.

This is obviously some new meaning of the term I wasn't
previously aware of.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top