You are not logged in.

  • Login

1

Monday, July 7th 2008, 11:40am

Variablen in System("") einbinden

Hi,

ich möchte in dem System Befehl eine Variable einbinen, die dann mit gestartet wird.

ich hatte daran gedacht mit net send zu arbeiten

2

Monday, July 7th 2008, 1:59pm

ehmm

C/C++ Quellcode

1
2
3
4
5
6
7
string Variabel;
 
Variabel = "exit";
 
//bin grad in der arbeit und hab net viel zeit aber wenn du in einem string eine int. zahl etc. reinbringen willst sollte es dafür die funk. fstring geben glaube ich
 
System(Variabel);


lg

3

Saturday, July 12th 2008, 7:57pm

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int asd;
    cout<<"Bitte Sekundenzahl angeben! Nach dieser Angabe wird der PC Heruntergefahren!\n\n";
    cin>>asd;
    string str= "start Shutdown.exe -s -t ";
    str += asd;
    system(str.c_str());
    system("PAUSE > NUL");
    return 0;
}


Ich hab da jetzt mal versucht mit nem STD::STRING zu arbeiten, funktioniert aber irgendwie auch nicht.

Wäre Dankbar, wenn jemand eine funktionierende Variante postet.

MfG: eriX

4

Saturday, July 12th 2008, 8:25pm

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int asd;
    cout<<"Bitte Sekundenzahl angeben! Nach dieser Angabe wird der PC Heruntergefahren!\n\n";
    cin>>asd;
    string str= "start Shutdown.exe -s -t ";
    str += asd;
    system(str.c_str());
    system("PAUSE > NUL");
    return 0;
}


Ich hab da jetzt mal versucht mit nem STD::STRING zu arbeiten, funktioniert aber irgendwie auch nicht.

Wäre Dankbar, wenn jemand eine funktionierende Variante postet.

MfG: eriX


start Shutdown.exe -s -t <-- Was soll denn dieses "start"?

XML Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdio>
 
using namespace std;
 
int main() {
	cout << "Befehl: ";
	char buf[256];
	cin >> buf;
	if(system(buf) == -1)
		cout << "FEHLER" << endl;
 
	return 0;	
}


Hab es mit mkdir und rm probiert - beides erfolgreich.

5

Tuesday, July 15th 2008, 12:27pm

Das ist es immer noch nicht.

Denn ein Teil des Befehls ist ja vordefiniert!

6

Tuesday, July 15th 2008, 12:43pm

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstdio>
#include <string>//oder war es string.h k.A^^ 
 
int main()
{
 std::cout << "Befehl: ";
 string buf,string vorgabe="shutdown ";
 std::cin >> buf;
 System(vorgabe+buf);
 return 0;	
}


Anmerkung ob das mit + funktoniert bin ich skeptisch =/ ich verwende für dynamische inhalte meistenseine funktion kann aber grad net (ned viel Zeit), hoffe ich konnte helfen!

7

Tuesday, July 15th 2008, 1:24pm

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstdio>
#include <string>//oder war es string.h k.A^^ 
 
int main()
{
 std::cout << "Befehl: ";
 string buf,string vorgabe="shutdown.EXE ";
 std::cin >> buf;
 System(vorgabe+buf ... .c_str());
 return 0;	
}



Allerdings wird das nicht funktionieren, da der Systembefehl ein Char-Array entegegen nimmt. du musst also buf zu vorgabe hinzufügen (vorgabe+=buf) und dann bei System(vorgabe.c_str()) angeben.
Aber das ist genau die Variante, die zu Beginn gepostet wurde. Die sollte dann auch funktionieren.

8

Tuesday, July 15th 2008, 1:48pm

in dem ersten post verstehe ich das nicht ganz?

system("PAUSE > NUL");

kenn nur normales pause?

hmm... haste auch wieder recht :O kannst du mal infos geben was genau nicht klappt (Fehlermeldungen, runtime beispiele, debuginfos)?

9

Tuesday, July 15th 2008, 5:03pm

Beim Compiler ist

string
buf
vorgabe
system

undeclared


dann will der Zeile 8 ein ; vor buf

10

Thursday, July 17th 2008, 3:38pm

Dann schreibst "std::string" statt "string"

11

Thursday, July 17th 2008, 8:29pm

Hab jetzt kein Linux ( ? ) aufgespielt.

Was haltet ihr hiervon:

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdio>
#include <string>
 
int main()
{
 std::cout << "Befehl: ";
 std::string buf, vorgabe="shutdown.EXE ";
 std::cin >> buf;
 vorgabe.append(buf);
 system(vorgabe.c_str());
 return 0;	
}


MfG

12

Thursday, July 17th 2008, 8:44pm

Linux?
Unter Linux heißt das sicher nicht "shutdown.EXE" - höchstens "shutdown" :)

13

Thursday, July 17th 2008, 8:54pm

Muss er dann die "SchiesS NiedeR.ExE" auch noch selbst schreiben :?:
Oder wie hat die sich hier eingeschlichen :?:
Man beachte die Gross-und Kleinschreibung ;)

14

Friday, July 18th 2008, 8:02am

Noch kleine Änderungen:

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdio>
#include <string>
 
int main()
{
 std::cout << "Befehl: ";
 std::string buf, vorgabe="shutdown "; // vor dem letzten ": Space oder nicht Space, das ist hier die Frage ;)
 std::cin >> buf;
 vorgabe.append(buf);
 std::system(vorgabe.c_str()); // wenn schon STL, dann ...
 return 0;	
}


Ein Leerzeichen hat mich mal ein paar Tage ins Grübeln gebracht - sieht man ja auch auf den ersten Blick ;)
Manchmal muss es da sein, manchmal darf es da nicht sein und in anderen Fällen ist es einfach egal.


hoffe, das ich nix mehr übersehen hab. ;)

This post has been edited 5 times, last edit by "bcc-fan" (Jul 19th 2008, 5:12pm)


15

Monday, July 21st 2008, 2:10pm

Danke bcc-fan

Der Code:

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdio>
#include <string>
 
int main()
{
 std::cout << "Befehl: ";
 std::string buf, vorgabe="shutdown.exe -s -f -t  ";
 std::cin >> buf;
 vorgabe.append(buf);
 std::system(vorgabe.c_str());
 return 0;	
}


Hat funktioniert!

Nun muss man aber die Zahl in Sekunden eingeben. Kann man den Wert vor dem Ausführen mal 60 rechnen?
Ich hatte ja an buf = buf * 60; gedacht... aber das funktioniert nicht. Wie kann ich das noch ändern?

16

Tuesday, July 22nd 2008, 8:17am

Um Castings kommst du wohl nicht rum.
Das Eifnachste hier wäre, einen Integerwert einzulesen, den zu multiplizieren mit 60 und schließlich in einen String zu casten.

17

Wednesday, July 23rd 2008, 6:55pm

Ja, gute idee!

Da hab ich jetzt den Artikel gefunden: http://www.cplusplus.com/doc/tutorial/typecasting.html

Bei ganz normalen int oder char wäre das kein Problem, denke ich.

Könnt ihr mir da jetzt bitte nochmal weiterhelfen?

18

Friday, July 25th 2008, 7:02pm

Probier mal sowas in die Richtung (includes nicht vergessen; Code ungetestet):

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
int main()
{
 std::cout << "Befehl: ";
 std::string vorgabe="shutdown.exe -s -f -t  ";
 int bufM
 std::cin >> buf;
 vorgabe.append(buf * 60);
 std::system(vorgabe.c_str());
 return 0;	
}

19

Saturday, July 26th 2008, 6:15pm

So hab ichs bis jetzt gemacht:

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdlib>
#include <iostream>
 
using namespace std;
 
bool checkIfInt(const char* text)
{
     for (int i = 0; i<strlen(text); ++i)
     {
         if (!isdigit(text[i]))
         {
           return false;
         }
     }
     return true;
}
 
int main(int argc, char *argv[])
{
    string asd;
    cout<<"Bitte Sekundenzahl angeben! Nach dieser Angabe wird der PC Heruntergefahren!\n\n";
    cin>>asd;
    if (checkIfInt(asd.c_str()))
    {                       
     string str= "start shutdown.exe -s -t  ";
     str += asd;
     system((str).c_str());
    }
    else
    {
         cout<<"Der eingegebene Text ist keine Zahl. Der PC wird nicht heruntergefahren.\n";
    }
    system("PAUSE > NUL");
    return 0;
}


Wie und wo muss ich dann welchen code einfügen?
@vorgänger: hat leider nicht funktioniert!

20

Saturday, July 26th 2008, 7:06pm

Ich habs gemerkt ;-)

Hier mein Vorschlag:

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdio>
#include <string>
 
int main()
{
  std::cout << "Befehl: ";
  std::string vorgabe="start shutdown.exe -s -t ";
  int buf;
  std::cin >> buf;
  char sBuf[128];
  sprintf (sBuf, "%u", buf * 60);
  vorgabe.append (sBuf);
  std::system(vorgabe.c_str());
  return 0;	
}

Social bookmarks