Unexpected Behaviors in the following Program. Why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Unexpected Behaviors in the following Program. Why?

Here is a program I created yesterday in my school. https://code.sololearn.com/cqfe6f4AzLw0/#cpp I don't understand some of the things, why do they happen like this. They are: 0) See the WriteToFile function. There I have simply done this to write the data to the binary file: fout.write((char*)&arr[i],sizeof(arr[i])); What I see this as is that this is an explicit conversion of a class Memory to char*. How is that possible? Did I just converted the memory to Bytes using this? Also, Putting the brackets in the functional form doesn't work here. Why? 1) In my ReadFromFile Function, the cursor in the file never reaches eof(). I created another test program to read all Entries contained and found that in the end, tellg() was returning -1, and the Data there was exactly the same as the last entry. Is is possible for the cursor to be at -1?

29th Jul 2017, 7:32 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
10 Answers
+ 11
According to http://www.cplusplus.com/reference/ostream/ostream/write/ http://www.cplusplus.com/reference/istream/istream/read/ ostream& write (const char* s, streamsize n) "Inserts the first n characters of the array pointed by s into the stream, without checking its contents." istream& read (char* s, streamsize n); "Extracts n characters from the stream and stores them in the array pointed to by s, without checking its contents nor appending a null character at the end." When I ran your code on desktop and inserted a block of data, the .dat file stored all data accordingly, with int rn and double tm crypted. If I'm not mistaken, every element inside the struct has its buffer casted to char* and then written to the .dat file. As for tellg() returning -1, it probably has something to do with doing fin.seekg(32,fin.cur); and then fin.seekg(-33,fin.cur).
29th Jul 2017, 5:44 AM
Hatsy Rei
Hatsy Rei - avatar
+ 9
According to: https://stackoverflow.com/questions/3682152/problem-with-istreamtellg https://www.gamedev.net/forums/topic/304921-fixed-files-with-unix-style-newlines-result-in-tellg-returning-negative/ Either if you replace your fstream object with ifstream object and call tellg(), or you fix the newline issue, it should return the correct position. This is really tricky because file streams deals closely with the OS. It is said that tellg() specifically returns -1 (not other negative values) if errors are met.
29th Jul 2017, 8:18 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
off topic. but will your teacher even understand 😊
29th Jul 2017, 7:45 AM
jay
jay - avatar
+ 5
Yes. I see that happening also. I am unable to write an uncorrupted file. the first entry is fine. everything else seems corrupt. I had to change while(!(fin.eof()) && fin.tellg != -1) to int len = fin.tellg(); while(!(fin.eof()) && len != -1) just to compile. I am going to try to write it as plain text and see if I can figure out why
29th Jul 2017, 8:06 AM
jay
jay - avatar
+ 5
mmm The main program runs fine now. my bad re corrupt files. It was reading from wrong location. I was getting three entries returned when reading the file (main program) but now it returns two results as expected... i will post it for you to test out.
29th Jul 2017, 8:16 AM
jay
jay - avatar
+ 4
@jay I want to understand the reason for the strange output, why the -1 cursor happens, in the first place. My teacher used the same method, read using char* casting, but was unable to explain these outputs... In the end, I was told to either read only finite objects, or stop displaying the last item where tellg becomes -1. Thus I posted this question to get more info...
29th Jul 2017, 7:56 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
@Hatsy Rei Thank You for the solutions. I first tried using ifstream instead of fstream, but the -1 C Pos is still shown on console. My program was created on a Win7 Ultimate PC in School and I have a Win7 Pro PC at Home. So the newline error shouldn't exist... I guess I will have to keep on including tellg()!=-1 to continue printing only correct results using the while loop. Ill try reading more on fstream and its working to understand where my data gets corrupted.
29th Jul 2017, 8:34 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Thank You! So what the char* casting does is convert the data of integers, etc of a class to respective bytes for storing in the binary file... Am I right? But, why does this fail? Student s; file.read(char*(&s),sizeof(Student)); //Isnt this casting as well? In my test program, all I do is read the file using the read() function and print the current position of the cursor using tellg(), all packed in a while loop that runs till eof is reached. I don't use seekg() at all. https://code.sololearn.com/cPZ9g8E4H0g1/#cpp But the last entry always shows the cursor at -1, no matter what. Why? I don't understand. Please help.
29th Jul 2017, 7:28 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Now the questions that remain are: 0) (char*)&s is casting... But is char*(&s) casting? 1) Even when seekg() is not used, tellg() returns -1 for the last entry (which was not written to the file by the user at all). Why? How can that happen? Isn't streampos supposed to belong to the Interval [0,n], where n is the size of the file in bytes?
29th Jul 2017, 8:00 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar