|
This makes sense. Thank you very much
|
|
|
|
|
I would like to test whether a stream was opened for binary read/writes or for plain text. The goal of my code is to output the data in a MyClass object in two different ways depending on this stream property.
class MyClass {
friend ostream& operator <<(ostream& stream, const MyClass& obj);
friend istream& operator >>(istream& stream, MyClass& obj);
}
ostream& operator <<(ostream& stream, const MyClass& obj) {
if ([stream is opened for binary writes]) {
...
} else {
...
}
}
istream& operator >>(istream& stream, MyClass& obj) {
if ([stream is opened for binary reads]) {
...
} else {
...
}
} Would doing such a thing be poor design, or is this pretty acceptable? Thanks,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
First: A stream is not binary or plain text, it is: do i want to treat incoming bytes as binary or as plain text. It is perfectly legal to open a stream containing plain text as binary. Next: you must open a stream with a flag that tells how you want to open it, so you already know if it's binary or not. Ergo: there's no need to test this.
But i guess i'm completely missing the point...
|
|
|
|
|
Ok, let me rephrase... one of the members of MyClass is an array. If the user wants to output my class in a human-readable form, I want to print the array as "[elem1, elem2, ... elemN]". If not, then I want to output the array length followed by each element in a binary representation. Now, when someone tries to output my class as "cout << MyClassInstance", it should be the human-readable version. When serializing the class to a file for later reloads, I want to output the binary version. How can I infer which version to use? I could conceivably write a specific implementation for ifstream and ofstream so I always read/write the non-human-readable version to a file, but that isn't really what I am trying to do. I could also write a specific method to serialize vs. just outputting my class, but again, I don't know what the standard way of doing this is. Thanks for any insight,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
std::ios_base::open_mode can tell you how the file stream was opened. The values are listed in xiosbase.h [edit] in VS.
modified on Monday, February 1, 2010 8:42 AM
|
|
|
|
|
After reading your post, I assumed that I could just use the statement:
bool const isBinary = ((out.flags() & ios_base::binary) != 0);
However, after testing this it appears that the ios_base::open_mode is not stored in the return value of "ostream::flags()". I have been unable to figure out how to get something from an ostream object that tells me the ios_base::open_mode used. Any idea how to extract this information after the file has been opened? Thanks,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Wow. 9 months later. Internet been a bit slow?
I'm sorry, I also incorrectly assumed the mode would be accessible somewhere within the stream obj.
Other than scanning the array data for CR,LF combos, or trying different read/write modes and seeing what fails and what succeeds, I don't how to determine how the stream was opened.
|
|
|
|
|
Yeah, clearly I get sidetracked easily. Anyway, I ultimately determined that opening a file in binary mode doesn't really do what I thought it did... I was under the impression that doing so would force the shift operator to output data in binary, which it doesn't. Therefore, I decided to implement two methods: one to output the data in binary, and the other to output it in ASCII format. Thanks for the help, though.
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Hello @ all,
i want to change the color in the titlebar,i mean the background.
Can anyone help me please?
|
|
|
|
|
If you're talking about the 'titlebar' of the app - as in the window frame, you'll need to handle WM_NCPAINT.
|
|
|
|
|
Hello and thanks,
may you can explain more exactly how i can do that
|
|
|
|
|
|
Sorry, but this is not exactly what i'am searching for!
I need help for window form painting, i mean that i want to color the titlebar
|
|
|
|
|
Okay, since you're working with forms you'd perhaps be better over here
|
|
|
|
|
I'am really, really sorry, but i can't find what you mean. I have searched the whole last link, the whole forum , but i can't find what i need. Please,please try again.
I need this for my recent project!SOS SOS SOS
|
|
|
|
|
|
Friends,
I want to read the data from Clip Board. what are the API's Avaialble for it in C++ . Can some one tel me Please.... Iam doing a Project on it ....
Thanks in Advance....
|
|
|
|
|
|
If you are doing a project on it, then your starting point should always be the MSDN reference[^], in order to get the basics of what is possible. I appreciate that MSDN is not always that easy to understand but you will at least learn the names of all the clipboard functions.
MVP 2010 - are they mad?
|
|
|
|
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STOP 0
#define TRUE 1
#define OK 1
#define FILE_NAME ("my.text")
typedef struct List
{
long lNum;
char cpsName[30];
long lCScore;
long lMathScore;
long atol(long i);
struct List *next;
}Stu;
Stu stuList[3] =
{
{1, "asd", 32, 44, 0},
{2, "ffg", 66, 66, 0},
{3, "dgd", 55, 44, 0},
};
Stu *CreateList()
{
Stu *head = NULL;
Stu *NewNode = NULL;
int i = 0;
for (i=0; i<3; i++)
{
NewNode = (Stu*)malloc(sizeof(Stu));
NewNode->lNum = stuList[i].atol(stuList[i].lNum);
strncpy(NewNode->cpsName, stuList[i].cpsName, 30);
NewNode->lCScore = stuList[i].atol(stuList[i].lCScore);
NewNode->lMathScore = stuList[i].atol(stuList[i].lMathScore);
NewNode->next = NULL;
if (NULL == head)
{
head = NewNode;
}
else
{
head->next = NewNode;
head = NewNode;
}
}
return head;
}
int SaveToFiles(Stu *head)
{
Stu *p = head;
FILE *fp;
char cFileName[1024] = {0};
if ((fp = fopen(FILE_NAME, "wb")) == NULL)
{
printf( "Open file error!\n" );
exit (1);
}
while (p != NULL)
{
if (fwrite(p, sizeof(Stu), 1, fp) == 1)
{
p = p->next;
}
else
{
printf("Write file error!\n");
exit(1);
}
}
fclose(fp);
printf( "Save file succeed!\n" );
return OK;
}
int OpenFiles()
{
char cFileName[256] = {0};
FILE *fp;
if (0 == (fp = fopen(FILE_NAME, "r")))
{
printf("Open file error!\n");
exit(0);
}
fgets(cFileName, 250, fp);
printf("%s", cFileName);
fclose(fp);
return OK;
}
int main()
{
Stu *head = NULL;
head = CreateList();
SaveToFiles(head);
OpenFiles();
return 0;
}
this is the error information:
error LNK2001: unresolved external symbol "public: long __thiscall List::atol(long)" (?atol@List@@QAEJJ@Z)
Debug/file.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
|
|
|
|
|
Firstly, do not repost the question, but edit the original.
As to the error you get the statement long atol(long i); in your structure is not defined anywhere. You cannot declare a method in a structure without implementing it. You should add your own atol() method, or simply call the default library function. Also as I stated earlier your CreateList() function also has a problem.
MVP 2010 - are they mad?
|
|
|
|
|
Yeah!
fist, Createlist()function has a problem ?why?could you give me a detail!
second, I know atol()is libiary but if I use like this it also has a problem,why?
thank you for your reply!
|
|
|
|
|
wbgxx wrote: I know atol()is libiary but if I use like this it also has a problem,why?
Sentences like this are rising to the top of the list of most useless statements. If your program does not work in some way, saying "it has a problem", gives us no information to guess what may be wrong. Please try and explain what happens; do you get a compiler error, does it cause an exception, does it return the wrong result, etc?
However, having looked at the code again I see:
NewNode->lNum = stuList[i].atol(stuList[i].lNum);
OK, so let's correct it
NewNode->lNum = atol(stuList[i].lNum);
The atol() function takes a string of digits as input and returns its value as a long variable. Your statement is trying to pass a long as its input parameter which a) is wrong, and b) makes no sense; the value is already a long .
The other problem with Createlist() is that it returns the pointer to the last structure in your list rather than the first; you need to keep a copy of the first entry added to the head pointer.
MVP 2010 - are they mad?
|
|
|
|
|
Oh!Yes,I see!
very grateful for you !
|
|
|
|
|
(2) Simple - if atol is a library function then use it as such!!
declare your struct like this:
typedef struct List
{
long lNum;
char cpsName[30];
long lCScore;
long lMathScore;
struct List *next;
}Stu;
And use the function like this:
NewNode->lCScore = stuList[i].atol(stuList[i].lCScore);
NewNode->lCScore = atol(stuList[i].lCScore);
|
|
|
|
|