Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

gets() problem

Posted on 2007-04-06
4
Medium Priority
?
315 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 46

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 46

Accepted Solution

by:
Kent Olsen earned 2000 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 46

Expert Comment

by:Kent Olsen
ID: 18863421


"reading from a socket or file stream"

sorry...

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded‚Ķ
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

610 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