?
Solved

What is wrong with this code?? gets dont work?

Posted on 2006-05-19
13
Medium Priority
?
249 Views
Last Modified: 2010-04-01
       char cCmd[255];
      char cLetter;
      int num;

      if (loadFile() == 1)
      {
            printf("\nError opening file!\n");
            return 1;
      }
      
      displayFile();
      
      printf("\n\nPlease enter command: ");
      gets(cCmd);
printf("here");

I have this code above, now for some reason it never get past the line gets(cCmd);.. anyone have any ideas?
0
Comment
Question by:sakuya_su
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 10

Author Comment

by:sakuya_su
ID: 16716138
what i mean is the program terminates on gets(cCmd)
0
 
LVL 5

Expert Comment

by:gerhardschoeman
ID: 16716205
have you tried running in debug mode?
are you maybe trying to enter a string longer then 255 characters?


regards;
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16716212
the string i typed was A 1
0
Independent Software Vendors: 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!

 
LVL 5

Expert Comment

by:gerhardschoeman
ID: 16716246
try and run the following abbreviation in your compiler:

#include <stdio.h>

int main()
{
    char cCmd[255];

    printf("Enter command:");
    gets(cCmd);

    return 0;
}

to see if it works.

regards;
0
 
LVL 5

Expert Comment

by:B1-66ER
ID: 16716261
Hi sakuya_su
> it never get past the line gets(cCmd);
what it mean7
Your program doesnt show msg "here"7
Try instead line

printf("here");

write

printf("here\n");
while(1) ;
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16716270
interestingly, I compiled with g++, and now it works, somehow compiling with gcc it does not work.. anyone have any ideas?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16716274
Hi sakuya_su,

From gets man page
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

fgets (cCmd, 255, stdin);

Cheers!
Sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16716285
Hi sakuya_su,

> interestingly, I compiled with g++, and now it works, somehow compiling
> with gcc it does not work.. anyone have any ideas?
It could have been one of the weird behaviors of gets() or it could be buffering ...

Try fflush (stdout); to flush the buffers ..

Also note that this is very Cish ... :)

Cheers!
Sunnycoder
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16716319
yeah, its C, lol I have become unfaimiliar with C now..

fflush didnt help
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16716339
what about fgets .. did you try
fgets (cCmd, 255, stdin);
in place of gets
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16716356
yeah fgets works, but it reads the \n, the point is now I do not need to fix this problem anymore with G++, but rather I want to know why compiling with gcc give me such a strange error?
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1000 total points
ID: 16716387
It is not gcc's fault ... it is gets which does exhibit strange behavior quite often and should be avoided by all means. I have no idea why it worked with g++ ... may be different I/O buffering
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 16716425
i see, thanks
0

Featured Post

Industry Leaders: 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

840 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