Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Just a quick note to say, "THANKS!" for these forums...The site is very well layed out and easy to use. Thanks for bringing us together - we need each other."

Geography

Where in the world do Tek-Tips members come from?

if i have a file with input as

niru1972 (Programmer)
14 Feb 01 20:38
if i have a file
with input as

one
two
three

how can i do str=fgets(buf, 30, file) what should i declare str as?
zBuilder (MIS)
17 Feb 01 17:02
You could do this:

char *str;
char buf[33];
FILE *file;
// open the file here so file points to a valid handle...

str=fgets(buf, 30, file);

And then check str for NULL to see if fgets failed or not.


 
Kim_Christensen@telus.net
http://www3.bc.sympatico.ca/Kim_Christensen/index.html
 
rbobbitt (Programmer)
21 Feb 01 0:43
Also, when you call fgets, it makes for better maintenance to use sizeof instead. And there's a chance that the user entered a line that was too long for your buffer, you will probably want to check for this too:

char buf[33];
FILE *file;

/* Open the file */

if (NULL==fgets(buf, sizeof buf, file)) {
    /* fgets() failed, handle error */
} else {
    char *tmp=strchr(buf,'\n');
    if (NULL!=tmp) {
       /* success, get rid of the newline */
       *tmp='\0';
    } else {
        /* line was truncated, handle accordingly */
    }
}



Russ
bobbitts@hotmail.com
http://home.earthlink.net/~bobbitts

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close