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;
} |
Partager