Solved

gets() problem

Posted on 2007-04-06
4
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
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:
Kent Olsen 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:Kent Olsen
ID: 18863421


"reading from a socket or file stream"

sorry...

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is sub-make ? 2 101
Constant string is of type char *   ? 7 52
Solaris acount issues 44 72
MS Excel duplicate input detect. 8 42
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

731 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