File handling problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

File handling problem

Once I write data into the file I want to read the file while reading the file I am not getting the output as well as when I run program There is box come on screen says program stopped Here is the code https://code.sololearn.com/crr3gdXdw757/?ref=app

12th Jun 2020, 9:16 PM
Mr Robot
Mr Robot - avatar
8 Answers
+ 1
$ยข๐Žโ‚น๐”ญ!๐จ๐“ first time I run the program without comments which save my data into file.on second run I just want to read the data from the file.the program I mentiond is second time run.but not get me my data
13th Jun 2020, 4:02 AM
Mr Robot
Mr Robot - avatar
14th Jun 2020, 11:34 AM
Ayushi Patel
Ayushi Patel - avatar
+ 1
Saad Mughal Nice, Thanks for the update.
14th Jun 2020, 12:59 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
0
Saad Mughal , You have to create the file which you are trying to open. Uncomment the below code and check it should work. ofstream file; file.open("a.txt",ios::out); if(!file) { cout<<"Error in creating file.."<<endl; return 0; } cout<<"\nFile created successfully."<<endl; v.SetInsurncePolicy(); v.SetVehicleData(); file.write((char*)&v,sizeof(v)); file.close();
12th Jun 2020, 10:06 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
13th Jun 2020, 11:37 AM
Mr Robot
Mr Robot - avatar
0
Hello Saad Mughal, Sololearn c++ server deletes all the files created by your executable after execution of your file is finished, So if you run second time due to absence file you are getting the error. Here I have written a code to print the files and directory present into current working directory before and after the file creation. If you observe closely for the first time file list is not present and file name is present into the second list. Run file second time and check that file you have created was not there. https://code.sololearn.com/ctAqQFYG34PP/?ref=app
14th Jun 2020, 12:49 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
0
I get the solution and it's not you guys telling me. Its actually Some casting issue i store data as a String which cause problem while reading. So I change my string data type members to ch datatype and it's working good. So that's the actual problem file.write((char*)&v,sizeof(v); this line have some casting issue
14th Jun 2020, 12:53 PM
Mr Robot
Mr Robot - avatar
14th Jun 2020, 2:18 PM
Mr Robot
Mr Robot - avatar