|
How about:
extern tistream& tcin;
#endif
#ifdef _UNICODE
tistream& tcin = wcin;
#else
tistream& tcin = cin;
#endif That should be preferable to defines, as it will preserve the variable and typename, which may be helpful when debugging.
If you're not compiler sensitive (portability etc), you can always try
#ifdef _UNICODE
__declspec(selectany) tistream& tcin = wcin;
#else
__declspec(selectany) tistream& tcin = cin;
#endif __declspec(selectany) eliminates the need for a .cpp file, as it instructs the compiler to allocate space for the reference in one and only one compilation unit. Nice if you want to eliminate the need for lib files.
|
|
|
|
|
I have never tried the "__declspec(selectany)" solution. Thanks for the insight.
|
|
|
|
|
Just put something like this in you precompiled header ("StdAfx.h"):
#include <string>
#ifdef _UNICODE
typedef std::wstring tstring;
#else
typedef std::string tstring;
#endif // _UNICODE
Then use tstring like TCHAR and such.
Steve
|
|
|
|
|
I have used the basic_string<TCHAR> typefef myself in the past, but nowadays I _always_ compile for Unicode, and use std::wstring everywhere. I then link with the UnicoWS library so my apps work on Win9x. It hasn't given me any problems.
|
|
|
|
|
I am Using CArray as Two Dimensional Array Containing float type vlaues.Say for Example the inital size of the Array is [4][4] now i want to increase the size to [5][4]..And i want insert Row in between 2nd and 3rd rwos. Can CArray insert new row between 2nd and 3rd rows and will it adjust rest the data in the array to new size..?
Thanks Before
James
-- modified at 23:33 Saturday 11th February, 2006
|
|
|
|
|
Post a little code please.
How is the 2D array declared?
Is it realy setup as 2D array? Then yes.
Are you faking a 2D array? Then no, because you need to do the calculations?
Modifed:
Just read the previous post.
Hints:
Wheeler gave you a 2D array.
McFarlane gave you a fake 2D array.
Which are you using?
INTP
Every thing is relative...
-- modified at 11:24 Sunday 12th February, 2006
|
|
|
|
|
Hello everyone!
Alright, I have this struct:
struct Person<br />
{<br />
char FullName[50];<br />
int Age;<br />
};
I open a binary file with ios::out | ios::binary | ios::app , so it appends to the file, so there can be many of these structs in a file... The write function is fine, because it outputs alright if I use a single-reading function... Here's how I read it with the multiple-reading function:
int entries = getNumOfEntries(&dat_file);<br />
<br />
Person persons[entries];<br />
<br />
for (int x = 0; x < entries; x++)<br />
{<br />
dat_file.read((char*) &(persons[x]), sizeof(Person));<br />
}
It outputs some weird stuff... Anyone realize what I'm doing wrong? Thanks!
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
|
|
|
|
|
An example of weird stuff would help.
You could try changing the conversion in the read statement to (unsigned char*).
Single stepping through the read loop should help.
The line "Person persons[entries];" is illegal and should not compile. I think you mean "Person* persons new Person[entries];".
Note: This does not affect you code, as written (povided you write records the same way), but unless you set packing for the structure, then the compiler will allign it to the next 4-byte boundry. So your structure size is realy 56 bytes not 54.
I assume that you have reopened the file with ios::in.
INTP
Every thing is relative...
|
|
|
|
|
Hmm... Well, I have changed things a little bit... I now write the char and the int alone, with no Person in between, but it won't work!
<br />
dat_file.write(person.FullName, sizeof(char)*NAME_SIZE);<br />
dat_file.write((char*) &person.Age, sizeof(int));<br />
<br />
dat_file.read(person.FullName, sizeof(char)*NAME_SIZE);<br />
dat_file.read((char*) &person.Age, sizeof(int));<br />
The file is now exactly 54 bytes...
Hmm... I tried cout ing dat_file.tellg() a few times between the read functions, and it said -1 on all of them... Maybe that's the problem? Thanks!
P.S: The file only contains 1 char and 1 int , no more.
P.S: I forgot to tell you that I get the size of the file with this:
long getFileSize(fstream * tempFile)<br />
{<br />
long begin, end;<br />
begin = tempFile->tellg();<br />
tempFile->seekg (0, ios::end);<br />
end = tempFile->tellg();<br />
return end-begin;<br />
}
It's repositioning the get pointer! I tried setting it to 0 after everything, but nope, didn't work... Still get the weird stuff:
Name: Vσ┬wP▼=
Age: 2009291924
P.S: Sorry for modifying that much, but I discovered something... Even if I change the name from "Leo" to "loahxoatlksrtjazme3h10ortlsahrzlirdatrengjklsafui5", I still get the exact same output!
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
-- modified at 13:06 Sunday 12th February, 2006
|
|
|
|
|
Your writing and reading where ok, as long as you read and wrote the same way. It would just mean that if some one else was going to read or write to the file, you would have to have documented the actual size of the record.
Whoops! getFileSize()
After calling end = tempFile->tellg(); , you need to call tempFile->seekg(0, ios::beg); . If you do not, then you will be trying to read from the end of the file. That is tellg will return -1.
You have the right idea for a general purpose function for getting the size, but I would probably do something like this:
long getFileSize(fstream * tempFile)
{
long begin, end;
begin = tempFile->tellg();
tempFile->seekg (0, ios::end);
end = tempFile->tellg();
tempFile->seekg (begin);
return end;
}
INTP
Every thing is relative...
|
|
|
|
|
|
Hi,
As in C++ , double matrix[10][10]; two dimensional array...How can we use CArray as a two dimensional..? IF we can how Can we add eleents in to this two array?.
Thanks before..
|
|
|
|
|
typedef CArray <int, int> CIntArray;
typedef CArray <CIntArray, CIntArray&> CMultiIntArray;
void TraceDynamicArray(const unsigned int rows, const unsigned int columns)
{
CMultiIntArray aTest;
aTest.SetSize( rows );
for (int row = 0; row < aTest.GetSize(); row++)
{
aTest[row].SetSize( columns );
for (int column = 0; column < aTest[row].GetSize(); column++)
{
aTest [row] [column] = 10 * row + column;
afxDump << aTest [row] [column] << "\t";
}
afxDump << "\n";
}
}
Kevin
|
|
|
|
|
I am new to MFC...As we do in C++
double matrix[10][5];
double somedoubleValue=10.005;
for(int i=0;i<10;i++)
{
for(int j=0;i<5;j++)
{
matrix[i][j]=i*j*somedoubleValue;
}
In the same manner instead i want to use CArray..bec..the Array matrix size can increase in future and i want insert in between..so i want to CArray as two dimesional array......Can we more Clear..I have read above posted message..but i don`t how to use it....so pls can be more clear.
Thank q
James
|
|
|
|
|
Try this:
CArray<CArray<int>> array;
array.SetSize(number_of_rows);
for (int row = 0; row < number_of_rows; row++) {
array[row].SetSize(number_of_columns);
}
for (int row = 0; row < array.GetSize(); row++) {
for (int col = 0; col < array[row].GetSize(); col++) {
array[row][col] = 0;
}
} This creates a two-dimensional array that is dimensioned [number_of_rows,number_of_columns] . Note that each row can be sized differently if necessary.
Software Zen: delete this;
|
|
|
|
|
I'm trying to create an AddIn for VC++ 7.1, and I'm having problems inserting a submenu into the Tools menu.
I'm also not sure how to undo the installation, so that when you look at AddIn manager, the AddIn is no longer listed.
How do I add a submenu, and how do I remove the AddIn to VC++ 7.1?
I have the following source code:
http://code.axter.com/Connect.cpp
And the current code is in the CConnect::OnConnection method.
I'm only interested in C++ methods, and not any VB code.
Thank you
Top ten member of C++ Expert Exchange.
http://www.experts-exchange.com/Cplusplus
|
|
|
|
|
Hello everyone!
I'm doing a program to learn how to use binary files...
I have a struct that has one member of type std::string and one int. I want to write that struct to a binary file, but just the int gets written!
fstream dat_file(file.c_str(), ios::out | ios::binary);<br />
<br />
Person person;<br />
<br />
person.FullName = fullName;
person.Age = atoi(strAge.c_str());<br />
<br />
dat_file.write((char*) &person, sizeof(Person));<br />
<br />
dat_file.close();
Why won't it write the name to the file? Thanks!
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
|
|
|
|
|
|
|
|
|
|
Morning All !
Well here is a problem. I am developing an application that needs to connect to the internet to perform an FTP transfer. Detecting the presence of a valid connection seems to be **** HARD !
Having read what’s seems to be available on developer sites, I feel the RasEnumConnections & RasGetConnectStatus have short comings and are too unreliable. (Worth pointing out that I have no experience of socket level programming)
A solution posted by Patrick Philippot seems to be what I need except the <iphlpapi.h> and lib are required and I spent many hours failing to install the SDK from Microsoft
Does anyone know of a way of detecting a valid Internet connection that wont invoke a 'dial up connection box' .
Many thanks boys and girls
Regards Philip....
|
|
|
|
|
Healdp wrote: Detecting the presence of a valid connection seems to be **** HARD !
Yup
Check out this article[^] for some ideas on how to tackle this problem.
The two most common elements in the universe are Hydrogen and stupidity. - Harlan Ellison
Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.
|
|
|
|
|
Checking the routing table for a default route is the best technique I've come across so far as it does not require checking a URL, pinging an IP address, or other techniques that will not be entirely accurate in all contexts. It also does not invoke any RAS connection dialog boxes which makes it sound like what your looking for.
(You mentioned you cannot get the SDK installed. If you are using VC++ 6.0, you will need the February 2003 SDK or prior installed to use this or to at least get the header and .lib files. Any SDK after this is incompatible with VC++ 6.0 at least without jumping through numerous hoops to get it to work. If you are using VC++ 7.1, the SDK is available on your installation disks)
I got the idea from someone on CodeGuru before they upgraded their website and lost all the legacy comments. Unfortunately, I do not know the name of the person who passed on the idea so I can't give credit where credit is do but I'll pass it on...
The function to use is GetIpForwardTable(). I use a timer to periodically check for connectivity and update my status indicator accordingly. There is a slight delay maybe a second or two if you unplug the cable or plug it in (assuming your timer is set to fire every second). No noticeable delay when disabling the connection using a GUI tool.
Don't forget to include in StdAfx.h...
#include <iphlpapi.h> // For Routing table query
And make sure to reference the Platform SDK lib file...
iphlpapi.lib
BOOL CNetworkConnectionDlg::IsInternetAvailable(void)
{
MIB_IPFORWARDTABLE * pRoutingTable;
DWORD dwBufferSize=0;
BOOL bIsInternetAvailable=FALSE;
DWORD dwResult;
DWORD dwIndex;
DWORD dwRowCount;
// Get the required buffer size
GetIpForwardTable(NULL,&dwBufferSize,FALSE);
pRoutingTable=(MIB_IPFORWARDTABLE*)new BYTE[dwBufferSize];
// Attempt to fill buffer with routing table information
dwResult=GetIpForwardTable(pRoutingTable,&dwBufferSize,FALSE);
if (dwResult==NO_ERROR) {
dwRowCount=pRoutingTable->dwNumEntries; // Get row count
// Look for default route to gateway
for (dwIndex=0;dwIndex<dwRowCount;dwIndex++) {
if (pRoutingTable->table[dwIndex].dwForwardDest==0) { // Default route designated by 0.0.0.0 in table
bIsInternetAvailable=TRUE; // Found it
break; // Short circuit loop
}
}
}
delete pRoutingTable; // Clean up. Just say "No" to memory leaks
return bIsInternetAvailable;
}
|
|
|
|