I make a lab in the university. There are structures whose data is entered from the keyboard, then written to the file using the fwrite() function. Until now, he worked only with fstream , and there were no problems. Here, too, it seems like I did everything correctly, but I get the device.txt file on the exhaust, and in it there are solid kryakozyabry. I collect from under g ++ Linux. The whole program is on the https://github.com/rotenbergwitalik/laba6.git
code with main:
#include <iostream> #include <cstdio> #define DEVICE_NAME_LEN 30 #define MEASUREVALUE_NAME_LEN 30 using namespace std; struct powerSource { float voltage; float amperage; float periodicity; }; struct measuredValue { char measuredValueName[MEASUREVALUE_NAME_LEN]; float lowBorder; float highBorder; float inaccuracy; }; struct device { char deviceName[DEVICE_NAME_LEN];// float devicePrice; int guarantee; //in month struct powerSource devicePowerSource; struct measuredValue deviceMeasureValue; }; int main() { struct device labaDevice; cout<<"Write name of device: "; cin>>labaDevice.deviceName; cout<<"Write price of device in $: "; cin>>labaDevice.devicePrice; cout<<"Write guarantee in month: "; cin>>labaDevice.guarantee; cout<<"Write power source for device: "<<endl; cout<<"\tVoltage: "; cin>>labaDevice.devicePowerSource.voltage; cout<<"\tAmperage: "; cin>>labaDevice.devicePowerSource.amperage; cout<<"\tPeriodicity: "; cin>>labaDevice.devicePowerSource.periodicity; cout<<"Write measure value for device: "<<endl; cout<<"Write name of measure value: "; cin>>labaDevice.deviceMeasureValue.measuredValueName; cout<<"\tLow border: "; cin>>labaDevice.deviceMeasureValue.lowBorder; cout<<"\tHigh border: "; cin>>labaDevice.deviceMeasureValue.highBorder; cout<<"\tInaccuracy: "; cin>>labaDevice.deviceMeasureValue.inaccuracy; FILE* outFile; if ((outFile = fopen("device.txt", "wb")) == NULL) { cout<<"Con not open the file"<<endl; return 1; } fwrite(&labaDevice, sizeof(device), 1, outFile); fclose(outFile); return 0; }