Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing to a file

Status
Not open for further replies.

mbkelly

Technical User
Dec 22, 2009
11
US
I am working a project in Visual Studio 2008, using C++ that is started using a custom wizard. The wizard was provided by the vender and their starter code works fine. It appears to be in .NET but I do not have sufficient knowledge to determine the exact style in terms of MFC, CLR, etc. I need to grab some data and write it to a file. I'll start with a text file, then add a binary file. Here are the relevant lines of the nonsense I have so far.

int number1 = 11;
int number2 = 22;

CString tempBuffer;
TCHAR pfileName = _T("C:\temp.txt");
CStiolFile f1;
f1.Open( pFileName, Cfile::modeCreate | <and someother options>);

tembBuffer = "\nnumber1:" + number1 + " number2" + number2;
f1.WriteWtring( tempBuffer );
f1.Close;

This code writes "number is" but does not get the value.

I tried:

tempBuffer.Format(" number1 is %d", number1 );

But then the compiler complained about the line several lines up:
TCHAR pfileName = _T("C:\temp.txt");
saying T is an unrecognized character escape sequence.


In summary, I just need to open a file and write a bunch of values to it.
It always seems that the easy and simple stuff is so hard to find. But maybe I'm barking up the wrong tree. Give me some direction and I will be glad to use a different concept that is more appropriate.

Thank you
 
Code:
_T("C:\\temp.txt");
That's all. Remember string literal syntax...
 
ArkM,
Thanks for taking the time to respond, but that's not my problem. Obviously I left out the \\ when writing the post.
The problem is this:
tembBuffer = "\nnumber1:" + number1 + " number2" + number2;
How do I get the values of the numbers into the string so they can be written to the file?

 
After more searches I finally found I can do this:
int temp_data = 13;
CString temp_string;
...
temp_string.Format( _T("\ntemp_data is: %d", temp_data ));
f1.WriteString( temp_string );
f1.Close
...

But when I look in the file, it contains:

temp_data is 899889814

I don't know how 13 translates to that, but it appears there is something I have omitted.

Second question: Is this the best basic technique to use for going forward within MFC when using Visual Studio C++, or maybe C#?


 
Try
Code:
temp_string.Format( _T("\ntemp_data is: %d"), temp_data );
 
Thank you, I have it working now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top