|
unsigend long x = 100;
unsigned int y = (unsigned int)x;
this will do. But why do you want to convert long to int. long is 64bits and int is 32 so it will get truncated.
|
|
|
|
|
Acutaly it depends on the compiler, in VC6 they are both 32bits. An int is supposed to be the size of a register word, but in reality it is up to the creators of the compiler.
INTP
Every thing is relative...
|
|
|
|
|
yea long is 32-bit in vc. I must have been sleeping when replying
|
|
|
|
|
I'd do it like this:
static_cast<uint>(your_ulong)
C style casts (a type in brackets) are a major haste and should be avoided - This is the reason C++ added the const_cast , dynamic_cast , static_cast and reinterpret_cast keywords.
I'm sure there will be people who disagree but I would wager they haven't had to spend weeks hunting down some obscure bug caused by a bad cast which is almost invisible in the source code.
Steve
|
|
|
|
|
That depends on the type of cast. Doing such things as uint casts, no I have never had problems with them.
However, when converting pointers, yeah, static_cast will save you MUCH pain.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
I agree that with numeric types there is no difference - however it's a policy thing. If all the casts are one of the function style casts you can "grep" for every cast in the project. Casts generally represent a design problem (but not in all cases) and as such should be highly visible.
Steve
|
|
|
|
|
I have never worked for a big company, but after years of reading (and fixing) other peaples code I understand why a policy is needed. I am not talking about visability (good point though), I am talking about simple things like comparing floating point numbers.
I recieved a varification a few years ago (that I was not a geek) when a guy (which I was warned about) told a joke about comparing floating point values and I said that I had no problem doing so (he stopped laughing). It took me a couple of minutes to realise what he was talking about, I did not find it funny becuase I take such things for granted.
INTP
Every thing is relative...
|
|
|
|
|
Hi!
I have a question regarding file input and output.
If I use a getchar or a getline, how can I tell
my program to go get a certain character or line
like for instance the character or line
after the phrase "Get this line:" or line # 43?
How can I do such things and which other things
are possible?
I haven't found anything usefull on the net so you
guys are my last resort.
Thanks!
Peter
|
|
|
|
|
If you know all the lines in the file are the same length then it is simply a matter of using one of the seek functions (fseek, lseek, basic_filebuf::seekpos, etc) to set the file pointer to the proper position. If that is not possible then you have to start at the beginning of the file and start reading one line at a time until you get to the desired line.
You may be right
I may be crazy
-- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
|
Yes, that might work.
Can you give me an example of the latter?
I am quite new to file input and output and I have never written any code that seeks
a certain line or a certain character.
Thanks!
|
|
|
|
|
You already mentioned the method "getline", it reads one whole line at a time. You just need to count the number of lines you read.
The other part of finding a substring (part of the line), you just do on a line by line bases. Unless there is a newline in the substring, in which case you have to search the whole file piece by piece or a character at at time, your choice.
You could just load the whole file (if not to larg) into an STL string type and call find to search for a substring.
The is a ridiculously large number of ways to do what you want, just look at what your library offers (string and file i/o), and your imagination.
You can even use regular expressions.
INTP
Every thing is relative...
|
|
|
|
|
I once knew a Peter Charlesworth who worked for a UK company called Kerridge back in the late '80s - not you by any chance?
The Rob Blog Google Talk: robert.caldecott
|
|
|
|
|
I'm afraid I'm not.
I wasn't born yet in the '80s, although I have been to the UK once.
Sorry to disappoint you, but that name is a pseudonym I came up with
while browsing through my font-list (Charlesworth is a font's name)
and I liked that name so I went for it. It sounds rather confident
and worthy, don't you agree?
Well I hope for you you'll see your old friend back one of these days!
Regards,
Me
|
|
|
|
|
hello.
i've used this code to open a program:
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi = {0};
BOOL bSuccess;
bSuccess = CreateProcess ( NULL, "\"C:\\Program Files\\dir\\program.exe\"",
NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
NULL, NULL, &si, &pi );
now i want to be able to minimize and restore the program. what code can i use to do this?
thanks in advance,
sam kline
-- modified at 12:59 Saturday 4th March, 2006
|
|
|
|
|
The PROCESS_INFORMATION structure will contain the thread handle of the newly opened program. You can use PostThreadMessage to post any windows messages to that thread.
You may be right
I may be crazy
-- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
|
Hi, I've tried to build a VC++ program in VS2005. I built it in release mode and although it runs correctly in my machine, when I try to run it in a machine which does not have VS2005 the application instead of running shows an error message box saying:
"The application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem."
My application is composed of an exe file and several dlls which I copy to the target machine. Do I have to copy anything else to the target machine?
Thank you.
|
|
|
|
|
When you created the program, did you select in application wizard, "Use in a shared dll", or "Use in a static library"? Windows shares many .dll files and if you chose a static library, Windows may not be able to access the program you are trying to deploy because the program is telling Windows that it has it's own dll's. Are you trying to deploy to computers with different operating systems, or are they the same? If they are different, you may need to find out what .dll's are needed for the program to work. I know that is not much help, but it's all I can think of.
Richard
|
|
|
|
|
If I chose static linking was Windows supposed to embed the library code I use in my binaries so that I don't need dlls at all? Anyway I'm using shared dll. The operating system in both machines is WinXP SP2.
Any idea what are these manifest files I got produced with each one of my dlls? Do I have to distribute them along with my app?
Themis
|
|
|
|
|
Themis wrote: Any idea what are these manifest files I got produced with each one of my dlls?
Please note: this is a rough explanation, based on my limited understanding.
Manifest files are how applications and their DLL's can specify their dependencies on each other. You probably need to include them in your installation. You can include the manifest in the resources for your DLL/EXE, which then avoids needing to worry about copying the manifest file.
Software Zen: delete this;
|
|
|
|
|
|
I am trying to fully justify text in CRichEditView. (MFC)When I compile and test the program, the text selected seems to default to left justification not full interword. I have RichEdit 4.1 installed, running windows xp pro, with all critical updates installed. If PFA_LEFT, PFA_RIGHT, AND PFA_CENTER work with pf.wAlignment, why won't PFA_FULL_INTERWORD? This is supported in RichEdit 4.1, and even in 3.0, but for some reason when the function is called below, Left justification is the only response. Maybe I shoud contact Microsoft? Any suggestions? Thank you so much if you can help.
GetParaFormatSelection();
PARAFORMAT2 pf;
pf.dwMask = PFM_ALIGNMENT;
pf.wAlignment = PFA_FULL_INTERWORD;
VERIFY(SetParaFormat(pf));
Richard
|
|
|
|
|
Hi,
I'm wondering if I can use the ANSI String Class in normal classes.
I tried yesterday and got an error.
Product.h
#include "string.h"
#include "iostream.h"
using namespace std;
class Product
{
public:
Product(Product&);
Product(string name, int amount , float price);
float GetPrice();
void SetPrice(float price);
void SetAmount(int Amount);
int GetAmount();
void SetName(string name);
string GetName();
Product();
virtual ~Product();
private:
float itsPrice;
int itsAmount;
string itsName;
};
Product.cpp
Product::Product()
{
}
Product::~Product()
{
}
string Product::GetName()
{
return itsName;
}
void Product::SetName(string name)
{
itsName = name;
}
int Product::GetAmount()
{
return itsAmount;
}
void Product::SetAmount(int Amount)
{
itsAmount = Amount;
}
void Product::SetPrice(float price)
{
itsPrice = price;
}
float Product::GetPrice()
{
return itsPrice;
}
Product::Product(string name, int amount, float price)
{
itsName = name;
itsAmount = amount;
itsPrice = price;
}
Product::Product(Product &rhs)
{
itsName = rhs.GetName();
itsPrice = rhs.GetPrice();
itsAmount = rhs.GetAmount();
}
main.cpp
#include "iostream.h"
#include "Product.h"
using namespace std;
int main()
{
Product p1("Apple",0.25,50);
cout << p1.GetName();
int s;
std::cin >> s;
return 0;
}
The error is : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion)
Please help.
Thanks in advance
Tom
|
|
|
|
|
Instead of
#include "string.h"
#include "iostream.h"
use
#include <string>
#include <iostream>
<hr>
<a href = http://www.codeproject.com/script/profile/whos_who.asp?id=14112#Blog> My programming blahblahblah blog</a>. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
I did! I typed in the '"' in the post because it wouldnt show in the preformatted way
Tom
|
|
|
|
|
I mean skip the ".h" part.
#include<string>
NOT
#include<string.h>
|
|
|
|