Members Online
Total Online: 31 Web Spiders: 14
Guests Online: 28
Members Online: 3
Registered Members: 70170 Newest Member: bahmx
|
View Thread
| Author |
C win/linux trouble |
chess_rock
Member

Posts: 243
Location:
Joined: 20.02.08 Rank: God |
|
I've recently installed linux on my machine and i have started programming with C in it recently. I've found out some problems since there is no system("PAUSE" commands on linux. I looked up on google and the suggestions of people is to replace system"("PAUSE" by getchar() , but i tried lots of differnt ways and i couldn't make my program simulate that pause. It just simply run as if there was no getchar() there. Do any of you know how could i make that work?
Also, i've heard of commands named sleep() and usleep(). According to the sites i've found, they work setting a time for the program to sleep (quite logic, huh? ). But then, what none of them really explained, was the difference between sleep and usleep. Does anybody know the difference between them? |
|
| Author |
RE: C win/linux trouble |
spyware
Member

Posts: 4190
Location: The Netherlands
Joined: 14.04.07 Rank: God Warn Level: 90
|
|
sleep = sleep for X seconds
usleep = sleep for X microseconds
getchar = (waiting for) user input.
I recommend you download an ebook and do some chapters, before venturing in the wondrous world of C alone.
Good luck!

"The chowner of property." - Zeph Widespread intellectual and moral docility may be convenient for leaders in the short term,
but it is suicidal for nations in the long term. - Carl Sagan Since the grid is inescapable, what were the earlier lasers about? Does the corridor have a sense of humor? - Ebert |
|
| Author |
RE: C win/linux trouble |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
What do you need system('pause') for anyway? In windows you generally use it to prevent your console box from disappearing as soon as the program is finished, but in linux you ought to be running your programs through the console anyway.
And in the future, disable smilies.
|
|
| Author |
RE: C win/linux trouble |
chess_rock
Member

Posts: 243
Location:
Joined: 20.02.08 Rank: God |
|
|
spyware wrote:
getchar = (waiting for) user input.
I recommend you download an ebook and do some chapters, before venturing in the wondrous world of C alone.
Good luck!
i've used getchar many times before, but it does not act just like a pause. I know it waits for user input, but then, why does it not wait for my input in linux? that is my doubt. I do the same exact thing in windows and it does work. Understad what i mean now?
thank you for answering my doubt with sleep and usleep.
ynori7 wrote:
What do you need system('pause'  for anyway? In windows you generally use it to prevent your console box from disappearing as soon as the program is finished, but in linux you ought to be running your programs through the console anyway.
And in the future, disable smilies.
Well, the thing is that i wrote the program on windows, and i wanted to try it on linux just to test the compiler, change the code a bit and feel the difference. I will present the win version on university, and i wanted my linux version to do exactly the same as the windows version. No real reason.
Sorry for the smiles... |
|
| Author |
RE: C win/linux trouble |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
|
chess_rock wrote:
Well, the thing is that i wrote the program on windows, and i wanted to try it on linux just to test the compiler, change the code a bit and feel the difference. I will present the win version on university, and i wanted my linux version to do exactly the same as the windows version. No real reason.
Then just comment out the system("pause") before testing it in linux. It should work fine then unless you're doing anything else in the program that is OS-specific.
|
|
| Author |
RE: C win/linux trouble |
chess_rock
Member

Posts: 243
Location:
Joined: 20.02.08 Rank: God |
|
|
ynori7 wrote:
Then just comment out the system("pause") before testing it in linux. It should work fine then unless you're doing anything else in the program that is OS-specific.
Yeah, i've done that already. But i'll explain it more clearly... There are many different switches, and at the end of all of those, i tell the program to pause, clear all the terminal, and go back to the main function. I want that pause to make the program go back to the main function with the window clean. I know that in linux, system("CLS") = system("clear"), i just need the pause really. Any suggestions?
Edited by chess_rock on 15-07-09 22:07 |
|
| Author |
RE: C win/linux trouble |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
Try "read -n1".
|
|
| Author |
RE: C win/linux trouble |
stealth-
Member

Posts: 995
Location: Eh?
Joined: 10.04.09 Rank: God |
|
chess_rock wrote:
ynori7 wrote:
Then just comment out the system("pause") before testing it in linux. It should work fine then unless you're doing anything else in the program that is OS-specific.
Yeah, i've done that already. But i'll explain it more clearly... There are many different switches, and at the end of all of those, i tell the program to pause, clear all the terminal, and go back to the main function. I want that pause to make the program go back to the main function with the window clean. I know that in linux, system("CLS") = system("clear"), i just need the pause really. Any suggestions?
I think I might be confused about what your trying to do. I may not be a C expert, but in python (in linux), pausing and clearing the screen and running the main loop is as easy as:
os.system("sleep 5")
# sleep for 5 seconds
os.system("clear")
# clear the screen
break
# exit this loop or if statement or whatever
or, if I was waiting for user input:
raw_input("press enter")
os.system("clear")
break
I could be completely misunderstanding you, but something like that shouldnt be that hard to implement in C
The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com
Edited by stealth- on 15-07-09 23:31 |
|
| Author |
RE: C win/linux trouble |
chess_rock
Member

Posts: 243
Location:
Joined: 20.02.08 Rank: God |
|
COM sent me a pm to help me with this and it was very useful. There were two ideas in it:
1) Avoid system(), since different systems have different commands.
2) Try using cin.get()
For 1), i understand the problem of compatibility and i think this is a very valuable information.
I tried 2) and it didn't work, so i just resigned making my program similar to that of windows, especially because it will work at the terminal window.
I will keep programming in windows as long as i need to display things orderly, with pauses and CLS. Much easier than trying to re-adapt the whole thing.
I'm posting this conclusions here as to close this thread and in case any person has the same problem that i had: this could turn out to be useful. |
|
| Author |
RE: C win/linux trouble |
ynori7
Future Emperor of Earth

Posts: 1481
Location: #valhalla
Joined: 08.10.07 Rank: Diabolical |
|
Like I said a few posts ago, try the Linux command "read -n1". It performs essentially the same function as "pause" in Windows. It waits for the user to input one character.
|
|
| Author |
RE: C win/linux trouble |
COM
Banned

Posts: 800
Location:
Joined: 31.08.07 Rank: God |
|
|
chess_rock wrote:
COM sent me a pm to help me with this and it was very useful. There were two ideas in it:
1) Avoid system(), since different systems have different commands.
2) Try using cin.get()
For 1), i understand the problem of compatibility and i think this is a very valuable information.
I tried 2) and it didn't work, so i just resigned making my program similar to that of windows, especially because it will work at the terminal window.
I will keep programming in windows as long as i need to display things orderly, with pauses and CLS. Much easier than trying to re-adapt the whole thing.
I'm posting this conclusions here as to close this thread and in case any person has the same problem that i had: this could turn out to be useful.
Seems you misunderstood my PM a bit.
At point one, I listed three reasons for not using system(), but let's leave that as that was not the main issue.
I did not suggest you to use cin.get() as that is C++, while strictly C does work with C++, it does not work the other way around. What I did was liken it to an issue that I recognize from using C++.
The issue in question is that when you've already input something once, the return value is still in the in buffer. What this leads to is that when you later on call a function which is content with just taking a return value, it will consume the one in the in buffer and move on. To make it pause you will just have to use the same command twice, in this case:
getchar();
getchar();
This should make the return value disappear and then proceed to waiting for input; which in this case, as wanted, can be only enter.
Hope it helps, I was really hoping to avoid having to post here myself... sigh.
K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth! |
|
| Author |
RE: C win/linux trouble |
S1L3NTKn1GhT
Member

Posts: 468
Location: XXXX
Joined: 03.06.06 Rank: God Warn Level: 10
|
|
COM wrote:
chess_rock wrote:
COM sent me a pm to help me with this and it was very useful. There were two ideas in it:
1) Avoid system(), since different systems have different commands.
2) Try using cin.get()
For 1), i understand the problem of compatibility and i think this is a very valuable information.
I tried 2) and it didn't work, so i just resigned making my program similar to that of windows, especially because it will work at the terminal window.
I will keep programming in windows as long as i need to display things orderly, with pauses and CLS. Much easier than trying to re-adapt the whole thing.
I'm posting this conclusions here as to close this thread and in case any person has the same problem that i had: this could turn out to be useful.
Seems you misunderstood my PM a bit.
At point one, I listed three reasons for not using system(), but let's leave that as that was not the main issue.
I did not suggest you to use cin.get() as that is C++, while strictly C does work with C++, it does not work the other way around. What I did was liken it to an issue that I recognize from using C++.
The issue in question is that when you've already input something once, the return value is still in the in buffer. What this leads to is that when you later on call a function which is content with just taking a return value, it will consume the one in the in buffer and move on. To make it pause you will just have to use the same command twice, in this case:
getchar();
getchar();
This should make the return value disappear and then proceed to waiting for input; which in this case, as wanted, can be only enter.
Hope it helps, I was really hoping to avoid having to post here myself... sigh.
See his avatar? Thats what he's doing upon reading that.
root@wtf.org#su - dumbass

Dude you're AWESOME!
-SystemMeltdown(MSN)
|
|
| Author |
RE: C win/linux trouble |
S1L3NTKn1GhT
Member

Posts: 468
Location: XXXX
Joined: 03.06.06 Rank: God Warn Level: 10
|
|
COM wrote:
chess_rock wrote:
COM sent me a pm to help me with this and it was very useful. There were two ideas in it:
1) Avoid system(), since different systems have different commands.
2) Try using cin.get()
For 1), i understand the problem of compatibility and i think this is a very valuable information.
I tried 2) and it didn't work, so i just resigned making my program similar to that of windows, especially because it will work at the terminal window.
I will keep programming in windows as long as i need to display things orderly, with pauses and CLS. Much easier than trying to re-adapt the whole thing.
I'm posting this conclusions here as to close this thread and in case any person has the same problem that i had: this could turn out to be useful.
Seems you misunderstood my PM a bit.
At point one, I listed three reasons for not using system(), but let's leave that as that was not the main issue.
I did not suggest you to use cin.get() as that is C++, while strictly C does work with C++, it does not work the other way around. What I did was liken it to an issue that I recognize from using C++.
The issue in question is that when you've already input something once, the return value is still in the in buffer. What this leads to is that when you later on call a function which is content with just taking a return value, it will consume the one in the in buffer and move on. To make it pause you will just have to use the same command twice, in this case:
getchar();
getchar();
This should make the return value disappear and then proceed to waiting for input; which in this case, as wanted, can be only enter.
Hope it helps, I was really hoping to avoid having to post here myself... sigh.
See his avatar? Thats what he's doing upon reading that.
root@wtf.org#su - dumbass

Dude you're AWESOME!
-SystemMeltdown(MSN)
|
|
| Author |
RE: C win/linux trouble |
varreon
Member

Posts: 65
Location:
Joined: 13.10.07 Rank: Hacker Level 2 |
|
Are you writing C or C++? The cin is an object from the C++ libraries. Using the methods declared in stdio.h(standard C library), you could use the following function:
void pause(void){
char c;
printf("Hit return to continue...\n" ;
scanf("%c",&c);
}
and of course, call it with
pause();


 |
|
| Author |
RE: C win/linux trouble |
p4plus2
Member
Posts: 167
Location:
Joined: 31.03.08 Rank: God |
|
This will work good enough if you want a timed pause
#include <time.h>
void sleep(unsigned int ms)
{
clock_t goal = ms + clock();
while (goal > clock());
}
Not the worlds best solution by far, but it works.
"You can't be something your not,
Be yourself by yourself
Stay away from me" ~Walk, Pantera
"Playing an acoustic guitar is like having sex with your clothes on" ~Dave Mustaine
Edited by p4plus2 on 21-07-09 05:51 |
|
| Author |
RE: C win/linux trouble |
COM
Banned

Posts: 800
Location:
Joined: 31.08.07 Rank: God |
|
|
varreon wrote:
Are you writing C or C++? The cin is an object from the C++ libraries. Using the methods declared in stdio.h(standard C library), you could use the following function:
void pause(void){
char c;
printf("Hit return to continue...\n");
scanf("%c",&c);
}
and of course, call it with
pause();
Unnecessary.
Not only did I explain the issue two posts ago and said that he had gotten it wrong as cin is C++ and not C, but that code will in fact not work if something besides only enter has been input by the user beforehand.
K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth! |
|
| Author |
RE: C win/linux trouble |
chess_rock
Member

Posts: 243
Location:
Joined: 20.02.08 Rank: God |
|
oops!
You're right, cin.get() is a c++ command. I've been programming C for a very short time, and i sometimes mix the commands with C++, which i've studied few, a long time ago. I'm not in touch with these languages all the time, since i'm more into PHP lately. And coming back to them brings a small confusion sometimes.
I've realized it is not possible to make C programs work the same on windows and linux, since both run the programs differently. The most similar to a pause would be a simple getchar().
I've tried read -n1 and it did not work properly as i would like it to work. And for scanf, i needed to create an extra variable, which i don't like much. My program might run under a very slow computer, imagine something a lot worse than a gameboy. So i don't like creating extra variables.
Sorry for making you post here COM, i didn't realize cin.get was a c++ command when you told me.
By the way, i didn't like the system("clear") a lot... It is better to load the program again and again rather than the "cute" look i tried to give to the program. I've realized that if i want my program to look "cute", i should program it on windows, no linux. Thank you all for your suggestions. I've tried them all :)
EDIT: With two getchar() the program didn't just jump the code as if there was nothing there. It stopped, waiting for my input.
Edited by chess_rock on 22-07-09 21:00 |
|
|
|
|