fscanf trennt : nicht
-
guten abend,
in meinem code trennt fscanf mein : nicht.
was mach ich da falsch?// // File: pwdsort.cc // Author: xmo // // Created on 5. Mai 2008, 12:31 // #include <stdlib.h> #include <iostream> #include <stdio.h> #include <string> #include <sys/stat.h> using namespace std; // // // string sortOption = "name"; string path = "/etc/password"; string newPath; string synkom = "pwdsort"; string myCin; size_t found; FILE * pFile; char buffer [130]; struct pwdent { char name[10]; char pwd[10]; int uid; int gid; char gcos[40]; char home[40]; char shell[20]; }; pwdent structs[100]; void checkKommando(string input) { if(input.substr(0,synkom.length()) == synkom) { if(input.find(" -") != string::npos) { sortOption = input.substr(synkom.length()+2, 4); //4 muss ersetzt werden cout << sortOption << '\n'; //Testweise } if(input.rfind(" ") != string::npos && (input.rfind(" ") + 1) != input.rfind("-")) { path = input.substr(input.rfind(" ") + 1, input.length() - input.rfind(" ")); cout << path << '\n'; //Testweise } }else cerr << "Syntax fehler"; } // HIER IN DIESER FUNKTION IST DAS PROBLEM void readFile() { char* myPath = const_cast<char*>(path.c_str()); pFile = fopen (myPath , "r"); if (pFile != NULL) { for(int i = 0 ; i < !feof(pFile)&& i < 1000 ; i++) { fscanf(pFile,"%s:%s:%d:%d:%s:%s:%s\n",structs[i].name,structs[i].pwd,structs[i].uid,structs[i].gid,structs[i].gcos,structs[i].home,structs[i].shell); } fclose (pFile); }else cerr << "Pfad nicht gefunden"; printf("%s#%s#%d#%d#%s#%s#%s",structs[0].name,structs[0].pwd,structs[0].uid,structs[0].gid,structs[0].gcos,structs[0].home,structs[0].shell); } int main(int argc, char** argv) { getline(cin, myCin); checkKommando(myCin); readFile(); return (EXIT_SUCCESS); }die txt datei sie ungefähr so aus:
name1:paswort1:1:1....
name1:passwort2:2:2:....und die ausgabe sieht so ungefähr aus:
name1:paswort1:1:1....#:paswort1:1:1....#..##
also irgend wie total verdrehtdanke schon mal im vorraus
-
Mische doch nicht die Funktionen von C und C++

-
Ich bin ein absuluter anfänger, keine ahnung was c und was c++ ist ^^
-
pwdent structs[100];und
for(int i = 0 ; i < !feof(pFile)&& i < 1000 ; i++) { fscanf(pFile,"%s:%s:%d:%d:%s:%s:%s\n",structs[i].name,structs[i].pwd,structs[i].uid,structs[i].gid,structs[i].gcos,structs[i].home,structs[i].shell); }man beachte die 100 und die 1000

zeig mal die zeile ndes Datei EXAKT wie sie da drin steihen, und die Ausgabe auch EXAKT...
-
xmo schrieb:
Ich bin ein absuluter anfänger, keine ahnung was c und was c++ ist ^^
1. Thema C/C++
#include <stdlib.h> // C, zudem in C++ bitte wenn schon #include <cstdlib> #include <iostream> // C++ #include <stdio.h> // C, zudem in C++ bitte wenn schon #include <cstdio> #include <string> // C++ #include <sys/stat.h> // Keine Ahnung...In C++ verwendet man in der Regel auch weder printf noch scanf, sondern greift auf die Streamklassen zu (z.B. zum Einlesen/Ausgeben von Dateien fstream).
2. Globale Variablen
Zudem sind globale Variablen in den aller seltesten Fällen sinnvoll.3. Les dir ein Tutorial oder Buch (wie C++ Primer) durch
cu André