Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if i have a file with input as

Status
Not open for further replies.

niru1972

Programmer
Feb 3, 2001
6
US
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?
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top