Solved

gets() problem

Posted on 2007-04-06
4
304 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

10 Experts available now in Live!

Get 1:1 Help Now