The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
FgetsNoNL function w/o using fgets(...)
Discuss FgetsNoNL function w/o using fgets(...) in the C Programming forum on Dev Shed. FgetsNoNL function w/o using fgets(...) C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 29th, 2012, 12:16 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 2
Time spent in forums: 43 m 39 sec
Reputation Power: 0
|
|
|
FgetsNoNL function w/o using fgets(...)
I'm writing my own function to supplement fgets(...), with the usual objective of getting strings either from file or stdin and dropping the NL character; I almost have it working but I must be doing something horrible to the space I've allocated for my variables. It recovers all the strings I send it, and exits when the EOF is encountered, but with a 'bus error' at the end, depending on the order in which I declare my variables.
Here's the code that gives the bus error (if I declare the file pointer first, I don't get the error):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* fgetsNoNL( char *line, int len, FILE *fp );
#define MAXLINE 100
int main( int argc, char *argv[] )
{
char caText[MAXLINE];
char *dum = calloc( MAXLINE, sizeof( char ) );
FILE *ifp;
if (argc == 2 ) {
ifp = fopen( argv[1], "r" );
if ( ifp == NULL ) { fprintf( stderr, "File not found. Quitting...\n" );
exit (1);
}
}
while ( dum != NULL ) { if ( argc == 2 && ifp != NULL )
dum = fgetsNoNL( caText, sizeof( caText ), ifp );
else
dum = fgetsNoNL( caText, sizeof( caText ), stdin );
fprintf( stderr, "%s\n", caText ); }
if ( ifp != NULL )
fclose( ifp );
}
char* fgetsNoNL( char *line, int len, FILE *fp )
{
int index = 0;
int tmp = '\0';
while( ( tmp = fgetc( fp ) ) != EOF )
{ if ( tmp == '\n' ) {
line[index] = '\0';
return line;
}
else
line[index] = tmp;
index += 1;
}
line[index] = '\0';
if ( tmp == EOF ) { line = NULL;
return NULL;
}
return NULL;
}
Thanks in advance. I know I should free the pointer allocated for the dummy variable. It doesn't matter while the code is grumping. Thanks in advance for any tips on my use (abuse?) of memory. I'm using Apple's version of gcc in OS X 10.5 (Leopard).
The same function is very easy when using the actual fgets(...) function inside the utility function, because it does return NULL when the EOF comes up. I just want a function with the same parameters and return type as fgets(...).
|

June 29th, 2012, 12:50 AM
|
 |
Contributed User
|
|
|
|
|
Well you also need to add a test for
index < len
to make sure there is no buffer overflow.
But I have to ask why not just call fgets() anyway to do all the tricky work, then have a 1-liner to blow away the newline?
|

June 29th, 2012, 09:09 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 2
Time spent in forums: 43 m 39 sec
Reputation Power: 0
|
|
|
Everything is working fine now. Turns out the problem was not my buffers, but trying to close the FILE pointed to by ifp when using stdin.
Really, really dumb oversight.
I put in a test to see if argc == 2 and ifp not NULL before I try to close ifp, and initialize the FILE pointer variable to NULL at declaration.
In the function I did put a test in for exceeding the max length permitted for the string.
I went through all this to practice handling the EOF condition explicitly, learning as I go, and I was never really having problems with EOF.
Thanks for responding, salem. I hope to have more interesting matters to discuss in the future.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|