|
I have a text file which has data written to it.. If I open the file, how would I read the data in the file one line at a time, then do something with the data, then read the next line etc
Thanks for your help!
--PerspX
|
|
|
|
|
File.ReadAllLines will return the text as a string array. Assuming you meant to ask in the .NET C++ forum ( which you have )
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I'm getting really confused by trivial properties. Can someone explain this to me:
#include "stdafx.h"
using namespace System;
ref class Foo
{
public:
Foo()
{
data = 0;
s = "default";
}
Foo(int d, String^ in)
{
data = d;
s = in;
}
virtual String^ ToString() override
{
return "Data: " + this->Data + " S: " + this->S;
}
property int Data
{
int get()
{
return data;
}
void set(int d )
{
data = d;
}
}
property String^ S
{
String^ get()
{
return s;
}
void set(String^ in)
{
s = in;
}
}
int data;
String^ s;
};
int main(array<System::String ^> ^args)
{
Foo f;
Foo b(1, "hello");
Foo ^c = gcnew Foo(5, "world");
Console::WriteLine(f.data + " " + f.s->ToString());
Console::WriteLine(b.data + " " + b.s->ToString());
Console::WriteLine(c->data + " " + c->s->ToString());
Console::WriteLine(f.Data + " " + f.S->ToString());
Console::WriteLine(b.Data + " " + b.S->ToString());
Console::WriteLine(c->Data + " " + c->S->ToString());
Console::WriteLine(f.ToString());
Console::WriteLine(b.ToString());
Console::WriteLine(c->ToString());
return 0;
}
I'm so confused.
On a similar note, I have another code sample where I'm writing a very basic copy constructor for a ref class (with a single int data member) and if I use a trivial or explicit property the copy constructor works. If I remove the property declaration entirely and just access the data member (when its public), it works. But if I have the property declared (explicitly or trivially) then I can't set the data member in the copy constructor by accessing the member itself. I have to use the property name otherwise it sets it to 0.
|
|
|
|
|
Maybe I'm missing something, but how can you expect to read a property if you omit the getter
function?
Also, is using ToString on a String redundant?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
"Go that way, really fast. If something gets in your way, turn."
|
|
|
|
|
Unless I'm horribly mistaken... Declaring the trivial property as follows:
property int Data;
should tell the compiler to generate something to the effect of:
property int Data
{
int get()
{
return data;
}
void set(int d)
{
data = d;
}
}
using a ' < backing_store > Data' variable behind the scenes.
Likewise for
property String^ S That's the whole point of trivial properties as I understood it. If you're just making a 'trivial' getter and setter with no validation then the trivial property declaration should accomplish what you need and save you from typing a few lines of code. At least that's what I gathered from http://www.codeproject.com/managedcpp/CppCliProperties.asp?df=100&forumid=179807&exp=0&select=1107337 and many other articles/books.
And yes, that was a little redundant I've been staring at this property stuff for so long I'm making some dumb mistakes and triple checking everything before I post it.
-- modified at 18:18 Tuesday 3rd July, 2007
|
|
|
|
|
It's me that's horribly mistaken.
When you initialize the trivial proprties in your constructors, you need to use the trivial
property names:
Foo()
{
Data = 0;
S = "default";
}
Foo(int d, String^ in)
{
Data = d;
S = in;
}
...
property int Data;
property String^ S;
Mark
Mark Salsbery
Microsoft MVP - Visual C++
"Go that way, really fast. If something gets in your way, turn."
|
|
|
|
|
Right, I just didn't see why simply declaring the trivial property (instead of an explicit getter and setter) caused accessing the member variables directly not to work. Nor had I ever seen mention of this fact. If declaring a property makes it so you can't access the member properly without it that seems like something that should've been mentioned somewhere...
|
|
|
|
|
You can access them. Using your code only failed for me when using the trivial properties
because the constructors were setting the non-trivial property variable names (actually they
weren't even properties at that point - they're just member variables). That left the
trivial property "S" uninitialized, causing a NULL exception when accessed.
Once you get rid of these variables
// private:
int data;
String^ s;
and access these instead
property int Data;
property String^ S;
it works fine.
For me it was a good example of why having two variable names that differ only in case is
a recipe for disaster
Mark
Mark Salsbery
Microsoft MVP - Visual C++
"Go that way, really fast. If something gets in your way, turn."
|
|
|
|
|
Here's my other test case that is confusing the hell out of me. The errors/results/questions are all in the comments:
#include "stdafx.h"
using namespace System;
ref class Foo
{
public:
Foo() {}
Foo(int d)
{
Value = d;
}
Foo(Foo^ b)
{
value = b->value;
}
property int Value;
int value;
};
int main (array<System::String ^> ^)
{
Foo b;
b.Value = 5;
Foo^ c = gcnew Foo(%b);
Console::WriteLine("After creating c using the copy constructor passing b:");
Console::WriteLine("b.Value: " + b.Value + " c->Value: " + c->Value);
c->Value = 10;
Console::WriteLine("\nAfter updating c->Value via property:");
Console::WriteLine("b.Value: " + b.Value + " c->Value: " + c->Value);
Console::WriteLine();
Foo d;
d.value = 5;
Foo^ e = gcnew Foo(%d);
Console::WriteLine("\nAfter creating e using the copy constructor passing d:");
Console::WriteLine("d.Value: " + d.value + " e->Value: " + e->value);
e->value = 10;
Console::WriteLine("\nAfter updating e->Value via property:");
Console::WriteLine("d.Value: " + d.value + " e->Value: " + e->value);
}
|
|
|
|
|
"property int Value;" creates it's own member variable to store the value, it won't use "int value;".
So you got two members (one property "Value" and one field "value") that are unrelated and store different values.
|
|
|
|
|
So it seems. I'm just confused because all the postings I've seen about it have said you can write a trivial property declaration just like I did in place of the type of explicit one that I did (i.e., no mention of making sure to name the trivial property differently). It makes it incredibly confusing if your trivial property has the exact same name (case and all) as a data member... so confusing as to be useless in my opinion...
|
|
|
|
|
From my form I change(add or delete) data in datagridView source; How can do to see immediatly thoses modifications
|
|
|
|
|
Have you tried the Refresh method?
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Hi all,
I know how to use the dynamic_cast in Borland, but how would I do something like this in CLI VC++:
for (int k = 0 ; k < Form1->ComponentCount ; k++)
{
if(Form1->Components[k]->ClassNameIs("TImage"))
{
dynamic_cast <TImage*>(Form1->Components[k])->Picture->LoadFromFile("T1.bmp");
}
}
Many Thanks
Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
If TImage is a managed object:
dynamic_cast<TImage^>(Form1->Components[k])->Picture->LoadFromFile("T1.bmp");
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
Thanks for the help George I'll try it...
Reagrds,
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
Hi,
I need to get a flag, and protocole type in tram TCP, for that i would use WinPcap4.0 but unfortunatly it doesn't work ' i have installed Wpcap.exe ', in first i try to obtain a device list:
<br />
pcap_if_t *alldevs;<br />
pcap_if_t *d;<br />
int i=0;<br />
<br />
for(d= alldevs; d != NULL; d= d->next)<br />
{<br />
printf("%d. %s", ++i, d->name);<br />
if (d->description)<br />
printf(" (%s)\n", d->description);<br />
else<br />
printf(" (No description available)\n");<br />
}<br />
<br />
if (i == 0)<br />
{<br />
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");<br />
return;<br />
}
Please help me de resolve this great broblem,
Thank you verry mutch
|
|
|
|
|
i have a project of developing a linux chat server and i need help for it fast.contact me at arjunjag@yahoo.com so that i can send you all the details as soon as possible.
thanks
Arjun
|
|
|
|
|
You do realise that this isn't the way that these forums work don't you? People will get to you when they can.
Plus, this site is more for Visual Studio and .NET, not Linux.
Finally, don't put your email address in a request. It's not very nice, and it is a very good way to get spammed. You do realise that these forums are googled don't you?
|
|
|
|
|
Hi All
I am using the srand function generate random numbers.Here is the problem.
for example:
#include<iostream>
#include <time.h>
int main()
{
int i = 0,j = 0;
srand((int)time(0));
for(i=0; i<10; i++)
{
j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
std::cout << j << '\t';
}
std::cout << std::endl;
return 0;
}
result:
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7
The problem is that the two lines of the results are the same when the compiler run over in one second.
How the function works?
how the computer make the rand numbers?
Does the function can generate really Random Numbers ?
Please help me out if you find some solution for the above problem.
Thanks in advancs !
Where did the good times go?
Don't try it, just do it!
*Archibald*rever dragon!
|
|
|
|
|
Random numbers are very rarely random. They are defined using standard algorithms, and will return the same values based on the same seed value. Hence, they are known as pseudo-random numbers.
If you need the sequences to differ, then you need to use a different seed value.
|
|
|
|
|
thx a lot
now i see!
Where did the good times go?
Don't try it, just do it!
*Archibald*rever dragon!
|
|
|
|
|
Can someone clarify the differences between interior pointers and handles for me?
As I understand it, interior pointers do what handles can plus they allow for pointer semantics (arithmetic and comparison). Interior pointers also can point to unmanaged objects and native pointers are implicitly converted to interior pointers (but not vice versa). Both seem to be able to point to interior members of managed objects (such as an array of managed objects, or a managed class with a member variable of another managed type). Neither support double indirection (ie handle to a handle). So is there anything a handle can do that an interior pointer can't? In which case I assume handles are used purely for convenience as they're far easier to read/write/work with if you don't need the extra capabilities of an interior pointer. So basically use interior pointers over handles only when you need pointer semantics or some sort of interop capability?
|
|
|
|
|
|
Right, that's just making use of the pointer semantics of interior pointers.
My main point of confusion right now is that my initial understanding was that handles could only point to whole CLI objects (ie not interior data members no matter their type). In which case that would be an important place where interior pointers would need to be used. However, I can make a ref class Foo with a data member of a ref class Bar and a data member of a managed value class V (each of those classes as int data members) and use a handle to point to the Bar member, the V member, or even the int members of those 2 objects. So while using a handle to the int might incur a boxing cost, its still quite possible. So the only thing I see handles unable to point which interior pointers can are native objects. This seems to say that interior pointers are only useful for when pointer semantics are required or you need them for interop which I thought for sure was not the only time you needed them.
That's a little confusing considering:
"CLI object references always refer to a whole object of CLI type on the CLI heap (e.g., it cannot refer to a directly held member inside a CLI reference type object, or to an object on the C++ heap)."
from page 27 of the C++/CLI Design Rationale. http://www.gotw.ca/publications/C++CLIRationale.pdf
|
|
|
|