| | |
Writing To a File in Binary Mode
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include<fstream.h> #include<string.h> using namespace std; class emp { public: char *per; void write() { ofstream out("ss.txt",ios::app | ios::binary); cout<<"Enter some character ....\n"; cin>>*per; //out.write(per,sizeof(per)); out.write(per,strlen(per)); } }; void main() { emp e; e.write(); }
Last edited by Ancient Dragon; Aug 11th, 2008 at 12:53 pm. Reason: add code tags
•
•
Join Date: Mar 2008
Posts: 1,429
Reputation:
Solved Threads: 115
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
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
I need pageviews! most fun profile ever :)
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.
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.
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.
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.
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
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 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)
#include<fstream.h> #include<iostream.h> #include<string.h> using namespace std; class emp { public: char *per; void write() { ofstream out("ss.txt",ios::app | ios::binary); cout<<"Enter some character ....\n"; cin>>*per; //out.write(per,sizeof(per)); out.write(per,strlen(per)); } }; int main() { emp e; e.write(); return 0; }
Last edited by Ancient Dragon; Aug 11th, 2008 at 12:57 pm. Reason: add code tags -- second time.
•
•
Join Date: Mar 2008
Posts: 1,429
Reputation:
Solved Threads: 115
>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.
What ?!, are you serious. There are so many places on this site that fully explains how they work.. Try reading the link I posted.
I need pageviews! most fun profile ever :)
>>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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 0
Trying to post again; using code tags
I have used char *per because 'write' function only accepts pointer to character.
C++ Syntax (Toggle Plain Text)
#include<fstream.h> #include<iostream.h> #include<string.h> using namespace std; class emp { public: char *per; void write() { ofstream out("ss.txt",ios::app | ios::binary); cout<<"Enter some character ....\n"; cin>>*per; //out.write(per,sizeof(per)); out.write(per,strlen(per)); } }; int main() { emp e; e.write(); return 0; }
I have used char *per because 'write' function only accepts pointer to character.
>>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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Writing PCM as raw data in C/C++ in Windows (C++)
- C++ How to write strings to a Binary file? (C++)
- Problems writing stdout to a file, please help! (Perl)
- Saving to a file from an array in C ? (C)
- Reading binary data from a file and writing it (Visual Basic 4 / 5 / 6)
- file types: eliminating leading white spaces (C)
- Windows API functions to read and write files in C (C)
Other Threads in the C++ Forum
- Previous Thread: How to count the number of charachters a user has inputted?
- Next Thread: Need help on a sorting algorithm function.
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






