Große ASCII-Dateien schnell einlesen



  • Hab hier im Forum folgenden C-Quellcode gefunden:

    FILE* pFile;
    	long lSize;
    	char* buffer;
    
    	pFile = fopen("myfile.txt","rb");
    	if (pFile==NULL) exit (1);
    
    	// obtain file size.
    	fseek (pFile, 0, SEEK_END);
    	lSize = ftell(pFile);
    	rewind (pFile);
    
    	// allocate memory to contain the whole file.
    	buffer = (char*) malloc (lSize);
    	if (buffer == NULL) exit (2);
    
    	// copy the file into the buffer.
    	fread(buffer,1,lSize,pFile);
    
    	/*** the whole file is loaded in the buffer. ***/
    
    	// terminate
    	fclose (pFile);
    	free (buffer);
    

    Wie kann man den in C++ übersetzen, dass er genauso schnell ist (3MB Daten in "null komma nix")? Benutze aktuell eine istream-"getline"-Variante, die benötigt für die 3 MB Daten 13 Sekunden. 😞



  • Morgen,

    da gibt es z. B. std::ifstream::read. Damit solltest du zurechtkommen.

    mfg
    v R



  • was versprichst du dir von der umsetzung in c++? die c version is doch prima
    oder machst dus nur zum spaß weil du sonst nix zu tun hast 😉



  • Sovok schrieb:

    was versprichst du dir von der umsetzung in c++? die c version is doch prima
    oder machst dus nur zum spaß weil du sonst nix zu tun hast 😉

    Der Übersichtlichkeit halber... nicht weil es mir Spaß macht.
    Ich komm in Erklärungsnot, wenn ich innerhalb eines Programmes mit new und malloc gleichzeitig arbeite, das sieht nicht richtig professionell aus.



  • Dann schreib doch new statt malloc 😉



  • Du kanst's doch auch in einen vector<char> einlesen.



  • FILE* pFile;
        long lSize;
        vector<char> buffer;
    
        pFile = fopen("myfile.txt","rb");
        if (pFile==NULL) exit (1);
    
        // obtain file size.
        fseek (pFile, 0, SEEK_END);
        lSize = ftell(pFile);
        rewind (pFile);
    
        // allocate memory to contain the whole file.
        buffer.resize(lSize);
    
        // copy the file into the buffer.
        fread( &(buffer[0]),1,lSize,pFile);
    
        /*** the whole file is loaded in the buffer. ***/
    
        // terminate
        fclose (pFile);
    


  • // "C"
    long lSize;
    char* buffer;
    buffer = (char*) malloc (lSize);

    // "C++"
    char* buffer = new (long);



  • wohl eher

    // "C"
    long lSize;
    char* buffer;
    buffer = (char*) malloc (lSize);
    free ( buffer );
    
    // "C++"
    char* buffer = new char[lSize];
    delete [] buffer;
    


  • #include<iostream>
    #include<fstream>
    #include<string>
    
    using namespace std;
    
    int
    main ()
    {
      string             txt;
      ifstream           in("myfile.txt",ios::ate+ios::binary);
      ifstream::off_type size;
    
      size=in.tellg();
      in.seekg(0);
    
      txt.resize(size);
      in.read(&txt[0],size);
      in.close();
    
      cout<<txt<<endl;
    
      return 0;
    }
    


  • hier noch die version ohne ios::binary:

    #include<iostream>
    #include<fstream>
    #include<string>
    
    using namespace std;
    
    int
    main ()
    {
      string             txt;
      ifstream           in("myfile.txt",ios::ate);
      ifstream::off_type size;
    
      size=in.tellg();
      in.seekg(0);
    
      txt.resize(size);
      in.read(&txt[0],size);
      txt.resize(in.gcount());
      in.close();
    
      cout<<txt<<endl;
    
      return 0;
    }
    

Anmelden zum Antworten