IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

file stream Segmentation fault


Sujet :

C++

  1. #1
    Membre régulier Avatar de J4e8a16n
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    271
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 271
    Points : 119
    Points
    119
    Par défaut file stream Segmentation fault
    Bonjour à tous,

    J'ai un Segmentation fault à l'entrée des fonctions displayAllStats(); et updateStat(index); et displayStat(index);

    Si, je n'utilise pas ces fonctions le reste du code va très bien.




    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <iostream>  
    #include <fstream>	
    #include <sstream>	
     
    ........
    int main()
    {
    std::cout << "3a" << std::endl;
        displayAllStats();
     
     
    .....
    }
     
    void displayAllStats()
    {
     
    	 std::cout << "1a line "<< __LINE__  << std::endl;
     
    	std::fstream ifile("tempStats.bin", std::ios_base::binary | std::ios_base::out | std::ios_base::out);
     
     std::cout << "1bline "<< __LINE__  << std::endl;
     
    ...........
    PROMPT

    3a
    1a line 52
    Segmentation fault


    Code complet:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
     
    #ifdef _c
    #error This source file is not C  but rather C++
    #endif
     
    /*
    cd /home/Another/softc++/files
    cd /home/Another/softc++/files
     
    x86_64-w64-mingw32-c++.exe   -Wall     -fmax-errors=5     -Wextra       -O2     tst.cpp  -o  tst.exe
    history -c
    */
    #include "stdafx.h"
    #include <iostream>   // For std::cout etc. 
    #include <fstream>	//file  stream classes:
    #include <sstream>
    #include "tempStat.h"
    /*
    typedef basic_istringstream string istringstream;
    typedef basic_ostream char* ostream;
    typedef basic_fstream char* fstream; */
     
     
    using std::cout;    // le programme utilise cout.
    using std::cin;     // le programme utilise cin.
    using std::endl;    // le programme utilise endl.
    using std::string;
     
    void updateStat(int index);
    void displayStat(int index);
    void displayAllStats();
    std::ostream &operator << (std::ostream &os, const tempStat &ts);
    std::istream &operator >> (std::istream &is, const tempStat &ts);
     
    //Insert formatted output (public member function )
    std::ostream &operator << (std::ostream &os, const tempStat &ts)
    {
      os << ts.minimum << "," << ts.maximum;
      return os;
    }
    //Extract formatted input
    std::istream &operator >> (std::istream &is, tempStat &ts)
    {
      is >> ts.minimum  >> ts.maximum;
      return is;
    }
     
     
    void displayAllStats()
    {
     
     
      std::cout << "1a line " << __LINE__  << std::endl;
      /*   std::fstream ifile;
      ifile.open ("tempStats.bin", std::ios_base::binary);
      if ( (ifile.rdstate() & std::fstream::failbit ) != 0 )
      {
      std::cerr << "Error opening 'tempStats.bin'\n";
      }
      else
      { */
     
      std::fstream ifile("tempStats.bin", std::ios_base::binary | std::ios_base::out | std::ios_base::out);
     
     
      std::cout << "1c line " << __LINE__  << std::endl;
      int index = 0;
      while(!ifile.eof())
        {
        tempStat ts ;
        ts.read(ifile);
     
        if (ifile.gcount() == 0)
          {
          break;
          }
     
        std::cout
            << "Index " << index
            << ", tempStat: " << ts
            << std::endl;
     
        index++;
        }
     
      ifile.close();
      /*   } */
     
     
    }
     
    void updateStat(int index)
    {
      tempStat ts ;
      std::cout << "Enter minimum and maximum temperatures: ";
      std::cin >> ts;
      std::cout << "4a" << endl;
      std::fstream ofile("tempStats.bin", std::ios_base::binary | std::ios_base::out | std::ios_base::in);
      std::cout << "4b" << endl;
      if (ofile.is_open())
        {
        ts.write(ofile, index);
        ofile.close();
        }
      else
        {
        std::cerr << "Couln't open tempStats.bin for writing." << std::endl;
        }
    }
     
    void displayStat(int index)
    {
      return;
      std::fstream ifile("tempStats.bin", std::ios_base::binary | std::ios_base::out | std::ios_base::in);
      if (ifile.is_open())
        {
        tempStat ts ;
        ts.read(ifile, index);
        ifile.close();
        }
      else
        {
        std::cerr << "Couln't open tempStats.bin for reading." << std::endl;
        }
    }
     
     
     
    /*
    history -c
    */
     
    int main()
    {
     
      std::cout << "Temperature utility." << endl;
      std::ofstream ofile;
      ofile.open ("tempStats.bin", std::ios_base::binary);
      if ( (ofile.rdstate() & std::ofstream::failbit ) != 0 )
        {
        std::cerr << "Error opening 'tempStats.bin'\n";
        }
      else
        {
        tempStat(-2.5, 7.5).write(ofile);
        tempStat(0, 9.9).write(ofile);
        tempStat(2.5, 11.0).write(ofile);
        tempStat(5.5, 14.5).write(ofile);
        tempStat(7.0, 17.7).write(ofile);
        tempStat(10.5, 23.7).write(ofile);
        tempStat(11.7, 29.5).write(ofile);
        tempStat(7.6, 22.9).write(ofile);
        tempStat(7.2, 21.5).write(ofile);
        tempStat(2.0, 16.0).write(ofile);
        tempStat(-4.7, 12.5).write(ofile);
        tempStat(-1.9, 8.5).write(ofile);
     
        ofile.close();
     
        std::cout << "Finished writing binary data to tempStats.bin." << std::endl;
        std::cout << "3a" << std::endl;
        displayAllStats();
        std::cout << "3b" << std::endl;
     
     
        std::cout << "Temp stats initially:" << endl;
        int index = 0;
        std::cout << "Which month?:" << std::endl;
        std::cin  >> index;
     
        if (index < 0 || index >= 12)
          {
          std::cerr << "Invalid index." << std::endl;
          }
        else
          {
          updateStat(index);
          displayStat(index);
     
          std::cerr << "Temp stats finally.." << std::endl;
          displayAllStats();
     
          }
     
        }
     
      return 0;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #ifdef _c
    #error This source file is not C  but rather C++.
    #endif
     
    #pragma once
    #include <iostream>	//  std::cin, std::cout, std::hex
    #include <string> //http://www.cplusplus.com/reference/string/string/?kw=string
     
     
    class tempStat
    {
    public:
    	double minimum, maximum;
    	static const size_t RECORD_SIZE = 2 * sizeof(double);
     
     
    	tempStat(double min = 0.0, double max = 0.0)
    	: minimum(min), maximum(max)
    	{}
     
    	void write (std::ostream& os)
    	{
    		os.write((char*)&minimum, sizeof(double));
    		os.write((char*)&maximum, sizeof(double));
    	}
    //can read input file stream or  input stream 
    	void read (std::istream& is)
    	{
    		is.read((char*)&minimum, sizeof(double));
    		is.read((char*)&maximum, sizeof(double));
    	}
     
    //write  tempStat  at specified index in file stream
    	void write (std::ostream& os, int index)
    	{
    		std::streampos pos(index * RECORD_SIZE);
     
    		os.seekp(pos); 
    		std::cout<< "About to write record at position"  << os.tellp() << std::endl;
     
    		os.write((char*)&minimum, sizeof(double));
    		os.write((char*)&maximum, sizeof(double));
    	}
     
    //read tempStat at specified index in file stream
    	void read (std::istream& is, int index)
    	{
    		std::streampos pos(index * RECORD_SIZE);
     
    		is.seekg(pos);
    		std::cout<< "About to read record at position" << is.tellg() << std::endl;
     
    		is.read((char*)&minimum, sizeof(double));
    		is.read((char*)&maximum, sizeof(double));
    	}
     
     
     
    	//plus others , as appropriate
    };
    Merci de votre attention,

    JPDaviau
    Petit Malin
    "accélérateur . . . qui pousse . . . un électron a passer par deux trous d’un écran en même temps." (Cyrille Burt: "C’est mieux qu’un fantôme") (Janus p.251)
    "Joy is to love what is, pain is to love what is not"
    )

    HP Pavilion Elite Desktop PC 570-p0xx - Window10 64 bits - Intel(R) Core(TM)2 Quad CPU Q8200 @ 3GHz x86_64-w64-mingw32-gcc-7.3.0.exe

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Déjà, est-ce que tu reproduits le problème quand tu compiles en -O0 ? Ne pas oublier qu'avec l'optimisation dans les compilateurs modernes, les comportements indéfinis peuvent "remonter le temps".
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Membre régulier Avatar de J4e8a16n
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    271
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 271
    Points : 119
    Points
    119
    Par défaut
    C'est beaucoup mieux !

    Je compilais avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    x86_64-w64-mingw32-c++.exe   -Wall     -fmax-errors=5     -Wextra       -O2     tst.cpp  -o  tst.exe
    Le:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    while(!ifile.eof())
        {
    .......
    dans displayAllStats() LIGNE 60 ne semble pas fonctionner.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    #ifdef _c
    #error This source file is not C  but rather C++
    #endif
     
    /*
    cd /home/Another/softc++/files
     
    x86_64-w64-mingw32-c++.exe   -Wall     -fmax-errors=5     -Wextra       -O2     tst.cpp  -o  tst.exe
    history -c
    */
    #include "stdafx.h"
    #include <iostream>   // For std::cout etc. 
    #include <fstream>	//file  stream classes:
    #include <sstream>
    #include "tempStat.h"
    /*
    typedef basic_istringstream string istringstream;
    typedef basic_ostream char* ostream;
    typedef basic_fstream char* fstream; */
     
     
    using std::cout;    // le programme utilise cout.
    using std::cin;     // le programme utilise cin.
    using std::endl;    // le programme utilise endl.
    using std::string;
     
    void updateStat(int index);
    void displayStat(int index);
    void displayAllStats();
    std::ostream &operator << (std::ostream &os, const tempStat &ts);
    std::istream &operator >> (std::istream &is, const tempStat &ts);
     
    //Insert formatted output (public member function )
    std::ostream &operator << (std::ostream &os, const tempStat &ts)
    {
      os << ts.minimum << "," << ts.maximum;
      return os;
    }
    //Extract formatted input
    std::istream &operator >> (std::istream &is, tempStat &ts)
    {
      is >> ts.minimum  >> ts.maximum;
      return is;
    }
     
     
    void displayAllStats()
    {
      std::fstream ifile;
      ifile.open ("tempStats.bin", std::ios_base::binary); 
       /*   std::fstream ifile("tempStats.bin", std::ios_base::binary | std::ios_base::out | std::ios_base::out);
     */
      if ( (ifile.rdstate() & std::fstream::failbit ) != 0 )
      {
      std::cerr << "Error opening 'tempStats.bin'\n";
      }
      else
      { 
      int index = 0;
      while(!ifile.eof())
        {
        tempStat ts ;
        ts.read(ifile);
    		std::cout << "dAS while line " << __LINE__  << std::endl;
        if (ifile.gcount() == 0)
          {
          break;
          }
    std::cout << "dAS line " << __LINE__  << std::endl;
        std::cout
            << "Index " << index
            << ", tempStat: " << ts
            << std::endl;
     
        index++;
        }
    std::cout << "dAS close line " << __LINE__  << std::endl;
      ifile.close();
       } 
     
     
    }
     
    void updateStat(int index)
    {
      tempStat ts ;
      std::cout << "Enter minimum and maximum temperatures: ";
      std::cin >> ts;
        std::fstream ofile("tempStats.bin", std::ios_base::binary | std::ios_base::out | std::ios_base::in);
     
       /*   std::fstream ofile;
      ofile.open ("tempStats.bin", std::ios_base::binary); 
       if ( (ofile.rdstate() & std::fstream::failbit ) != 0 )
      {
      std::cerr << "Error opening 'tempStats.bin'\n";
      } */
     
     
      if (ofile.is_open())
        {
        ts.write(ofile, index);
    	 std::cout << "uS line " << __LINE__  << std::endl;
        ofile.close();
        }
      else
        {
        std::cerr << "Couln't open tempStats.bin for writing." << std::endl;
        }
    }
     
    void displayStat(int index)
    {
     
       std::fstream ifile("tempStats.bin", std::ios_base::binary | std::ios_base::out | std::ios_base::in);
     
    	    /*   std::fstream ifile;
      ifile.open ("tempStats.bin", std::ios_base::binary); 
       if ( (ifile.rdstate() & std::fstream::failbit ) != 0 )
      {
      std::cerr << "Error opening 'tempStats.bin'\n";
      } */
    	 if (ifile.is_open())
        {
        tempStat ts ;
        ts.read(ifile, index);
    	 std::cout <<   "dS  line "<< __LINE__  << std::endl;
        ifile.close();
        }
      else
        {
        std::cerr << "Couln't open tempStats.bin for reading." << std::endl;
        }
    }
     
     
     
    /*
    history -c
    */
     
    int main()
    {
     
      std::cout << "Temperature utility." << endl;
      std::ofstream ofile;
      ofile.open ("tempStats.bin", std::ios_base::binary);
      if ( (ofile.rdstate() & std::ofstream::failbit ) != 0 )
        {
        std::cerr << "Error opening 'tempStats.bin'\n";
        }
      else
        {
        tempStat(-2.5, 7.5).write(ofile);
        tempStat(0, 9.9).write(ofile);
        tempStat(2.5, 11.0).write(ofile);
        tempStat(5.5, 14.5).write(ofile);
        tempStat(7.0, 17.7).write(ofile);
        tempStat(10.5, 23.7).write(ofile);
        tempStat(11.7, 29.5).write(ofile);
        tempStat(7.6, 22.9).write(ofile);
        tempStat(7.2, 21.5).write(ofile);
        tempStat(2.0, 16.0).write(ofile);
        tempStat(-4.7, 12.5).write(ofile);
        tempStat(-1.9, 8.5).write(ofile);
     
        ofile.close();
     
        std::cout << "Finished writing binary data to tempStats.bin." << std::endl;
     
        displayAllStats();
     
        std::cout << "Temp stats initially:" << endl;
        int index = 0;
        std::cout << "Which month?:" << std::endl;
        std::cin  >> index;
     
        if (index < 0 || index >= 12)
          {
          std::cerr << "Invalid index." << std::endl;
          }
        else
          {
          updateStat(index);
          displayStat(index);
     
          std::cerr << "Temp stats finally.." << std::endl;
          displayAllStats();
     
          }
     
        }
     
      return 0;
    }

    Merci,

    JPDaviau
    Petit Malin
    "accélérateur . . . qui pousse . . . un électron a passer par deux trous d’un écran en même temps." (Cyrille Burt: "C’est mieux qu’un fantôme") (Janus p.251)
    "Joy is to love what is, pain is to love what is not"
    )

    HP Pavilion Elite Desktop PC 570-p0xx - Window10 64 bits - Intel(R) Core(TM)2 Quad CPU Q8200 @ 3GHz x86_64-w64-mingw32-gcc-7.3.0.exe

  4. #4
    Rédacteur/Modérateur


    Homme Profil pro
    Network game programmer
    Inscrit en
    Juin 2010
    Messages
    7 115
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Canada

    Informations professionnelles :
    Activité : Network game programmer

    Informations forums :
    Inscription : Juin 2010
    Messages : 7 115
    Points : 32 967
    Points
    32 967
    Billets dans le blog
    4
    Par défaut
    - le code identé aléatoirement n'aide en rien à lire et comprendre le truc.
    - sais-tu utiliser eof ?
    eof ne fait que retourner si un flag a été levé, il n'indique pas quand le fichier est fini mais quand la dernière lecture a échoué pour cause de fichier fini.
    - es-tu sûr que tu testes avec les bonnes permissions, accès au bon fichier etc ?
    Pensez à consulter la FAQ ou les cours et tutoriels de la section C++.
    Un peu de programmation réseau ?
    Aucune aide via MP ne sera dispensée. Merci d'utiliser les forums prévus à cet effet.

  5. #5
    Membre régulier Avatar de J4e8a16n
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    271
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 271
    Points : 119
    Points
    119
    Par défaut
    Voilà, il ne reste qu'un petit bug.

    L'enregistrement ou la lecture de :
    Enter minimum and maximum temperaturesa: Via
    PROMPT
    Temperature utility.
    Finished writing binary data to tempStats.bin.
    Index 0, tempStat: -2.5, 7.5
    Index 1, tempStat: 0, 9.9
    Index 2, tempStat: 2.5, 11
    Index 3, tempStat: 5.5, 14.5
    Index 4, tempStat: 7, 17.7
    Index 5, tempStat: 10.5, 23.7
    Index 6, tempStat: 11.7, 29.5
    Index 7, tempStat: 7.6, 22.9
    Index 8, tempStat: 7.2, 21.5
    Index 9, tempStat: 2, 16
    Index 10, tempStat: -4.7, 12.5
    Index 11, tempStat: -1.9, 8.5
    dAS close line 59
    Temp stats initially:
    Which month?:
    7
    Enter minimum and maximum temperaturesa: Via
    7.7 7.7
    About to write record at position: 112
    uS line 76
    About to read record at position: 112
    dS  line 94
    Temp stats finally...
    Index 0, tempStat: -2.5, 7.5
    Index 1, tempStat: 0, 9.9
    Index 2, tempStat: 2.5, 11
    Index 3, tempStat: 5.5, 14.5
    Index 4, tempStat: 7, 17.7
    Index 5, tempStat: 10.5, 23.7
    Index 6, tempStat: 11.7, 29.5
    =====================
    Index 7, tempStat: 9.26842e-315, 7.7         Enter minimum and maximum temperaturesa: Via
    ==========================
    Index 8, tempStat: 7.2, 21.5
    Index 9, tempStat: 2, 16
    Index 10, tempStat: -4.7, 12.5
    Index 11, tempStat: -1.9, 8.5
    dAS close line 59


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    #include "stdafx.h"
    #include <iostream>   // For std::cout etc. 
    #include<istream>
    #include <fstream>	//file  stream classes:
    #include <sstream>
    #include <ctime>
    #include <typeinfo>
    #include "tempStat.h"
     
    using std::cout;    // le programme utilise cout.
    using std::cin;     // le programme utilise cin.
    using std::endl;    // le programme utilise endl.
    using std::string;
     
    void updateStat(int index);
    void displayStat(int index);
    void displayAllStats();
     
    //Insert formatted output (public member function )
    //http://www.cplusplus.com/reference/ostream/basic_ostream/operator-free/
    std::ostream &operator << (std::ostream &os, const tempStat &ts)
    {
      os << ts.minimum << ", " << ts.maximum;
      return os;
    }
    //Extract formatted input
    //  http://www.cplusplus.com/reference/istream/basic_istream/operator-free/
    std::istream &operator >> (std::istream &is, tempStat &ts)
    {
      is >> ts.minimum  >> ts.maximum;
      return is;
    }
     
     
    void displayAllStats()
    {
     
      std::ifstream ifile("tempStats.bin", std::ios_base::binary | std::ios_base::out | std::ios_base::out);
     
      int index = 0;
      while(ifile.eof() != 0)
        {
        tempStat ts ;
        ts.read(ifile);
        if (ifile.gcount() == 0)
          {
          break;
          }
     
        std::cout
            << "Index " << index
            << ", tempStat: " << ts
            << std::endl;
     
        index++;
        }
      std::cout << "dAS close line " << __LINE__  << std::endl;
     
      ifile.close();
    }
     
    void updateStat(int index)
    {
      tempStat ts ;
      std::cout << "Enter minimum and maximum temperaturesa: Via " << endl;
     
      std::cin >> ts; // Via std::istream& operator >>
     
      std::ofstream ofile("tempStats.bin", std::ios_base::binary | std::ios_base::out | std::ios_base::in);
     
      if (ofile.is_open())
        {
        ts.write(ofile, index);
        std::cout << "uS line " << __LINE__  << std::endl;
        ofile.flush();
        ofile.close();
        }
      else
        {
        std::cerr << "Couln't open tempStats.bin for writing." << std::endl;
        }
    }
     
    void displayStat(int index)
    {
      std::ifstream ifile("tempStats.bin", std::ios_base::binary | std::ios_base::out | std::ios_base::in);
     
      if (ifile.is_open())
        {
        tempStat ts ;
        ts.read(ifile, index);
        std::cout <<   "dS  line " << __LINE__  << std::endl;
     
        ifile.close();
        }
      else
        {
        }
    }
     
    int main()
    {
     
      std::cout << "Temperature utility." << endl;
      std::ofstream ofile;
      ofile.open ("tempStats.bin", std::ios_base::binary);
      if ( (ofile.rdstate() & std::ofstream::failbit ) != 0 )
        {
        std::cerr << "Error opening 'tempStats.bin'\n";
        }
      else
        {
        tempStat(-2.5, 7.5).write(ofile);
        tempStat(0, 9.9).write(ofile);
        tempStat(2.5, 11.0).write(ofile);
        tempStat(5.5, 14.5).write(ofile);
        tempStat(7.0, 17.7).write(ofile);
        tempStat(10.5, 23.7).write(ofile);
        tempStat(11.7, 29.5).write(ofile);
        tempStat(7.6, 22.9).write(ofile);
        tempStat(7.2, 21.5).write(ofile);
        tempStat(2.0, 16.0).write(ofile);
        tempStat(-4.7, 12.5).write(ofile);
        tempStat(-1.9, 8.5).write(ofile);
     
     
     
        ofile.flush();
        ofile.close();
     
        std::cout << "Finished writing binary data to tempStats.bin." << std::endl;
     
        displayAllStats();
     
        std::cout << "Temp stats initially:" << endl;
        int index = 0;
        std::cout << "Which month?:" << std::endl;
        std::cin  >> index;
        cin.clear();
        if (index < 0 || index >= 12)
          {
          std::cerr << "Invalid index." << std::endl;
          }
        else
          {
          updateStat(index);
          displayStat(index);
     
          std::cerr << "Temp stats finally..." << std::endl;
          displayAllStats();
     
          }
     
        }
     
      return 0;
    }
    Images attachées Images attachées  
    Petit Malin
    "accélérateur . . . qui pousse . . . un électron a passer par deux trous d’un écran en même temps." (Cyrille Burt: "C’est mieux qu’un fantôme") (Janus p.251)
    "Joy is to love what is, pain is to love what is not"
    )

    HP Pavilion Elite Desktop PC 570-p0xx - Window10 64 bits - Intel(R) Core(TM)2 Quad CPU Q8200 @ 3GHz x86_64-w64-mingw32-gcc-7.3.0.exe

  6. #6
    Membre régulier Avatar de J4e8a16n
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    271
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 271
    Points : 119
    Points
    119
    Par défaut
    ts.minimum, qui a la même adresse que ts n'enregistre pas la valeur minimum.
    Pourquoi?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    updateStat-&ts: 0x66fb50  ts: 9.26842e-315, 222 min: 9.26842e-315 &ts.min: 0x66fb50 max: 222 &ts.max: 0x66fb58 &ofile: 0x66f980
    J'ai inclus ceci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    std::cout << "problème line 122 Envoie  minimum maximum via ts à std::istream& operator >> \n Enter minimum and maximum temperature: " << endl;
    à la ligne qui précède std::cin >> ts; (environ ligne 123)
    c'est là qu'est l'erreur.

    JPD
    Petit Malin
    "accélérateur . . . qui pousse . . . un électron a passer par deux trous d’un écran en même temps." (Cyrille Burt: "C’est mieux qu’un fantôme") (Janus p.251)
    "Joy is to love what is, pain is to love what is not"
    )

    HP Pavilion Elite Desktop PC 570-p0xx - Window10 64 bits - Intel(R) Core(TM)2 Quad CPU Q8200 @ 3GHz x86_64-w64-mingw32-gcc-7.3.0.exe

  7. #7
    Membre régulier Avatar de J4e8a16n
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    271
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 271
    Points : 119
    Points
    119
    Par défaut
    Je vous le donne en 1000 !


    Un fichier windows corrompue.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Old value = 0
    New value = 1.0430033711752506e-309
    0x00007ffeb6c02f0c in msvcrt!memmove ()
       from /cygdrive/c/WINDOWS/System32/msvcrt.dll
    JPD
    Petit Malin
    "accélérateur . . . qui pousse . . . un électron a passer par deux trous d’un écran en même temps." (Cyrille Burt: "C’est mieux qu’un fantôme") (Janus p.251)
    "Joy is to love what is, pain is to love what is not"
    )

    HP Pavilion Elite Desktop PC 570-p0xx - Window10 64 bits - Intel(R) Core(TM)2 Quad CPU Q8200 @ 3GHz x86_64-w64-mingw32-gcc-7.3.0.exe

  8. #8
    Expert éminent sénior
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2005
    Messages
    5 074
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2005
    Messages : 5 074
    Points : 12 120
    Points
    12 120
    Par défaut
    Un fichier windows corrompue.
    Non, c'est juste qu'à force de faire des conneries, vous vous êtes pris un mûr, implanter des les librairies du système, ici la C-Runtime.
    Le problème est dans VOTRE code à 99,999999 %.

  9. #9
    Rédacteur/Modérateur


    Homme Profil pro
    Network game programmer
    Inscrit en
    Juin 2010
    Messages
    7 115
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Canada

    Informations professionnelles :
    Activité : Network game programmer

    Informations forums :
    Inscription : Juin 2010
    Messages : 7 115
    Points : 32 967
    Points
    32 967
    Billets dans le blog
    4
    Par défaut
    Vu le taux d'utilisation de memmove, s'il était vraiment bugué dans ton système, tu aurais d'autres problèmes bien plus importants que ton erreur de lecture.
    Pensez à consulter la FAQ ou les cours et tutoriels de la section C++.
    Un peu de programmation réseau ?
    Aucune aide via MP ne sera dispensée. Merci d'utiliser les forums prévus à cet effet.

  10. #10
    Membre régulier Avatar de J4e8a16n
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    271
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 271
    Points : 119
    Points
    119
    Par défaut
    Reste qu'après un fsc /scannow cela s'est mis à fonctionner du coup.
    Petit Malin
    "accélérateur . . . qui pousse . . . un électron a passer par deux trous d’un écran en même temps." (Cyrille Burt: "C’est mieux qu’un fantôme") (Janus p.251)
    "Joy is to love what is, pain is to love what is not"
    )

    HP Pavilion Elite Desktop PC 570-p0xx - Window10 64 bits - Intel(R) Core(TM)2 Quad CPU Q8200 @ 3GHz x86_64-w64-mingw32-gcc-7.3.0.exe

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Debutant , gestion d'une file , segmentation fault
    Par Freedom57 dans le forum Débuter
    Réponses: 4
    Dernier message: 22/12/2010, 16h52
  2. [SDL_Image] Img_Load : segmentation fault ....
    Par Mathieu.J dans le forum OpenGL
    Réponses: 6
    Dernier message: 19/10/2004, 23h52
  3. [REDHAT] Segmentation fault systematique
    Par mela dans le forum RedHat / CentOS / Fedora
    Réponses: 2
    Dernier message: 21/09/2004, 06h05
  4. Réponses: 13
    Dernier message: 13/07/2004, 15h41
  5. Comment contrer la "segmentation fault" ?
    Par guillaume_pfr dans le forum C
    Réponses: 15
    Dernier message: 08/08/2003, 13h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo