Re: Replacing fgets
- From: "FireHead" <satish.athreya@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: 2 Oct 2006 01:51:04 -0700
Richard Heathfield wrote:
[Lots of source, but it #included a non-standard header, so I couldn't
compile it for crits]
<snip>
The file < config.h >
....er, I didn't even see a <config.h> - the only inclusion I saw was of
a
<cliargs.h>
contains the following header file declartion.
#ifndef COMMON_HEADER_FILES
#define COMMON_HEADER_FILES
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#include <time.h>
#include <sys/mman.h>
#include <fcntl.h>
#endif
(Four of which are not standard C headers.)
I should have put the standard header files instead .. oops.
Yeah, especially when posting in comp.lang.c it's a good idea to reduce your
problem to one that can be expressed using only standard C constructs - or,
at the very least, to special-plead "I know this has got Unixy stuff in it,
but please just ignore all that stuff and focus on the C bit", or even "I'm
not sure whether this is a C problem or a Unix problem, here's the problem,
here's the code, what do you reckon?"
//---------------------------------------------------------------------------------------------------------//
Just a pointer the config.h supposed to cliargs.h header file.
In terms of portablilty anybody should be able these codes.
When the fopen is called rather that /usr/local/some_file_name it can
be also be used for any platform as long as you give valid folder/file
path.
For windows it can be < fopen( "Window File Name");
then code that I have currently will still work for any platform.
One this is definitely you are right about is that I have rewrite the
source code.
One more pointer is that if I use mmap it will solve my problem that is
to avoid calling the function free() in unix/Linux and other BSD
variants. But for windows it will a portability issue in the case of
using mmap function.
I was trying replicate sort of using my function called get_Copy().
By using the function get_Copy I should be elminate the usage of mmap
somehow.
But I just keep gets blasted Segmentation Error/ Memory out of range.
By using get_Copy I might be able simply code even further.
You all might have noticed that I have 3 version of the same
functionality.
2 function will be portable for any platform.
The below function is clean and understandable but its not functionable
on SCO UNIX and on windows.
Now I will not include windows because Windows is just @##$@#$.
/*
short get_Stream(FILE* pSource,char* result){
size_t buflen = 0;
char offset= 0;
int ret=EXIT_SUCCESS;
fpos_t* pCurPos=0 ; //current file position
fpos_t* pOldPos=0 ; //old file position
char *finally = "";
char *buffer = 0;
//get the starting File pointer position
fgetpos(pSource,&pOldPos);
//find the newline = 0x13
do{offset = 0; offset=fgetc(pSource);
} while(offset!=0x0A && offset!=0x0D && offset!=EOF && offset!=NULL &&
offset!=0xFF);
//get the Current File position
fgetpos (pSource,&pCurPos);
printf("%u",pCurPos);
//Total Buffer Length
buflen = (size_t)pCurPos - (size_t)pOldPos;
if(( offset == 0xFF || offset == EOF || offset==NULL) && (buflen ==
EXIT_FAILURE || buflen == EXIT_SUCCESS))
ret= EXIT_FAILURE;
else{
//Reset Everything
fsetpos (pSource,&pOldPos);
//allocate memory for the new address register
buffer = (char *)malloc(buflen);
//read file buffer using fgets
fgets(buffer,buflen,pSource);
//copy the string to a automatic variable register
//finally = get_Copy(buffer);
printf("\nfeof = %d\n",strlen(buffer));
//free the contents
free(buffer);
ret=EXIT_SUCCESS;
}//ELSE
buffer=0;
pCurPos = 0;
pOldPos = 0;
offset = 0;
return ret;
}*/
For the UNIX thinkers the proverb goes in the following way:
< No news is good news>.
So to me this proverb equated to where < EXIT_SUCCESS = meant that the
function worked fine and have had no bugs > and likewise EXIT_FAILURE
is the opposite of EXIT_SUCCESS.
.
- References:
- Re: Replacing fgets
- From: FireHead
- Re: Replacing fgets
- From: Richard Heathfield
- Re: Replacing fgets
- From: Christopher Layne
- Re: Replacing fgets
- From: Richard Heathfield
- Re: Replacing fgets
- Prev by Date: Re: standard libraries
- Next by Date: Re: Replacing fgets
- Previous by thread: Re: Replacing fgets
- Next by thread: Re: Replacing fgets
- Index(es):
Relevant Pages
|