C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 29th, 2012, 12:16 AM
nonergodic nonergodic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 nonergodic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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(...).

Reply With Quote
  #2  
Old June 29th, 2012, 12:50 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 9 m 51 sec
Reputation Power: 1774
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?
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old June 29th, 2012, 09:09 AM
nonergodic nonergodic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 nonergodic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > FgetsNoNL function w/o using fgets(...)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap