943,752 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2901
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 11th, 2008
0

Writing To a File in Binary Mode

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include<fstream.h>
  2. #include<string.h>
  3. using namespace std;
  4. class emp
  5. {
  6. public:
  7. char *per;
  8. void write()
  9. {
  10. ofstream out("ss.txt",ios::app | ios::binary);
  11. cout<<"Enter some character ....\n";
  12.  
  13. cin>>*per;
  14. //out.write(per,sizeof(per));
  15. out.write(per,strlen(per));
  16.  
  17. }
  18.  
  19. };
  20.  
  21. void main()
  22. {
  23. emp e;
  24. e.write();
  25.  
  26. }
Why this is not working?
Last edited by Ancient Dragon; Aug 11th, 2008 at 12:53 pm. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008
Aug 11th, 2008
0

Re: Writing To a File in Binary Mode

Oh boy, where to start..
First, learn to use code tags, its not difficult.
Give more details on your problem, and show the errors you are getting.
Dont use void main.
Try posting this again, but this time.. properly
and then I will help you with your problem.

Read this thread which you should have read anyway before posting.
Read This Before Posting
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 11th, 2008
0

Re: Writing To a File in Binary Mode

What errors are you getting?
Why isn't your code in [code] tags?
... or indented for that matter?

You should post this along with your compiler.

You're trying to use cout without #including <iostream>. That won't work I guess.

You're #including <string>, but then using a <char*> instead of a <string>!

On top of that, you're declaring a pointer that's pointing to ANY place in the memory and ordering cin to write the input to that? I think that's what you were trying to do.

I don't know how cin and char* work together, but it's better to make per a <string> instead of a <char*>, so you won't run into memory troubles.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Aug 11th, 2008
0

Re: Writing To a File in Binary Mode

EDIT:

cin >> <char*> should work, but be sure to first make the <char*> point to some allocated memory with the new operator. And keep in mind that it won't parse spaces.

To parse spaces as well, use cin.getline();

Your output mechanism should work, but you might want to get rid of ios::app, just try it with ios::binary alone.
Last edited by Clockowl; Aug 11th, 2008 at 8:07 am.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Aug 11th, 2008
0

Re: Writing To a File in Binary Mode

Click to Expand / Collapse  Quote originally posted by Clockowl ...
What errors are you getting?
Why isn't your code in [code] tags?
... or indented for that matter?

You should post this along with your compiler.

You're trying to use cout without #including <iostream>. That won't work I guess.

You're #including <string>, but then using a <char*> instead of a <string>!

On top of that, you're declaring a pointer that's pointing to ANY place in the memory and ordering cin to write the input to that? I think that's what you were trying to do.

I don't know how cin and char* work together, but it's better to make per a <string> instead of a <char*>, so you won't run into memory troubles.
I am just trying to learn thing My self. Sorry for the improper coding standard.

I really don't understand, what you both mean by saying 'code tags'

I am using Borland C++ compiler.

I am trying to enter inputs from keyboard to a file in binary format. I am not getting any error instead, if i enter in keyboard as "sfdahjkllllllllg", it will be written in the ss.txt file as 's“A', now if i enter in keyboard as "asdfgeturit", then in file it will be written as 'a“A'

Let me repost the code again with modifications:-
C++ Syntax (Toggle Plain Text)
  1. #include<fstream.h>
  2. #include<iostream.h>
  3. #include<string.h>
  4. using namespace std;
  5. class emp
  6. {
  7. public:
  8. char *per;
  9. void write()
  10. {
  11. ofstream out("ss.txt",ios::app | ios::binary);
  12. cout<<"Enter some character ....\n";
  13.  
  14. cin>>*per;
  15. //out.write(per,sizeof(per));
  16. out.write(per,strlen(per));
  17. }
  18. };
  19.  
  20. int main()
  21. {
  22. emp e;
  23. e.write();
  24. return 0;
  25. }
Last edited by Ancient Dragon; Aug 11th, 2008 at 12:57 pm. Reason: add code tags -- second time.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008
Aug 11th, 2008
0

Re: Writing To a File in Binary Mode

>I really don't understand, what you both mean by saying 'code tags'
What ?!, are you serious. There are so many places on this site that fully explains how they work.. Try reading the link I posted.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 11th, 2008
0

Re: Writing To a File in Binary Mode

First: read the link william posted. It's important.

And...
I've said some more, also answer those questions. It doesn't make sense you're using a <char*> when you've included <string> for example.
Last edited by Clockowl; Aug 11th, 2008 at 8:20 am.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Aug 11th, 2008
0

Re: Writing To a File in Binary Mode

>>cin>>*per;
As mention before, per is a pointer that just points to some random memory location. and *per only references the first byte, not the entire string. Unless the purpose of this assignment is to learn character arrays and pointers, then you would be better off using std::string.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 12th, 2008
0

Re: Writing To a File in Binary Mode

Trying to post again; using code tags
  
C++ Syntax (Toggle Plain Text)
  1. #include<fstream.h>
  2. #include<iostream.h>
  3. #include<string.h>
  4. using namespace std;
  5. class emp
  6. {
  7. public:
  8. char *per;
  9. void write()
  10. {
  11. ofstream out("ss.txt",ios::app | ios::binary);
  12. cout<<"Enter some character ....\n";
  13.  
  14. cin>>*per;
  15. //out.write(per,sizeof(per));
  16. out.write(per,strlen(per));
  17. }
  18. };
  19.  
  20. int main()
  21. {
  22. emp e;
  23. e.write();
  24. return 0;
  25. }

I have used char *per because 'write' function only accepts pointer to character.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008
Aug 12th, 2008
0

Re: Writing To a File in Binary Mode

>>I have used char *per because 'write' function only accepts pointer to character.
False. You can put anything you want to there -- just typecast it to char*


But you still failed to correct the problem. per is still an unallocated pointer which will crash your program.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to count the number of charachters a user has inputted?
Next Thread in C++ Forum Timeline: Need help on a sorting algorithm function.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC