Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Save data to text file in C++

Status
Not open for further replies.

nsw1216

Member level 3
Joined
May 7, 2010
Messages
60
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,667
Hello?
I am new in Visual C++ programming and interfacing. Currently I am trying to create an monitoring system interface which can communicate with my PIC USB device. I have created an interface which can read and send information from/to PIC.

Now, I would like to add a function which is saving data of my monitoring system from time to time as a record in text. However, i only found the manually saving data examples but not the automatically type.

Is there anyone know what should I do to achieve what i want ?
Is there any examples about this matter ?

Your help are most welcome and appreciated.
By the way, I am using Microsoft Visual Studio 2010 in this project.

Thank you.
 

horace1,

But i noticed that, from the example i found, the syntax used in the examples are different for the standard C++ file IO methods.
For example,
Code:
private: System::Void SaveData_Click(System::Object^  sender, System::EventArgs^  e) {
		 if(AttachedState == TRUE)
		 {
			double TTemperature = TemperatureValue/2.0;
			double TTemperature1 = TemperatureValue1/2.0;
			String^ fileName = "MonitoringData-%date:~4,2%-%date:~7,2%-%date:~12,2%.txt";
			StreamWriter^ sw = gcnew StreamWriter(fileName);
			sw->WriteLine("Data Logged at {0}",DateTime::Now);
			sw->WriteLine("");
			sw->WriteLine("Temperature   : {0} Degree Celsius", TTemperature);
			sw->WriteLine("Temperature1   : {0} Degree Celsius", TTemperature1);
			sw->WriteLine("");
			sw->WriteLine("Data Logged Using Monitoring System (c) 2010 Sunny Group");
			sw->Close();
			MessageBox::Show( "File Saved!","Save Complete!",
            MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
		 }

		 if((AttachedState == FALSE) || (AttachedButBroken == TRUE))
		 {
			MessageBox::Show( "Please Connect Monitoring System!","Save Data Error!",
            MessageBoxButtons::OK, MessageBoxIcon::Exclamation );

		 }

	}

Thanks for your respond.

Regards,
nsw1216
 

your example is using Microsoft API specific methods. I would tend to use standard C++ IO as the code is then portable to other compilers and operating systems
 

horace1,

Microsoft API? Is there anyway can modify the code to automatically saving the data of my monitoring system ?
 

I use standard C++ stream IO to read/write files. It is simple, e.g. modified from the C++ tutorial on file
Code:
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  int idata=23;
  float fdata=3.14159f;
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile << "integer " << idata << " float " << fdata << endl;
  myfile.close();
  return 0;
}
opens the output file example.txt and then uses the << (insertion ) output operator to write data to the file.
the output files looks like
Code:
Writing this to a file.
integer 23 float 3.14159
 

horace1,

I understand about this. But i can mixed it with the microsoft API coding ? Because I created an interface with microsoft API already.Now I just wanted to modify it to be saving data continuously.
 

I have had no problem mixing the Microsoft GUI API with normal C++ IO, e.g. when using Windows Forms I use cout << to write debug infomation to a console window.
Centainly don't mix call to Microsoft's stream API (StreamWriter etc) with standard C++ stream IO, use one or the other.
 

horace1,

means i need to include the header files in order to use the standard C++ stream IO ?
 

yes, you need
Code:
#include <iostream>
#include <fstream>
using namespace std;
 

horace1,

Thanks for the explanations. But i would like to learn the Microsoft API specific method to save data instead of standard C++ IO stream. Do you know any information or reference about it ?
 

horace1,

Thanks for the suggestions.
 

horace1,

I got one questions about setting the filename. IS it possible i attach the date into the filename i created ?
 

yes, you can read the system date and time as a character string and create the filename using it, see
localtime - C++ Reference

rememeber that the system saves the date and time when a file is created (but it is still often useful to have it as part of the filename)
 

horace1,

Oh I See. Is there any way that using Microsoft API specific method like DateTime::Now or DateTime::Today ?
 

I would guess so but never having used DateTime cannot say for certain - I tend to try to keep to standard C/C++ where possible avoiding platform specific APIs.
All you need is a method which will extract the date from DateTime as a char array or a string that can be used as part of a filename.
look at the the documentation of the API.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top