|
I have a text file which contains a list of sets of four floating point numbers. They are arranged like this, each * representing some number in the file:
* *
* *
* *
* *
.
.
.
and so on, but I don't know in advanced how many sets of four there are. I need to read them into a program using C++/VC++/MFC and I'm wondering if there is a "best" way to do it?
Currently I am using
<br />
while(fscanf(stream, "%lf %lf %lf %lf", a, a + 1, a + 2, a + 3) == 4)<br />
{...}<br />
where a is an array
double a[4]; .
I'm guessing this isn't a particularly good way to do it...
|
|
|
|
|
That's C. Read my iostream articles to see how it's done in C++
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Bring em in as a BYTE if possible. A WORD if not.
You can't do somthing faster than you can do nothing.
"Naked we come and bruised we go."
- James Douglas Morrison
Best Wishes,
ez_way
|
|
|
|
|
I have a static function and I want it to call a non-static function. The computer is telling me that I can not do this just by calling the non-static function outright.
Is there a way to call the nonstatic function from the static function?
|
|
|
|
|
Pass a pointer as a parameter of static function, call the non-static function by pointer.
|
|
|
|
|
If you need to, then the function should not be static.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
If it should not be static, so there are any problem.
You should modify your function, that can accept an argument (this pointer). So that, in the function, you can call none static function from this pointer.
For example:
class A{
static void Func(A* pA)
{
pA->NoneStaticFunc();
}
}
|
|
|
|
|
This is plain stupid. If you can only call a method when you have an instance of the class, the method should not be static. It in essence won't be static, it can only be called when you have an instance of the class to call it with, even if you don't call it from the class.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
No. I don't think so. According to C++ principle, static members should be shared with every instances of class. But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case.
|
|
|
|
|
nguyenvhn wrote:
According to C++ principle, static members should be shared with every instances of class
Wrong - a static method is visible to anyone who can see the class scope, even if no instance exists. In C++, it's also visible from the instance scope, although in C# it is not.
nguyenvhn wrote:
But in few case, we need an exception. C++ do not prevent me to implement such that. And here, the guy had a reason to have such case.
We've not even seen his code, or know what he's trying to do. The fact remains, if you need an instance of the class to make the code work, if it needs to call a non static method, then either the other method should be static, or the other method has state, making this method also stateful, and also a method that should not be static.
If a method does not make sense to call when there's no instance of the class, it should not be static, simple as that. Your solution makes it seem static, but it's not, it can't be called without an instance. A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Oh oh, In fact, you are right. I have nothing to protest.
Of course, a static member has class scope, so that it can call only others static members.
But in this case, the guy need a solution to adapt with his problem (it may be by historic). Because he needed to call a none static member, it was evident that there had to be exist an instance of class.
It is very clear that we shoud avoid such case but a trick in porogamming makes the life more funny;P and I like to solve that stuck
It is so interesting in reasoning with you.;P
|
|
|
|
|
In general, I agree with Christian on this.
Christian Graus wrote:
A design principle of C++ is to allow bad coding, and trust that coders will write good code, so they never have to fight the language. What you propose is possible, that doesn't make it good coding practice.
And Microsoft encourages this practice with Windows callback functions.
The callback (if implemented as part of a C++ class) has to be a static member, but in general, what's done is that there's a programmer-supplied value (usually void*) that can be passed as well as the function address.
In those cases the approach is to pass the address of an instance of the class, as has been previously suggested.
Steve S
Developer for hire
|
|
|
|
|
Does anybody here has a tutorial or a link to a tutorial of
Visual C++.NET 2003?
|
|
|
|
|
Recently upgrading to VC++ .NET 2003, a logging utility using std::ofstream has broken. When outputting a string, the _address_ of the string gets into the log file and not the string contents!!! What is going on??
Addendum: Seems ostream has no << _Elem* operator. When I changed to write(...) it works like a charm. The standard has changed I guess...
/R
|
|
|
|
|
Yes Robert. This was the most considerable change I was able to find out in VC.NET . You dont have streams now but still you can use STL's fstream and ostream etc.
Just do a plain include like
#include <fstream>
I hope this will solve your problem.
Stuck to Programming through an unbreakable bond
My Articles
|
|
|
|
|
Thanks Aamir,
I'm not sure what you mean by the "plain include"? I do use the include:
#include <fstream>
for the std::ifstream and std::ofstream STL classes.
I checked the VC7 STL implementation and a lot of stream operators are available for the std::ofstream (through std::ostream). All _except_ char* (!!). For example, when the ofstream was passed a char* it used the operator << (void * ptr), so it was no wonder that the pointer value got into the text file instead of the string content. I'm not convinced that this is according to the standard, as STLPort is much more standard compliant and with STLPort it works just as intended. I guess the VC6 STL bugs just have been replaced by VC7 STL bugs...
/Rob
|
|
|
|
|
I have used a bit mask before but how does one
extract individual bits. Say I wanted to extract
bit 28 from 32 bit int, how do I do that?
Thank You
Bo Hunter
|
|
|
|
|
Try this:
int nvalue = 0xabcd;
if ( nvalue & (1<<28) )
;
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
That is exactly what I needed.
Thank You
Bo Hunter
|
|
|
|
|
The other guys code works, and sometimes it is best. However consider the following
#define FOO_MASK 0x01000000 //0000 0001 0000 0000 0000 0000 0000 0000 0000, mask for foo
...
if(my_var && FOO_MASK) {
....
This has the advantage that when I read your code I know instantly you are looking for foo, while the other guys code just gives me (1 >> 28). It takes a moment to realize that he is looking at bit 29, and still gives me no indication of what bit 29 is!
Instead of the above you can also use bitfields:
#pragma pack(1)
struct myStruct {
long foo :28;
long bar :1;
long baz :3;
}
#pragma pack()
...
if(mystrct.bar) {
...
This is compiler and CPU specific, but it works fairly often in the real world, and is more readable.
If you must use the other guys code, please do it in a class, and only in one place. Then comment what is going on so when I come across it 10 years from now I can safely make a change.
|
|
|
|
|
Hello to All,
I need to create a bitmap object from the pixel color information in WindowsCE. I know its size ( width and length) and the RGB value of each pixel. Could any body tell me how to create a DIB with above info?
"CreateDIBitmap" function is not supported in WindowsCE.So how can i achieve this funtionality?Please help me.
Thanks in Advance,
Muthupandi.s
|
|
|
|
|
This seems like an easy question, but nevertheless, Im still
stuck.
How can I programmatically create a folder on the c:\ drive?
I just want to create an empty folder.
Please, any response any one can give me will be greatly
appreciated.
Sincerely,
Danielle Brina
|
|
|
|
|
CreateDirectory(...); // in Windows SDK
INTP
|
|
|
|
|
if ( CreateDirectory("c:\\MyDir", NULL))
{
AfxMessageBox("Danielle Brina's directory created ");
}else
{
AfxMessageBox("Danielle Brina's directory creation failed ");
}
Wandered in, liked what I saw, and am here to stay!
What's your excuse?
|
|
|
|
|
Hope this Will help
SHFileOperation
Copies, moves, renames, or deletes a file system object.
int SHFileOperation(<br />
LPSHFILEOPSTRUCT lpFileOp<br />
);
-----------------------------
"I Think It Will Help"
-----------------------------
Alok Gupta
visit me at http://www.thisisalok.tk
|
|
|
|