string Problem
-
#include <iostream> #include <fstream> #include <string> using namespace std; #define MAX 5000 string read_config (char* path) { /* Just some weird var stuff */ string buffer; string config_markers[MAX]; int i = 0; /* EOF var stuff */ ifstream file(path); while (!file.eof()) { /* Read line by line */ getline(file, buffer); /* We don't like Buffer Overflows */ if (i < MAX && strstr (buffer.c_str(), "#") == NULL) { /* Write the array */ config_markers[i] = buffer; i++; } } /* Close the file handle */ file.close(); /* Return the number of elements in the array */ return config_markers; }
In file included from main.cpp:27:
read_config.h: In function »std::string read_config(char*)«:
read_config.h:40: Fehler: Umwandlung von »std::string*« in nicht-skalaren Typen »std::string« angefordert
main.cpp: In function »int main(int, char**)«:
main.cpp:37: Warnung: veraltete Konvertierung von Zeichenkettenkonstante in »char*«
read_config.h: In function »std::string read_config(char*)«:
read_config.h:40: Fehler: Umwandlung von »std::string*« in nicht-skalaren Typen »std::string« angefordertKann mir das wer erklären?
Das ist eine string Funktion, die ein string array zurückgibt... sollte doch hinhauen?
-
In Zeile 40 gibst du einen std::string-Pointer zurück, hast als Rückgabetyp aber std::string deklariert (Zeile 11). Zu dem Warning in main.cpp:37 kann ich nichts sagen, da der Code fehlt. Zeile 11 müsste entweder
string[] read_config (char* path)
oder
string* read_config (char* path)
lauten.
-
alexander@Osiris:~/Desktop/sis c++/Project$ g++ main.cpp read_config.h scan.h
In file included from main.cpp:27:
read_config.h:11: Fehler: expected unqualified-id before »[« token
read_config.h:11: Fehler: expected unqualified-id before »[« tokenwenn ich zeile 11 mit string[] mache
und mit string* gibts auch zig fehler!
-
Man übergibt dem Compiler keine Header-Dateien.
-
stimmt und das hilft mir ja bei den Fehlern weiter was?
alexander@Osiris:~/Desktop/sis c++/Project$ g++ main.cpp -o file
In file included from main.cpp:27:
read_config.h:11: Fehler: expected unqualified-id before »[« token
-
also:
main.cpp
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <stdlib.h> #include <pthread.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <pthread.h> #include <time.h> #include <string.h> #include <sys/stat.h> #include <fcntl.h> #include <signal.h> #include <unistd.h> /* This is where all files and directories get scanned! */ #include "scan.h" /* This is the include for the configuration reader */ #include "read_config.h" #define MAX 5000 /* Variables section */ string* config_array_elements; string test; int main(int argc, char *argv[]) { config_array_elements = read_config ("pattern.conf"); //config_array_elements = read_config ("include.conf"); //print_rec_dir (argv[1], config_markers, MAX); for (int i=0; i < 5; i++) { cout << config_array_elements[i] <<endl; } return 0; }
read_config.h
#include <iostream> #include <fstream> #include <string> using namespace std; #define MAX 5000 string* read_config (char* path) { /* Just some weird var stuff */ string buffer; string config_markers[MAX]; int i = 0; /* EOF var stuff */ ifstream file(path); while (!file.eof()) { /* Read line by line */ getline(file, buffer); /* We don't like Buffer Overflows */ if (i < MAX && strstr (buffer.c_str(), "#") == NULL) { /* Write the array */ config_markers[i] = buffer; i++; } } /* Close the file handle */ file.close(); /* Return the number of elements in the array */ return config_markers; }
So funktionierts jetzt, Ausgabe in der Konsole:
alexander@Osiris:~/Desktop/sis c++/Project$ ./file
r57������shell������exec����system��
Versteh ich nicht ganz....
-
Hat keiner eine Ahnung warum das nicht funktioniert?
Danke
-
Du gibst einen Zeiger auf C-Array von lokalen strings zurück. Also am Ende der Funktion sind alle strings wieder freigegeben. Kannst aber nen vector von strings zurückgeben, dann bräuchtest du auch kein MAX, da vectors dynamisch wachsen können.