I was just wondering how you read multiple lines of user inputed data into an array using fgets?
thanks
Get program to read multiple lines of user input
Page 1 of 19 Replies - 14781 Views - Last Post: 17 September 2009 - 10:55 PM
#1
Get program to read multiple lines of user input
Posted 17 September 2009 - 03:08 AM
Replies To: Get program to read multiple lines of user input
#2
Re: Get program to read multiple lines of user input
Posted 17 September 2009 - 03:57 AM
use sscanf.
It simply reads input strait into a string. If you have an array of strings then you can use this inside of a loop to take in input until something is taken in or done.
If you want to use gets then the same principle can be applied
It simply reads input strait into a string. If you have an array of strings then you can use this inside of a loop to take in input until something is taken in or done.
If you want to use gets then the same principle can be applied
This post has been edited by carltech: 17 September 2009 - 04:00 AM
#3
Re: Get program to read multiple lines of user input
Posted 17 September 2009 - 04:24 AM
im currently using fgets inside a for loop...
but it doesnt read multiple lines..?
thanks.
for( i=0; i< iconParam.height; i++ )
{
fgets(temp, sizeof ( temp ), stdin );
}
but it doesnt read multiple lines..?
thanks.
#4
Re: Get program to read multiple lines of user input
Posted 17 September 2009 - 05:02 AM
What is iconParam.height?
#5
Re: Get program to read multiple lines of user input
Posted 17 September 2009 - 05:35 AM
Why not using a while loop? or an infinite for loop?
Con we see code?
Con we see code?
#6
Re: Get program to read multiple lines of user input
Posted 17 September 2009 - 05:57 AM
I wrote this little snippet for a user yesterday to demonstrate this.
No, 10 quadrillion times NO!!! I've just told this user NOT to use gets! You should not be encouraging it, and I would suggest you read the links within that post as well.
carltech, on 17 Sep, 2009 - 05:57 AM, said:
If you want to use gets then the same principle can be applied
No, 10 quadrillion times NO!!! I've just told this user NOT to use gets! You should not be encouraging it, and I would suggest you read the links within that post as well.
#7
Re: Get program to read multiple lines of user input
Posted 17 September 2009 - 12:34 PM
Any time you use an array there is a certain risk that is taken. coding with gets isn't a problem if you are careful and use good coding practices.
Just saying to use fgets() is like saying to always use a vector in stead of an array...they may be safer but there are certain things that you may wan't an array for. Maybe he wants to exclude the new line character or something.
And I wasn't really encouraging the use of gets() as much as providing info about it...hence why I said to use sscanf().
I was just trying to help.
Just saying to use fgets() is like saying to always use a vector in stead of an array...they may be safer but there are certain things that you may wan't an array for. Maybe he wants to exclude the new line character or something.
And I wasn't really encouraging the use of gets() as much as providing info about it...hence why I said to use sscanf().
I was just trying to help.
This post has been edited by carltech: 17 September 2009 - 12:35 PM
#8
Re: Get program to read multiple lines of user input
Posted 17 September 2009 - 12:36 PM
Sorry, with all the options available, even suggesting the presence of gets as a function is irresponsible. Start new learners off with good practices so they don't have to later unlearn poor ones.
EDIT: From the Mac OS X man page for gets (and fgets):
From the Linux man page for gets:
Footnote from the relevant C-FAQ:
EDIT: From the Mac OS X man page for gets (and fgets):
Quote
SECURITY CONSIDERATIONS
The gets() function cannot be used securely. Because of its lack of
bounds checking, and the inability for the calling program to reliably
determine the length of the next incoming line, the use of this function
enables malicious users to arbitrarily change a running program's func-
tionality through a buffer overflow attack. It is strongly suggested
that the fgets() function be used in all cases. (See the FSA.)
The gets() function cannot be used securely. Because of its lack of
bounds checking, and the inability for the calling program to reliably
determine the length of the next incoming line, the use of this function
enables malicious users to arbitrarily change a running program's func-
tionality through a buffer overflow attack. It is strongly suggested
that the fgets() function be used in all cases. (See the FSA.)
From the Linux man page for gets:
Quote
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.
Footnote from the relevant C-FAQ:
Quote
When discussing the drawbacks of gets(), it is customary to point out that the 1988 ``Internet worm'' exploited a call to gets() in the Unix finger daemon as one of its methods of attack. It overflowed gets's buffer with carefully-contrived binary data which overwrote a return address on the stack such that control flow transferred into the binary data.
#9
Re: Get program to read multiple lines of user input
Posted 17 September 2009 - 05:41 PM
Oh I thought he said gets() in the original post but I must have been thinking of his last thread.
I was wondering why he would be so insistent on using gets().
Sorry bout that, also the link takes you to where you can learn about fgets() and it is still a similar principle that can be used...as stated above just use a simple loop to check for a certain condition(a sentinel) then exit the loop when it is entered and BLAMO!!!
I was wondering why he would be so insistent on using gets().
Sorry bout that, also the link takes you to where you can learn about fgets() and it is still a similar principle that can be used...as stated above just use a simple loop to check for a certain condition(a sentinel) then exit the loop when it is entered and BLAMO!!!
#10
Re: Get program to read multiple lines of user input
Posted 17 September 2009 - 10:55 PM
Thanks for your help everyone!
Im nearly there
im just having trouble now with assignment from temp to my pointer.
cause when i print the returned pointer... i just get one line instead of the 4...
any help with it? thanks
Im nearly there
im just having trouble now with assignment from temp to my pointer.
cause when i print the returned pointer... i just get one line instead of the 4...
any help with it? thanks
iconParam.width = 4;
iconParam.height = 4;
if(( newPtr = calloc( iconParam.width*iconParam.height+iconParam.height, sizeof( unsigned char ) ) ) == NULL )
{
fprintf( stderr, "Error Allocating Memory!");
return 0;
}
while( heightCount != iconParam.height )
{
count = 0;
buf = fgets(temp, sizeof ( temp ), stdin );
if ( buf)
{
if (buf[strlen(buf) - 1] == '\n')
{
buf[strlen(buf) - 1] = 0;
}
}
else
{
printf("\n");
break;
}
for ( i=0; i< iconParam.width*iconParam.height; i++ )
{
newPtr[i] = temp[i];
}
for ( i=0; temp[i] != '\0'; i++ )
{
count++;
}
heightCount++;
}
if ( count != iconParam.width )
{
printf("Incorrect Width\n");
return 0;
}
return newPtr;
Page 1 of 1

New Topic/Question
Reply


MultiQuote




|