Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to print a log file to a text file abd when I open the text file it's blank. Any help is appreciated. Thank you.

C++
#include <iostream>
#include <fstream>

ofstream myfile;
myfile.open ("c:\\example.txt");
printf("\n   Source      IP: %s", ipSrc);
myfile << "\n   Source      IP: %s", ipSrc;
myfile.close();
Posted
Updated 24-Oct-11 18:34pm
v4
Comments
Sergey Alexandrovich Kryukov 25-Oct-11 0:56am    
When you learn hot to formal code in HTML? Please click "Improve question" and see how I fixed it. At least check up the posted result. You need to escape < >.
--SA
Member 7766180 25-Oct-11 12:49pm    
Thank you for showing me. I was trying to fix it ut it keep getting worse.

How can it possibly work? You open myfile but print some ipSrc (and you don't even show its declaration) using some printf who knows where. The declaration of ipSrc is important. Do you have an operator << for it? According to your format, it must be a string.

This is a right pattern of using ofstream:

C++
#include <iostream>
#include <fstream>
using namespace std;

//...

ofstream outfile ("test.txt");
myfile << "\n   Source IP: " << ipSrc;
outfile.close();


You can also use format, of course, but to combine it with << you will need to use different function, to print data into char* buffer: http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/[^].

—SA
 
Share this answer
 
Comments
Malli_S 25-Oct-11 1:01am    
But in any case,

myfile << "\n Source IP: %s", ipSrc;

will at least print "\n Source IP: %s" in the file.
Chandrasekharan P 25-Oct-11 1:16am    
Good Explanation.
Sergey Alexandrovich Kryukov 25-Oct-11 1:18am    
Thank you, Chandru.
--SA
Member 7766180 25-Oct-11 13:18pm    
Thank you SA and Chandru080. I'm getting the output now. The next problem is that I have several of these sprinkled throughout the code. "myfile << "\n Source IP: " << ipSrc;" but the output prints only one, I have the close t the end of the program. Do I have to set this up differently to get all of them to print to file, do I have to append to the file somehow? Thank you.
Member 7766180 25-Oct-11 13:37pm    
This works
myfile.open ("C:\\test.txt",ios::out | ios::app);
myfile <<"\n No Action Required:";
myfile << "\n Source IP:" << ipSrc;
myfile << "\n Destination IP:" << ipDest;
Your GetLastError() might be returning 5 (i.e. Access Denied) as you are trying to write the file on C Drive.

Try using other drive, it will work as the code is fine. Win7 doesn't allow any process to directly write to OS Drive due to UAC.
 
Share this answer
 
Comments
Member 7766180 25-Oct-11 13:41pm    
I have to write to C:\ I have no other drives on my computer. But it is working now, thank you.
Instead of putting like this
myfile << "\n   Source      IP: %s", ipSrc;


you can code like this
myfile <<"Source      IP: "<<ipSrc;


It works for me.
 
Share this answer
 
Comments
Malli_S 25-Oct-11 1:02am    
But in any case,

myfile << "\n Source IP: %s", ipSrc;

will at least print "\n Source IP: %s" in the file.
Chandrasekharan P 25-Oct-11 1:17am    
It does print "Source IP: Hello" for me in the example file.
Member 7766180 25-Oct-11 13:15pm    
Yes this prints, thank you,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900