ERLEDIGT
NEIN
NEIN
ANTWORTEN
1
1
ZUGRIFFE
419
419
EMPFEHLEN
-
19.01.12 17:34 #1
- Registriert seit
- Apr 2004
- Ort
- Kleinwallstadt --> Aschaffenburg --> Frankfurt
- Beiträge
- 238
Hallo ihr lieben,
hab eine Funktion geschrieben welche kommandozeilenparameter interpretieren soll...
Leider gibts ein kleines Problem... wenn ich das erste mal die Funktino aufrufe kommt der Parameter nicht richtig an... nach dem 2. mal geht es...
Könnte mir das bitte jemand erklären?
Code cpp: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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <stdio.h> #include <stdlib.h> #include <string.h> #define ARGV_INPUT "--input=" #define ARGV_OUTPUT "--output=" #define ARGV_LANG "--lang=" #define ARGV_HELP "--help" #define ARGV_DELIMITER "=" #define HELP_MSG "Usage: wechselkurse [--input= | --output | --lang=[de|en]]\n\ --input: input file\n\ --output: output file\n\ --lang: language\n" //Function to extract the input stream char* get_arg_input(int argc, char* argv[]) { int i; char temp[256] = ""; char *ptr1 = NULL; //Pointer for strstr char *ptr2 = NULL; //Pointer for strtok //==> Start at index 1 -> Filename is not our required "lang"-tag :D for(i=1; i < argc; i++) { ptr1 = NULL; ptr2 = NULL; sprintf(temp, "%s", argv[i]); //==> Search "--input"-Tag strcpy(temp, argv[i]); //<== Copy string ptr1 = strstr(strlwr(temp), ARGV_INPUT); //==> Found the right index with the "needed" content if (ptr1 != NULL) { //==> Find the "lang"-Tag ptr2 = strtok(ptr1, ARGV_DELIMITER); if (ptr2 == NULL) { continue; } //==> Shift Pointer to next result ptr2 = strtok(NULL, ARGV_DELIMITER); if (ptr2 != NULL) { return strlwr(ptr2); } } } return "stdin"; //<== Default language } int main(int argc, char *argv[]){ printf("\n->Erste ausgabe: %s", get_arg_input(argc, argv)); printf("\n->Zweite Ausgabe: %s", get_arg_input(argc, argv)); printf("\n->Erste ausgabe: %s", get_arg_input(argc, argv)); printf("\n->Zweite Ausgabe: %s", get_arg_input(argc, argv)); printf("\n->Erste ausgabe: %s", get_arg_input(argc, argv)); printf("\n->Zweite Ausgabe: %s", get_arg_input(argc, argv)); printf("\n->Erste ausgabe: %s", get_arg_input(argc, argv)); printf("\n->Zweite Ausgabe: %s", get_arg_input(argc, argv)); return EXIT_SUCCESS; }
Und die Ausgabe sieht bei mir so aus:
D:\Test>cmd_fuck.exe --input=../daten/eurofxref-hist.txt
->Erste ausgabe: ../daten/eurofxref-hist.≡8>
->Zweite Ausgabe: ../daten/eurofxref-hist.txt
->Erste ausgabe: ../daten/eurofxref-hist.txt
->Zweite Ausgabe: ../daten/eurofxref-hist.txt
->Erste ausgabe: ../daten/eurofxref-hist.txt
->Zweite Ausgabe: ../daten/eurofxref-hist.txt
->Erste ausgabe: ../daten/eurofxref-hist.txt
->Zweite Ausgabe: ../daten/eurofxref-hist.txt
D:\Test>
Bin gerade sehr verwirrt...
Mfg
HawksterGeändert von sheel (19.01.12 um 19:02 Uhr) Grund: Cpp-tags
Das Softwarebusiness ist Binär, du bist eine 1 oder eine 0 - Lebendig oder Tod...
-
Hi
der Returnpointer der Funktion ist hier jedes Mal ein Problem.
Entweder ein Literal ("stdin"): Was ist, wenn man das im main ändern will?
Oder, hier das Problem: Der Speicherbereich von temp, einer lokalen Funktionsvariable,
die am Funktionsende gelöscht wird.
Was dann für printf drinsteht, ist Zufall
(bzw. abhängig vom restlichen Code, Compiler, -optimierungen, etc. etc)
Mach einen zusätzlichen Parameter, der ein char-Array von main für das Ergebnis bekommt
Noch was: Das sprintf in der Funktion ist nicht nur problematisch (% in String?),
sondern sinnlos (strcpy gleich darunter).
GrußNetiquette (vA §15) und Nutzungsregeln (vA §4.8) einhalten! Programmcode in Codetags/Codeboxen.
Sehr gute Beiträge bitte Bewerten (Stern darunter oder "Danke").
"Funktioniert nicht" ist zu ungenau! Code, Fehlermeldungen, Verhalten des Programms, ...?
Ähnliche Themen
-
Kopie oder Pointer?
Von Thomasio im Forum C/C++Antworten: 5Letzter Beitrag: 12.03.08, 16:46 -
Aus Zwei Pointer Arrays mache ein Pointer Array
Von janosch im Forum C/C++Antworten: 1Letzter Beitrag: 22.12.05, 09:27 -
Pointer oder nicht?
Von GuenterMies im Forum VisualStudio & MFCAntworten: 9Letzter Beitrag: 28.05.04, 13:29 -
array oder pointer als Rückgabewert
Von bergamo im Forum Javascript & AjaxAntworten: 1Letzter Beitrag: 12.12.03, 13:33 -
[C++] Objekt Pointer zuweisen und Methoden auf Pointer realisieren?
Von Ezzz im Forum C/C++Antworten: 6Letzter Beitrag: 02.11.03, 21:23





Zitieren

Login






