Solved

gets() problem

Posted on 2007-04-06
4
306 Views
Last Modified: 2013-12-27
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);

}

0
Comment
Question by:deadite
  • 3
4 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 18863204
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
0
 
LVL 8

Author Comment

by:deadite
ID: 18863347
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.
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 18863416
Hi Deadite,

first thing in this function, insert this statement:

  fflush (stdin);

It's non-standard, but most C implementations support it.  It will empty the input buffer and continue on so that anything that's already in the buffer goes away and the next read (gets, fgets, etc.) starts with a clean slate.

This is not the kind of thing that you'll want to do if the program is reading a socket or file start as there may be data in the buffer that you don't want skipped, but for this test it should tell us if "left over" data is the problem.

Kent
0
 
LVL 45

Expert Comment

by:Kdo
ID: 18863421


"reading from a socket or file stream"

sorry...

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now