|
Why are you reading the file twice and throwing away the bytes read in the first pass? Here's a function to read all the bytes of a file in one pass to a C++ std::vector .
#include <fstream>
#include <vector>
#include <string>
using namespace std;
const int SizeBuff=1024;
typedef unsigned char BYTE;
void readFile(const string& inputFile, vector<BYTE>& buffer)
{
ifstream inputStream;
vector<BYTE> tmpBuf(SizeBuff);
streamsize bytesRead;
inputStream.open(inputFile.c_str(), ios::in|ios::binary);
while(inputStream) {
inputStream.read(reinterpret_cast<char*>(&tmpBuf[0]), SizeBuff);
bytesRead=inputStream.gcount();
if(bytesRead > 0) {
buffer.insert(buffer.end(), tmpBuf.begin(),
tmpBuf.begin() + bytesRead);
}
}
inputStream.close();
}
|
|
|
|
|
markkuk said...
Why are you reading the file twice and throwing away the bytes read in the first pass? Here's a function to read all the bytes of a file in one pass to a C++ std::vector.
That was when I was new to C#. I was making the first pass to get the actual number bytes in the file so I could use it to fill dynamic arrays with each character. I did not know at that time what method or call was used to get the size. Yes, I knew before I placed the code on this thread that it is a waste of time to read a file twice. I do know C# now, and I know console C++. Just a couple of days ago I have migrated to VC++MFC which I am still a little bit new at, your help is greatly appreciated.
Thanks markkuk,
Interesting function you have.
Vector is a STL container. You are passing both the "file_path_and_name" and the vector named buffer by reference so it is a void function. You also used a typedef.
So within the main program you could...
vector<BYTE> BufferNumberOne;
readFile("C:\files\picture1.jpg", BufferNumberOne);
for (int a = 0; a < BufferNumberOne.size(); a++)
{
BufferNumberOne[a];
//do stuff like fill array, modify chars, or etc.
}
or would you...
String some_file = "C:\files\picture1.jpg";
vector<BYTE> BufferNumberOne;
readFile(some_file, BufferNumberOne);
for (int a = 0; a < BufferNumberOne.size(); a++)
{
BufferNumberOne[a];
//do stuff like fill array, modify chars, or etc.
}
Please quote me if I am wrong on any of the above statements.
In past C++ courses we used statement like while(!EOF) but EOF is char 26, ^Z and it exists many times within jpg or other such file types that use any or all of the 256 different characters. Not many C++ text books discuss methods in any detail at all on how to read files of types other than txt.
Thanks Much markkuk,
demasoni.com
Elite
|
|
|
|
|
You can call the function both ways, just remember that the standard string type is string , not String . Some error checks should be added to the function before using it in a "real" program.
The constant EOF in C and C++ code isn't the ASCII EOF character, it's an integer value different from any character and can be used with any types of file.
|
|
|
|
|
Thanks for the info,
Your help will save me quite a bit of time. Enjoy the holiday.
jjs
Elite
|
|
|
|
|
Hi.
I want to add a licensing support to an existing project.
I did this:
Added this two lines to the control's immplementation file:
static const TCHAR BASED_CODE _szLicFileName[] =
_T("License.lic");
static const WCHAR BASED_CODE _szLicString[] =
L"Copyright (c) 1995 ";
then these functions:
BOOL CLicenseCtrl::CLicenseCtrlFactory::VerifyUserLicense()
{
return AfxVerifyLicFile(AfxGetInstanceHandle(),
_szLicFileName, _szLicString);
}
BOOL CLicenseCtrl::CLicenseCtrlFactory::GetLicenseKey(DWORD dwReserved,
BSTR FAR* pbstrKey)
{
if (pbstrKey == NULL)
return FALSE;
*pbstrKey = SysAllocString(_szLicString);
return (*pbstrKey != NULL);
}
but it does not work. I figured out that the license keyword needs to be added to the .odl file, and also a license key created.
Anybody that can direct me to a good site explaining this? Thanks.
.
|
|
|
|
|
How can you stop the Tab key acting as an Enter key during text entry into a Edit Box.
If I enter text into the Edit Box and press the Tab key, then the program acts as if the Enter key was actioned.
grahamfff
|
|
|
|
|
I think the tab key was overrided for switching between controls in CWnd.
You could use CTRL D to see the sequence of tab sequence.
My opinion will be override the OnKeyUp of your CWnd for VK_TAB.
But probably there are simpler way to do.
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|
|
See Michael Dunn's C++ FAQ on this site. It outlines a good method.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Could not see it on the FAQ list to quoted, could you specify a link please.
This apprears adifficult problem to solve!
|
|
|
|
|
|
Spoon....
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|
|
Huh?
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
I am trying to create a program using the CreateProcessWithLogonW. I am working on a Windows 2000 Professional. The program compiles fine but when it tries to link it, it gives the following errors:
Linking...
CreateProcess.obj : error LNK2001: unresolved external symbol __imp__CreateProcessWithLogonW@44
Debug/CreateProcess.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Creating browse info file...
CreateProcess.exe - 2 error(s), 0 warning(s)
The code that I am trying to compile is as follows:
#define UNICODE
#define _WIN32_WINNT 0x0500
#define WINVER 0x0500
#include <windows.h>
#pragma comment(lib, "Advapi32")
int main(int argc, char* argv[])
{
STARTUPINFO si;
si.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION pi;
if (CreateProcessWithLogonW(L"UserName", L"Domain", L"password", LOGON_WITH_PROFILE, NULL, L"\"full file path\"", CREATE_DEFAULT_ERROR_MODE, NULL, L"\"current directory\"", (LPSTARTUPINFO)&si, (LPPROCESS_INFORMATION)&pi)) {
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
return 0;
}
|
|
|
|
|
smbecker wrote:
#pragma comment(lib, "Advapi32")
change to
#pragma comment(lib, "Advapi32.lib")
Rob Manderson
I'm working on a version for Visual Lisp++
|
|
|
|
|
I made the change and I am still getting the same linking error as before. Any other suggestions? Thanks
|
|
|
|
|
Hey all,
I got 2 questions.
1st one is how do you change the size of a CString? I have tired and I couldn't get it, here is what I have tried.
<br />
CFont font;<br />
font.CreatePointFont(480, "Arial", &dc);<br />
<br />
<br />
CString str("DEP");<br />
<br />
CSize size = dc.GetTextExtent(str);<br />
<br />
dc.DrawText(str, printArea, DT_NOPREFIX | DT_WORDBREAK);<br />
<br />
2nd question is how do you get a Veritcal Scroll Bar to work? I know how to add it to a Dialog, but can't get it to scroll.
Thanks for the help
|
|
|
|
|
Before a call to the device context functions, you need to select the graphics object(s) you need to work with; in your case, a CFont object. Write the following statements before and after a call to the DrawText(...) function respectively:
Before
CFont *pOldFont = dc.SelectObject(&font);
After
dc.SelectObject(pOldFont);
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
I am trying to access an Excel spreadheet in Visual C++ 6.0. I am using the XL5EN32.OLB library. However I keep getting errors, one of which is:
error C2440: 'type cast' : cannot convert from 'class Sheets' to 'class COleVariant'
I just wanted to be able to pull data out of a spreadsheet and use it in my program. There are problems with Ole. I thought you could define a sheet from the workbook object. However it does not allow me to do this. You have do get sheets from a workbook and a workbook from a file. But I can not do this, the return types don't match. I looked at MSDN article 179706
http://support.microsoft.com/default.aspx?scid=kb;EN-US;179706
It doesn't seem to work that way though. I don't know how they got the return types to match.
COleVariant fn;
CString f;
Application app;
Workbooks books;
Workbook book;
Range pRange;
Sheets sheets;
double Result;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
app.CreateDispatch("Excel.Application");
app.SetVisible(covTrue);
books.Open(fn, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional);
sheets = book.Sheets(COleVariant((short)(1)));
|
|
|
|
|
There are some internal logic that you need to handle.
The way to correct this is.
LPDISPATCH lpDisp;
lpDisp = book.Sheets(COleVariant((short)(1)));
ASSERT(lpDisp);
sheets.AttachDispatch(lpDisp);
I have not tested it but it should be that kind of style down the road using AttachDispatch(...)
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|
|
I get the following error when I try
LPDISPATCH lpDisp; lpDisp = book.Sheets(COleVariant((short)(1))); ASSERT(lpDisp); //some checking sheets.AttachDispatch(lpDisp);
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct tagVARIANT' (or there is no acceptable conversion)
|
|
|
|
|
Try
LPVARIANT lpVar = (book.Sheets(COleVariant((short)(1)))).operator LPVARIANT( );
LPDISPATCH lpDispatch;
if(lpVar->vt == VT_DISPATCH)
{
lpDispatch = lpVar->ppdispVal;
}
else
{
ASSERT(NULL);
}
sheets.AttachDispatch(lpDisp);
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|
|
I have a modal dialog box that I would like to go modaless if a button is clicked on and then back to a modal state after the buttons action has ended.
I have tried EndModalDialog but with no success, does anyone know a command that can help me?
|
|
|
|
|
Just call yourDialog.DoModal() for modal dialog.
and yourDialog.Show() for modeless.
There are plenty of tutorials on modeless dialog in codeproject.
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|
|
I want the Dialog Box to be modal unless the button is clicked.
If they never click the button then the dialog box should just be
modal the entire time.
What I am trying to do is: if a user clicks on a button then it opens a doc/view window where they can make a choice that sends them back to the dialog box which would return to its modal state.
Thanks
|
|
|
|
|
I think Gary R. Wheeler suggestion is implementable.
Like
((YourParentDlg*)this->GetParent())->EnableWindow(FALSE);
Anyway, I think the logic behind is and should lie under you.
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|