|
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.)
|
|
|
|
|
Nick Usechak wrote: 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;
}
This function will open the file, write to it, and leave it open, but the next time you call it, it will try to do the same thing. What you will seem to want is something more along these lines:
#include <iostream>
#include <fstream>
class plot_interface
{
public:
plot_interface() { PlotType = 0; }
~plot_interface() { if (outFile.is_open()) outFile.close(); }
bool open_file(char* FileName);
bool data_write();
int get_plot() { return PlotType; }
void set_plot(int pt) { PlotType = pt; }
private:
std::ofstream outFile;
int PlotType;
};
bool plot_interface::open_file(char* FileName)
{
if (!outFile.is_open())
{
outFile.open(FileName, ios::out | ios::binary);
}
return false;
}
bool plot_interface::data_write()
{
if (outFile.is_open())
{
outFile.write((char *) &PlotType, sizeof(int));
return true;
}
return false;
}
void _tmain(int argc, _TCHAR* argv[])
{
plot_interface plot;
plot.set_plot(32);
if (false != plot.open_file("loser.dat"))
{
if(!plot.data_write())
{
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;
}
Just a couple notes though:
1) Why are you bothering to make a class for this? Why not just use the ofstream directly instead of throwing it in a class when it really isn't needed?
2) You can actually write it better by using basic_ofstream<int> instead of ofstream (at least more readable).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
I tried the code that you posted and still have the same error. Is this possibly a compiler thing? The code still does not run properly!
as for your questions:
1. I deleted 400 lines of code from the class to bring the problem to light. I intended to use it for writting binary files contining plotting instructions (for a plotter I wrote) as well as data from numerical simulations I am working on. The bottom line is that I wanted an easy way to write the data from those programs and initialize the plot. (I don't want to have to use ofstream directly every time especially to load up the default plot settings I don't want to change).
2. I am unfamiliar with basic_ofstream? Could you point me somewhere?
Thank you for your help
|
|
|
|