|
samkook wrote: What should i do to put it in a byte directly?
BYTE is a typedef for unsigned char, so, makes no difference.
samkook wrote: Also, I used CFile only because i never managed to copy japanese correctly with fstreams.
OK, well, sounds like you weren't using fstream properly either. A stream should not care if the bytes are japanese text, or a picture of the pope.
I think you need to post code, and wait for someone who uses CFile to step in.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
It would look something like this. It's only a test i made to read the first 10 bytes and them write it.
This code in particular doesn't work, because the second Cfile doesn't want to open and i really don't know why, but it's close enough to what i would do to write it.
private: System::Void mnuSave2_Click(System::Object * sender, System::EventArgs * e)
{
CFile fVampFile;
CFile fVampFileSav;
CString csFilename;
CString csFilenameSav;
unsigned char cByteBuff[1];
int iByteMove = 1;
unsigned char cByteArray[10];
if(odOuvrir->ShowDialog() == DialogResult::OK)
{
csFilename = odOuvrir->FileName;
if( !fVampFile.Open(csFilename, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary) )
{
MessageBox::Show(S"File probably in use");
}
try
{
fVampFile.SeekToBegin();
for (int iCtr = 0; iCtr < 10; iCtr++)
{
fVampFile.Read(cByteBuff,iByteMove);
cByteArray[iCtr] = *cByteBuff;
}
fVampFile.Close();
if(odSave->ShowDialog() == DialogResult::OK)
{
csFilenameSav = odSave->FileName;
}
if(fVampFileSav.Open(csFilenameSav,CFile::modeWrite | CFile::shareDenyNone | CFile::typeBinary ) )
{
fVampFileSav.Write(cByteArray,10);
}
}catch(...)
{
MessageBox::Show(S"Erreur lors de la lecture du fichier");
}__finally
{
fVampFileSav.Close();
eTexte->Text = "Done!";
}
}
}
};
|
|
|
|
|
You're using managed C++ AND MFC together ?
So long as you're using managed code, have you tried using a filestream ? If this is .NET 2.0, System.IO.File has static methods that will read and write an entire file in one line of code.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Yeah, i'm using both because i wanted to learn .net, but i'm too used to the good old ways.
And i also tried using a filestream(it was the first thing i tried), but i ended with the same result as the fstream.
|
|
|
|
|
samkook wrote: Yeah, i'm using both because i wanted to learn .net, but i'm too used to the good old ways.
IMO the end result is a disaster. Don't worry about the 'new ways', I don't think there's a real future for .NET in C++, certainly not mixed with MFC.
samkook wrote: And i also tried using a filestream(it was the first thing i tried), but i ended with the same result as the fstream.
if(fVampFileSav.Open(csFilenameSav, CFile::modeCreate|CFile::modeWrite | CFile::shareDenyNone | CFile::typeBinary ) )
I had a hard time making your code compile ( it relies on objects that were not there ) but adding CFile::modeCreate caused it to work for me. Otherwise, the file open for write failed altogether.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
samkook wrote: if( !fVampFile.Open(csFilename, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary) )
Your code is not wrong, but CFile objects open in binary mode by default.
samkook wrote: fVampFile.SeekToBegin();
This is redundant as the file pointer is already positioned at the beginning.
samkook wrote: for (int iCtr = 0; iCtr < 10; iCtr++)
{
fVampFile.Read(cByteBuff,iByteMove);
cByteArray[iCtr] = *cByteBuff;
}
Reading one byte at a time, disk I/O is going to be your enemy here. Try:
int nLength = fVampFile.GetLength();
LPBYTE pBuffer = new BYTE[nLength];
fVampFile.Read(pBuffer, nLength);
fVampFile.Close();
fVampFileSav.Write(pBuffer, nLength);
fVampFileSav.Close();
delete [] pBuffer; Once you get this working correctly, then you can add the code that inserts the new text;
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
hmmm, looks like it's working. I don't know what I was doing wrong before.
Thanks for your help guys.
-- modified at 12:52 Friday 13th October, 2006
Oh and btw, I don't have a choice(maybe i do, but it's easyer that way i think) to read it byte by byte because I need to identify some tags in the file.
-- modified at 12:54 Friday 13th October, 2006
|
|
|
|
|
samkook wrote: I don't have a choice(maybe i do, but it's easyer that way i think) to read it byte by byte because I need to identify some tags in the file.
This makes no sense. Using the code snippet I showed, you can look at each byte of lpBuffer . RAM is a whole lot faster than the disk.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Now that i think of it, your right. I shoudn't reply to message that need thinking when I wake up, it doesn't get good results.
|
|
|
|
|
|
|
Yeah, that'll work! Thanks!
Windows Calculator told me I will die at 28.
|
|
|
|
|
I have 2 dialog,
first dialog I have a checkbox. If you click on that dialog box a new dialog is show and I have a button there and when that button is pressed on I need to end the dialog and the checkbox on the first dialog should not be checked.
So how can I do that? Sorry for the bad english.
|
|
|
|
|
Which part do you have so far and which part are you having trouble with?
Mark
|
|
|
|
|
Larsson wrote: ...when that button is pressed on I need to end the dialog...
Just call EndDialog() like would happen if you clicked the OK button.
Larsson wrote: ...and the checkbox on the first dialog should not be checked.
Just uncheck it with SetCheck() .
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
|
what problems with david crow answer
|
|
|
|
|
ok. I think that you are ok with the first part.
in the second part, you have two approaches:
in the 2nd dlg button write code that looks like this:
void CDialog2::OnButton()
{
(CDialog1 pDlg*)=GetParent();
pDlg->m_ctlCheck1.SetCheck(FALSE);
CDialog::OnOK();
}
or after exiting the 2nd dialog set a flag and check it in the first diaog
regards,
Mohammad Gdeisat
|
|
|
|
|
|
Well this is what I get on (CDialog1 pDlg*)=GetParent();
error C2146: syntax error : missing ')' before identifier 'pDlg'
|
|
|
|
|
Larsson wrote: Well this is what I get on (CDialog1 pDlg*)=GetParent();
Come on ! what you are trying to do ?
I think, CDialog1* pDlg=(CDialog1*)GetParent();
|
|
|
|
|
amjadamq wrote: (CDialog1 pDlg*)=GetParent();
pDlg->m_ctlCheck1.SetCheck(FALSE);
Which totally ties the two dialogs together. Not a good design. The preferred solution would be for the second dialog to simply post a message to the firt dialog.
In addition, SetCheck() does not take a BOOL argument. It's only by coincidence that FALSE and "unchecked" resolve to 0.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Could someone please tell me where I've gone wrong with the following class? The following code compiles and runs with no errors (VC++.Net 1.1) but the data is not written to the file (or at least not properly). Ideally I would like a class that opens up a stream for intermittent writing from a main program via multiple member functions and then closes the stream when the class destructor is called. I figure that that should be a trivial thing to do.
Thank you in advance for any help and I apologize if this turns out to be a stupid mistake on my part.
Nick
<pre>
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
class plot_interface{
private:
ofstream outFile;
public:
bool data_write(char *FileName);
plot_interface(){PlotType = 0;};
int PlotType;
};
bool plot_interface::data_write(char *FileName){
//ofstream outFile; // if this is uncommented the program works corectly
// but it does not achive what I want it to.
outFile.open(FileName,ios::out | ios::binary);
outFile.write((char *) &PlotType, sizeof(int));
return true;
}
void _tmain(int argc, _TCHAR* argv[]){
plot_interface plot;
plot.PlotType = 32;
if(!plot.data_write("loser.dat")){cout<<"could not open the file"; return;}
int temp_type;
ifstream inFile;
inFile.open("loser.dat");
inFile.read((char *) &temp_type, sizeof(int));
cout<<"we read: "<<temp_type<<" back from the file\n\n";
cin.get();
return;
}
</pre>
|
|
|
|
|
Nick Usechak wrote: outFile.open(FileName,ios::out | ios::binary);
This will try to reopen the file every time, if it's a member variable, but you never close it. You should open/close, or open in the constructor/close in the destructor.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Sorry, I should have pointed out that I also tried to initialize the file in the constructur and I still have the same problem. (It compiles with no errors but when I read the value back from the file it is just garbage.)
|
|
|
|