9 Replies - 14781 Views - Last Post: 17 September 2009 - 10:55 PM Rate Topic: -----

#1 HELPME!   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 70
  • Joined: 06-April 09

Get program to read multiple lines of user input

Posted 17 September 2009 - 03:08 AM

I was just wondering how you read multiple lines of user inputed data into an array using fgets?

thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Get program to read multiple lines of user input

#2 carltech   User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

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

This post has been edited by carltech: 17 September 2009 - 04:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 HELPME!   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 70
  • Joined: 06-April 09

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...

for( i=0; i< iconParam.height; i++ )
	{
		fgets(temp, sizeof ( temp ), stdin );
	}



but it doesnt read multiple lines..?

thanks.
Was This Post Helpful? 0
  • +
  • -

#4 carltech   User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Get program to read multiple lines of user input

Posted 17 September 2009 - 05:02 AM

What is iconParam.height?
Was This Post Helpful? 0
  • +
  • -

#5 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

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?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

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.

View Postcarltech, 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.
Was This Post Helpful? 0
  • +
  • -

#7 carltech   User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

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.

This post has been edited by carltech: 17 September 2009 - 12:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

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):

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.)


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.

Was This Post Helpful? 0
  • +
  • -

#9 carltech   User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

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!!!
Was This Post Helpful? 0
  • +
  • -

#10 HELPME!   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 70
  • Joined: 06-April 09

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


	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;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1