+ 1

Problems reading a binary file C++

I'm working with binary files, and I'm trying to read a binary file where it contains emails, my problem is, when I declare a variable in my class, and I try to search by sender, it doesn't work, somehow, it doesn't read the last register, but ONLY if I declare the variable, when I don't declare the variable, searching by sender works perfectly; the variable that I declared in my class, it doesn't have name collision, I only declared it, so... I should not have any problem, but I don't know why, when I declared a variable in my class, searching by sender doesn't works. This is a weird problem for me and is the first time I see that, declaring a variable, makes something useless. Here is the code: https://code.sololearn.com/caR2UIP9LAk4/#cpp NOTE: The most important parts are lines 51 (the horrible variable variable in class ElectronicEmail.hoo) and 242-256, class ReadEmail.cpp (where I read the binary file to find the sender and compare it with the sender that the user wrote). Also I'm working in QT.

26th Oct 2019, 7:51 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
1 ответ
+ 1
in C++ _______ #include<iostream.h> #include<fstream.h> //for handling files in cpp #include<stdlib.h> // structure goes here struct City{ char city_name[20]; float population; }; //main programs void main() { ofstream fout; // open file in output mode and binary fout("file.dat",ios::out | ios::binary); City c1; //execute the below lines in a loop to get more input gets(c1.city_name); // gets string input from user cin>>c1.population; // gets population from user fout.write((char*)&c1,sizeof(c1)); }
29th Oct 2019, 12:25 PM
Ahmed Zubayer (thisiszubayer)
Ahmed Zubayer (thisiszubayer) - avatar