Follow us on Twitter!
Understanding is the answer, hatred is the problem, and hackers are the slaves abused and destroyed in the process of peace online - Deshouleres
Wednesday, April 16, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 15
Guests Online: 12
Members Online: 3

Registered Members: 82810
Newest Member: TheDuke777
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C win/linux trouble

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
Posted on 15-07-09 21:27
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"Wink commands on linux. I looked up on google and the suggestions of people is to replace system"("PAUSE"Wink 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? Pfft). 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: 4192
Location: The Netherlands
Joined: 14.04.07
Rank:
God
Warn Level: 90
Posted on 15-07-09 21:33
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!




img507.imageshack.us/img507/3580/spynewsig3il1.png
"The chowner of property." - Zeph
[small]
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
[/s
http://bitsofspy.net
Author

RE: C win/linux trouble

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 15-07-09 21:39
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.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C win/linux trouble

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
Posted on 15-07-09 21:48
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'Wink 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
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 15-07-09 21:51
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.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C win/linux trouble

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
Posted on 15-07-09 22:06
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
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 15-07-09 22:19
Try "read -n1".


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C win/linux trouble

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 15-07-09 23:29
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:

Code

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:
Code

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.stealt. . .

Edited by stealth- on 15-07-09 23:31
http://www.stealth-x.com
Author

RE: C win/linux trouble

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
Posted on 19-07-09 18:54
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
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 19-07-09 19:03
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.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C win/linux trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-07-09 21:15
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:

Code

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.


Author

RE: C win/linux trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-07-09 01:28
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:

Code

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.


Author

RE: C win/linux trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-07-09 01:30
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:

Code

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.


Author

RE: C win/linux trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 21-07-09 03:21
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"Wink;
scanf("%c",&c);
}

and of course, call it with
pause();




Author

RE: C win/linux trouble

p4plus2
Member

Your avatar

Posts: 167
Location:
Joined: 31.03.08
Rank:
Newbie
Posted on 21-07-09 05:51
This will work good enough if you want a timed pause

Code

#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
p4plus2@hotmail.com
Author

RE: C win/linux trouble


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 21-07-09 09:11
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.


Author

RE: C win/linux trouble

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
Posted on 22-07-09 20:23
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