Link to home
Start Free TrialLog in
Avatar of deadite
deaditeFlag for United States of America

asked on

gets() problem

OK, I need to get input from command line (stdin) and the input will be text characters and possible spaces.  I have 8 prompts for input, which are being stored in arrays... then I am combining all 8 into 1 comma big array.  I was going to use gets() and it works fine when I run it on my local Linux box.  However, this is a simple client/server application I'm writing and have it running on a Solaris server.  My problem is, when I run it remotely on the server, I cannot enter anything for the first gets().  It always skips it and puts a blank in.  I've been pounding my head on the wall for a while on this one to no avail.  Currently, I am using scanf as a bandaid, but would prefer not to because I cannot have blank spaces(it will overflow the next array).  I will also accept another way to write this code... I don't care how it is done... all I need is to get the 8 inputs and put it into my array (and include spaces).  Here is the piece of code that is failing:(let me know if you need more info)

void createContact(char *host, char *service) {

      /* Login variables */
      char inbuf[MAX_BUF];
      char tag = 'n', SUCCESS = 'S', FAIL = 'F', results;
      int s, w, n; /* socket, write, and read count */
      int i;

      printf("Create New Contact Function\n");

      /* Add First name to buffer */
      char fname[SHORT_STRING];
      printf("First name:");
      gets(fname);

      /* Add Last name to buffer */
      char lname[SHORT_STRING];
      printf("Last name:");
      gets(lname);

      /* Add Building to buffer */
      char building[SHORT_STRING];
      printf("Building:");
      gets(building);

      /* Add Room to buffer */
      char room[SHORT_STRING];
      printf("Room:");
      gets(room);

      /* Add Phone extension to buffer */
      char phone[SHORT_STRING];
      printf("Phone Ext:");
      gets(phone);

      /* Add Email to buffer */
      char email[LONG_STRING];
      printf("Email:");
      gets(email);

      /* Add IM screename to buffer */
      char im[SHORT_STRING];
      printf("IM Screename:");
      gets(im);

      /* Add Web page to buffer */
      char web[LONG_STRING];
      printf("Web page:");
      gets(web);

      /* Build Buffer */
      sprintf(inbuf, "%c%s,%s,%s,%s,%s,%s,%s,%s", tag, fname, lname, building, room, phone, email, im, web);
      inbuf[strlen(inbuf)] = '\0';  /* Ensure Null Terminated */

      /* Fill Rest of Buffer with Nulls */
      for (i = strlen(inbuf); i < MAX_BUF; i++) {
            inbuf[i] = '\0';
      }

      /* Connect to Server */
      s = connectTCP(host, service);
      if (s < 0) {
            errexit("Error establishing connection to %s:%s\n", host, service);
      }

      /* Write to Server */
      w = write(s, &inbuf, MAX_BUF);
      if (w < 0) {
            errexit("socket write failed: %s\n", sys_errlist[errno]);
      }

      /* Read from Server */
      while ((n = read(s, &results, sizeof(results))) > 0) {
            if (n < 0)  /* Read Error */
                  errexit("socket read failed: %s\n", sys_errlist[errno]);

            if (results == SUCCESS) {
                  printf("Successfully added %s %s\n", fname, lname);
            }
            else {
                  printf("Failed! adding %s %s\n", fname, lname);
            }
      }

      /* Close Socket */
      close(s);

}

Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi deadite,

Just to keep the program from stumbling on itself, convert the gets() statements to fgets().

  fgets (name, SHORT_STRING, stdin);

There are pluses and minuses here.  The plus is that if the data gets "out of sync", you won't try and read 200 characters into a 10 character buffer.  The minus is that the program goes merrily along with the wrong data in the wrong place.  The minus is pretty small and goes away if you'll just perform a little data validation.


If the program always skips the first gets() and puts in a blank, that's because there's already data in the input buffer.  Check the rest of the program to make sure that it's reading everything that you expect it to read.


Kent
Avatar of deadite

ASKER

Kent,

Thanks for the quick comments.  I just converted it to fgets and re-ran my make files.... same result as the gets().  It  skips the first fgets() and goes right to the 2nd one (last name).  The line looks like:

Create New Contact Function
First name:Last name:

Then if I check first name, it always has a blank value.  As far as I can tell, everything is working fine in the program.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


"reading from a socket or file stream"

sorry...