In the following code, the object is not being written into file in line 30 stfile.write((char*)this,sizeof(item)); When line 26 :*)&ob,sizeof(item)); is hidden the object is written into the file.

Please help!!!

fstream stfile;
class item
	int code;
	char name[25];
	int price;
	int qty;
	int getcode()
		return code;

	void GetNew()
//	  cout<<"\nENTER THE CODE";      //Unhide this when 
//	  cin>>code;                   //entering first iteminto the file   
	  cout<<"\n ENTER THE ITEM NAME";
	  cout<<"\n ENTER THE PRICE";
	  cout<<"\n ENTER STOCK";
	  item ob;
	  code=ob.getcode();code++;           //hide these three
	  stfile.seekp(0,ios::end);           //lines when entering
	  cout<<stfile.tellp()<<endl;        //first iten into the file
	  return ;
	  void show()
	  {	cout<<"\ncode " <<code
		    <<"\nname "
		    <<"\n price"
		    <<"\n quantity"

void ShowItem(int c)

	{item ob;*)&ob,sizeof(item));
	{perror("search"); break;}
	{;break;	 }
iamthwee commented: So many f.u.c.k ups -2
Ancient Dragon commented: Equalizer +21

Recommended Answers

All 12 Replies

You are not properly initializing your fstream. Try: "fooey.dat", ios::in | ios::out | ios::binary ); I'm still a bit dubious about what you are doing in your code. (There's nothing wrong with writing an object to file like you are; it's just that your code looks really scattered.)

Hope this helps.

[EDIT] BTW. How did this thread get a four-star rating from only ~40 views and no responses?

Sorry i forgot to mention I am opening the file in the main() function"stock.txt",ios::in|ios::out); And those tellp() functions I used just to see if the file is being actually written into the file.

What baffles me is that the object is being written into the file when the statement that reads an object from file (line 26 in the snippet given in question) is removed ( making it a comment).

> gets(name);
Why are you using this AWFUL C function in a C++ program?
Consider using cin.getline(), which as well as being C++ proper, also protects your char array from buffer overflow.

It will also fail if the file "stock.txt" doesn't exist before you start, since there is nothing to read and you haven't reset the error condition.

"Stock.txt" exists and has 1 object in it.
I wrote it by hiding the read line mentioned in the question and my last post.
How many times should I say that?:@
That's the reason I'm asking this question in this thread. :S
Else I would have assumed that I have messed up with the syntax or logic somewhere!!!

@ Salem
The gets() function has nothing to do with reading and writing into a file I guess. Anyway, I have changed it to cin, if its a bad style to use C functions. [I'm curious to know if there is indeed something awful with that function!]

This is driving me nuts. I'm stuck because I cannot go ahead with my program without writing this into the file.

One more thing the ios::fail flag is set after the write function call. But ios::bad is not set.

gets() has no means of preventing buffer overflow - pure and simple.

> The gets() function has nothing to do with reading and writing into a file I guess.
True, but that doesn't stop us commenting on other horrors in your code.

Karkaroff, we're trying to help you. There is no need to get snippy.

I've spent some time debugging this for you. The problem is that the compiler is tripping over the type of the offset value when you seek to the last record on line 25. Change it to stfile.seekg( -(std::streamoff(sizeof(item))), ios::end ); That should get you rockin'.

Also, when I wrote "fooey.dat", ios::in | ios::out | [B]ios::binary[/B] ); i meant it. Play with fire and you'll get burnt. Keep your hand in the fire and you'll get toasted.

[EDIT] Nice links, iamthwee. Karkaroff hasn't violated any serialization rules though...

Oh yeah, while I'm still thinking about it (for the nth time), replace that gets(name) on line 19 with cin.get( name, sizeof( name ) ); Hope this helps.

I have changed that gets() function then itself(See post #6, reply to Salem). I just wanted to know if gets() gets into fstream's way ;)

I have added the binary mode too into open() .
I didn't get you(Duoas) the first time you told that.I thougt you were asking me to open a file before trying to read and write from it!!!

I was not able to use that statement Duoas asked me to.
compiler couldnot recognize std.
I tried using the statement using namespace std; and got error : declaration syntax error.
The IDE does not recognize using as a keyword either.
Though I fixed the problem by first taking the value of sizeof into an integer variable and then passing this variable inside seekg().(How it worked I dont know! Maybe it helped in type conversion).
Opening file in binary mode was particularly useful to read the objects from file
(or is it unserializing the objects from the file?)
The size of all objects were not the same in text mode.(Why? I don't know)

In short, the problem is solved. And I would be glad if someone would help understand how it got solved!!! :-/
Please try to answer the questions I've asked in this post when you explain what has really happened!!
I'm sorry I got a bit angry at you!

Don't worry about it.

I'm not sure exactly why the problem occurs either. But the type was being mangled incorrectly without the explicit cast.

I added a tellg() statement after the seekg() to see where the fp was, and I was getting some really large number, which showed me that it was not seeking from eof properly. So I checked the type of sizeof() against what the function wanted in order to find the error. I think that the problem is one of those "gotchas" from using templated objects.


For the namespace problem, it sounds like you have already used something like using namespace fooey; or using namespace std; or one of the headers you imported did it for you, so if you try to say using namespace std; again it looks in the local namespace and says, basically, "std doesn't have any namespace called 'std'".

You can fix it by explicitly referring to the global namespace: stfile.seekg( -([B]::[/B]std::streamoff(sizeof(item))), [B]::[/B]ios::end ); or by (if you are using std) just stfile.seekg( -(streamoff(sizeof(item))), ios::end ); That could fix it. (Of course, your solution, by using an intermediate/temporary int works as well [obviously], because the compiler could properly promote the int type to streamoff.)

For the binary stuff, try running your program without the binary I/O and entering the number 10 in answer to either price and/or stock. You'll see that it doesn't write 40 bytes, but 41 or 42. This is because 10 is the ASCII code for '\n', which gets turned into '\r\n' on Windows systems!

Enjoy. :)

commented: thanks! that clears things a bit +1

Thanks Duoas!

And everyone who tried to help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.