Sign in to follow this  

stdin problem

This topic is 4199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm rather new at programming, and I'm having a problem with C code. I need to make a program that will send data through USB, and I have the hardware, drivers, and library I need. I modified a sample program that came with the software to send data in small commands instead of large blocks. My problem is, however, how can I modify it to read from a keyboard instead of a file? Here's the piece of my code that I'm having trouble with: (sorry I don't know how to format it correctly) for (index = 0; index < bufcnt; index++) { bytesread = fread(buffer[index], 1, 200, inFile); if (bytesread == 0) { shutdown = TRUE; break; } ... The inFile is declared using a command-line argument, and that works fine. I would like to change it though so that the user can type in a value that would go into buffer[index]. (The bytesread is unimportant). I tried replacing inFile with stdin, but the program doesn't resume when I'm finished typing, even after I pressed ctrl-z and enter a bunch of times. Thanks in advance for your help.

Share this post


Link to post
Share on other sites
Your program will block until it's read 200 bytes, just like you told it to.

Try using gets() instead.

for (index = 0; index < bufcnt; index++)
{
if (NULL == fgets(buffer[index], 200, inFile))
{
shutdown = TRUE;
break;
}
}

Share this post


Link to post
Share on other sites

This topic is 4199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this