Funktionsbibliotheken - C/C++

Tutorials.at
Verfügbare Informationen zu "Funktionsbibliotheken - C/C++"

  • Qualität des Beitrags: 0 Sterne
  • Beteiligte Poster: DrPhil_Guth - exbs
  • Forum: Tutorials.at
  • Forenbeschreibung: Programmierforum
  • aus dem Unterforum: Allgemeines
  • Antworten: 6
  • Forum gestartet am: Mittwoch 19.04.2006
  • Sprache: deutsch
  • Link zum Originaltopic: Funktionsbibliotheken - C/C++
  • Letzte Antwort: vor 16 Jahren, 2 Monaten, 22 Tagen, 14 Stunden, 4 Minuten
  • Alle Beiträge und Antworten zu "Funktionsbibliotheken - C/C++"

    Re: Funktionsbibliotheken - C/C++

    DrPhil_Guth - 06.01.2007, 17:07

    Funktionsbibliotheken - C/C++
    Hi!
    Wie wäre es, wenn wir einen thread aufmachen, wo jeder seine Funktionen oder klassen Reinstellt, von denen er glaubt dass sie nützlich sind?
    Dann hätten wir nen "funktionspool", und sparen uns etwas arbeit. Vielleicht könnte man dort auch reinposten, welche funktionen man brauchen würde, und dann könnte man zusammenarbeiten, um die arbeit zu vereinfachen.
    Was haltet ihr von der Idee?



    Re: Funktionsbibliotheken - C/C++

    exbs - 06.01.2007, 17:16


    finde ich gut. und um euch zu füttern füge ich mal schnell etwas an, was keiner mehr anders machen sollt.

    also da man kein "fflush(stdin)" verweden sollte (warum hab ich hier irgendwo schonmal gepostet), hab ich mal schnell das ganze in eine tolle funktion gepackt.

    was kleines aba feines:

    Code: /* Funktion zum leeren des Eingabepuffers */

    void clrpuf(void)
    {
        setvbuf(stdin,NULL,_IONBF,0);
        setvbuf(stdin,NULL,_IOFBF,BUFSIZ);
    }



    Re: Funktionsbibliotheken - C/C++

    DrPhil_Guth - 06.01.2007, 17:29


    So, hier eine Klasse zum laden einer Textdatei in ein array:
    Benötigt den Header filesize.h, der am ende des posts steht.

    Code: // CLoadFile_AK.h

    /*
     * This header file is free for private use
     * and was completely written by Alexander Korsunsky.
    */

    // CLoadFile(const char* FileName, char* BigArray, int MaxChars)
    // CLoadFile::load()

    //CLoadFile loads a file with a certain maximum filesize into an array

    /* USAGE:
     *
     * 1. Instance
     *
     * Declare an Instance of this class with the parameters
     * FileName, which has to contain the name of the file
     * BigArray and MaxChars
     * BigArray must be an Array that is big enough to
     * contain the whole Text File, and have MaxChars elements
     *
     * 2. Initialise by calling the function CLoadFile::load()
     *
     * 3. Exception handling
     *
     * CLoadFile::load() returns 0 if the operation worked,
     * CLoadFile::FILE_ERR if the file could not be loaded and
     * CLoadFile::SIZE_ERR if the file was bigger than MaxChars bytes
     *
     * A possible way of catching exceptions would be:

    int nErrorMessage;

    nErrorMessage = textfile.load();         
    if (nErrorMessage == CLoadFile::FILE_ERR )
       {
       printf ("The file could not be loaded.\n");
       }
    else if (nErrorMessage == CLoadFile::SIZE_ERR)
       {
       printf ("The file is too big\n");
       }
    */

    // DEPENDENCIES ///////////////////////////////////////////////////////////////
    /*
     * This file depends on the header file
     * filesize.h
    */


    #ifndef CLOADFILE

    #define CLOADFILE


    // INCLUDES ///////////////////////////////////////////////////////////////////
    #include <stdio.h>
    #include <stdlib.h>
    #include "filesize.h"

    // DEFINES ////////////////////////////////////////////////////////////////////
       /* Nothing, the constants are declared as public in the class*/


    class CLoadFile
    {

    // Variables //////////////////////////////////////////////////////////////////
          
    public:
       static const int FILE_ERR;
       static const int SIZE_ERR;
       static const int BLOCKSIZE;

    protected:

       //sz String, where the name of the file is stored
       char szFileName[256];

       //Defines the maximum amount of characters to be loaded from file
       int nMaxChars;

       //Pointer to the Array where the text is to be stored
       char* szTargetBuffer;
       
       //Pointer to the File
       FILE* flInFile;
       
       // Flag wich defines wether loading the file was succesfull
       bool fFileLoaded;



    // FUNCTION HEADERS, FUNCTIONS ////////////////////////////////////////////////
       
    public:

       // CONSTRUCTOR
       CLoadFile (const char* fileName, char* targetBuffer,int maxChar)
          {
    #ifdef DEBUG__
    printf ("CLoadFile opened\n");
    #endif //DEBUG__
             strncpy (szFileName, fileName, 255);
             szTargetBuffer = targetBuffer;
             nMaxChars = maxChar;      

          }

       // Function to initialize the class,
       // this function is to be called by user manually
       int load();

       //DESTRUCTOR
       ~CLoadFile()
          {
             if (fFileLoaded)
                {
                rewind(flInFile);
                fclose(flInFile);
                }
          }

    };//CLoadFile



    const int CLoadFile::FILE_ERR = 1;

    const int CLoadFile::SIZE_ERR = 2;

    const int CLoadFile::BLOCKSIZE = 32;








    int CLoadFile::load()
    {
    //How many characters were stored unnecessarily
    int nTooMuch;

    //How big is the file (bytes)
    int nFileSize;

    // Determine the Filesize and check how many chars were loaded unnecessarily
    nFileSize = FileSize(szFileName);
    nTooMuch = nFileSize % BLOCKSIZE;

    #ifdef DEBUG__
    printf ("CLoadFile::load() opened\n");
    #endif //DEBUG__
       //Load the file
       if (nFileSize < nMaxChars)
       {
          if (( flInFile = fopen (szFileName, "r") ) == NULL)
             {
             fFileLoaded = 0;
             return FILE_ERR;
             }

          fFileLoaded = 1;
    #ifdef DEBUG__
    printf ("File opened\n");
    #endif //DEBUG__   
       } //if (iFileSize < maxChar)
       else
       {
          fFileLoaded = 0;
          return SIZE_ERR;
       } // else (iFileSize < maxChar)

    #ifdef DEBUG__
    int k = 0;
    #endif //DEBUG__   

       //Read the file into a string blockwise
       do {
    #ifdef DEBUG__
    k++;
    #endif //DEBUG__
       fread (szTargetBuffer, BLOCKSIZE, 1, flInFile);
       szTargetBuffer = szTargetBuffer + BLOCKSIZE;

    #ifdef DEBUG_BLOCKS
    if ( (k%50) == 0 )
    printf ("Block %d loaded\n", k);
    #endif //DEBUG_BLOCKS

       } while ( feof(flInFile) == 0 );


       /*
        * Quick and dirty way to correct the last characters of
        * the text stored in szTargetBuffer
       */
       memset (&szTargetBuffer[nFileSize-1], '\0', nTooMuch);

       return 0;
    }

    #endif //#ifdef CLOADFILE



    Code: //filesize.h

    /*
     * Taken from http://www.codeproject.com/
     *
     * PLEASE NOTE that this is not my own work.
     * This function was written by Christopher Diggins, http://www.cdiggins.com/
     * and published on http://www.codeproject.com/file/filesize.asp
    */

    #ifndef FILESIZE_H

    #define FILESIZE_H

    #include <fstream>
    unsigned int FileSize(const char* sFileName)
    {
      std::ifstream f;
      unsigned int filesize=0;
     

      f.open(sFileName, std::ios_base::binary | std::ios_base::in);
      if (!f.good() || f.eof() || !f.is_open()) 
         return 0;
     
      f.seekg(0, std::ios_base::beg);
     
      std::ifstream::pos_type begin_pos = f.tellg();
     
      f.seekg(0, std::ios_base::end);
     
      filesize = static_cast<unsigned int>(f.tellg() - begin_pos);
      f.close();
      return filesize;
    }

    #endif //FILESIZE_H



    Re: Funktionsbibliotheken - C/C++

    exbs - 06.01.2007, 19:48


    zwar net sooon riesen ding aba klein und nützlich :wink:

    Wer mal Zeit braucht:

    Code: /* Funktion um eine Pause von n Sekunden in das Programm einzubauen.*/

    #include <windows.h>

    void wait(double sec)
    {
        const DWORD dwWait = GetTickCount();
        while(GetTickCount() - dwWait < sec * 1000);
    }

    Das war in PASCAL mal was gutes:

    Code: /* Funktion um den Courser an eine bestimme Stelle zu setzen (x, y) */

    #include <windows.h>

    void gotoxy(int x, int y)
    {
       COORD cur = {x, y};
       SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
    }

    was diese funktionen jetzt nicht beinhalten sind gewissen aspekte ( ;) ) der fehlerbahandlung.

    [edit] <windows.h> is kla, dass das net unter linux oda sonnst einem zu windoof konkurirendem bs futzt.



    Re: Funktionsbibliotheken - C/C++

    DrPhil_Guth - 06.01.2007, 20:16


    Und hier die portablere variante, ich denke is auch besser wenn man sich nicht auf windows.h verlässt...
    Is aber nicht von mir, hab ich aus irgend einem tutorial.

    Code: #include <time.h>

    void warten (int sekunden)
        {
            clock_t dauer;
            dauer = clock() + sekunden * CLOCKS_PER_SEC ;
            while (clock() < dauer) {}
        }



    Re: Funktionsbibliotheken - C/C++

    exbs - 06.01.2007, 22:20


    Zitat: Und hier die portablere variante, ich denke is auch besser wenn man sich nicht auf windows.h verlässt... Warum ?

    da ich mich für winapi interessiere liegt natürlich die windows version näher;).



    Mit folgendem Code, können Sie den Beitrag ganz bequem auf ihrer Homepage verlinken



    Weitere Beiträge aus dem Forum Tutorials.at

    Euer Alter - gepostet von geek.90 am Sonntag 20.05.2007
    Datei einlesen - gepostet von Blümchen am Mittwoch 07.02.2007
    c-Programme mit psp - gepostet von Elch am Donnerstag 13.09.2007
    Welche Programmiersprache für Kassenprogramm - gepostet von mikeingo am Freitag 02.11.2007
    C++ schwer? - gepostet von DrPhil_Guth am Samstag 03.06.2006
    CS-Video ? - gepostet von exbs am Freitag 03.11.2006
    IRC - gepostet von Dragorad am Freitag 15.06.2007
    Blender-Problem - gepostet von toby-man am Samstag 06.10.2007
    Gleichen in gleichungen. - gepostet von CRASH am Samstag 24.02.2007



    Ähnliche Beiträge wie "Funktionsbibliotheken - C/C++"