Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
I have a program to connect to an hospital machine using socket,send request to the machine,receive data from the machine and parse the data received from the machine.
It write all the data fields in the txt file like
 09/17/12::17:28:17 status of machine is-on
09/17/12::17:28:17  Mode of machine is-active
09/17/12::17:28:17 Temp is -38


But Now as the requirement changes i want the data to be written as:-
09/17/12::17:28:17 on,active,38 


How can i do that?
Please tell by pseudo i would be great help from your side.
Posted
Comments
Richard MacCutchan 20-Sep-12 3:38am    
Given the number of questions you are posting on this subject, your time might be better spent reviewing C++ and its libraries.
Tarun Batra 20-Sep-12 3:39am    
I have posted only this question with this subject
Richard MacCutchan 20-Sep-12 4:04am    
Here is a link to your questions. Many of them could be solved quite easily by a little more research by you, reading the C++ manual and looking at the MSDN documentation.
Legor 20-Sep-12 3:52am    
If you are allready writing some formatted output to a logfile how could it ever be a problem to just change the output format (given that you really wrote the log output by yourself the first time. I mean according to your information you just want to output the same information to a single line which you are now writing to three seperate lines. If you've done it once there should be no magic to this.
Volynsky Alex 20-Sep-12 7:52am    
Probably you should have a function (probably function the member a class)
which is responsible for data input to log file.
You should change this function.

Parser will get three information(On, Active, Temperature) at time X, then parser write it to file like this.

C++
printf( "%s status of machine is %s", X, On );
printf( "%s Mode of machine is- %s", X, Active );
printf( "%s Temp is- %s", X, Temperature);


Please change like this

C++
printf( "%s %s,%s,%s", X, On, Active, Temperature );
 
Share this answer
 
v2
Comments
Tarun Batra 20-Sep-12 3:40am    
sir i have to write in log file not on console
Richard MacCutchan 20-Sep-12 4:06am    
Then use fprintf() instead. As I said above you could solve simple questions like this by a little more effort in learning the base library functions.
Tarun Batra 20-Sep-12 4:23am    
u said correctly fprintf can be used here is my function that use fprintf:-int WriteToDataFile(char* str)
{
FILE* log;
log = fopen(DataFilePath, "a+");
if (log == NULL)
return -1;
fprintf(log, "%s\n", str);
fclose(log);
return 0;
}But as you can see my output i need data and time in the beginning that i am not able to get my output should be 09/17/12::17:28:17 on,active,38 i am only able to print like this on,active,38 by doing like this sprintf(buf,",%s,%s,%d", status_of_machine,mode,tmp);WriteToDataFile(buf); just tell how to add date and time
Richard MacCutchan 20-Sep-12 4:57am    
Like I said, and keep saying, go and learn the library functions. You cannot create a fully working project by asking questions one at a time in this forum. Here is a link to one of the functions you can use to format the time and date. You can find the fprintf() link for yourself and see how to use that. Make use of MSDN, it contains the answer to all the simple questions, and many of the difficult ones.
Tarun Batra 20-Sep-12 5:31am    
int WriteToDataFile(char* str)
{
FILE* log;
char szDate[12];
char szTime[12];

char buf1313[2500];
_strdate( szDate );
_strtime( szTime );
strcpy(buf1313,szDate);
strcat(buf1313," ");
strcpy(buf1313,szTime);
strcat(buf1313," ");
strcpy(buf1313,str);

log = fopen(DataFilePath, "a+");
if (log == NULL)
return -1;
fprintf(log, "%s\n", buf1313);
fclose(log);
return 0;
}the above is my function to write to the file i did something like this:-sprintf(bufff,"%s,%d,",curretnt_data.mc_no,fill.Dialysate_flow);WriteToDataFile(bufff); But still i am not getting the date and time can u tell me the problem
C++
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );


char datebuff[1024];

sprintf( datebuff, "%02d/%02d/%02d::%02d:%02d:%02d", timeinfo->tm_mon + 1, timeinfo->tm_mday, 
( timeinfo->tm_year + 1900 ) % 100, timeinfo->tm_hour,
timeinfo->tm_min, timeinfo->tm_sec );

sprintf(buf,"%s,%s,%s,%d", datebuff, status_of_mac, hine,mode,tmp);
WriteToDataFile(buf);
 
Share this answer
 
v2
Comments
Tarun Batra 20-Sep-12 5:28am    
int WriteToDataFile(char* str)
{
FILE* log;
char szDate[12];
char szTime[12];
memset(buf1313, 0, sizeof(buf1313));
char buf1313[2500];
_strdate( szDate );
_strtime( szTime );
strcpy(buf1313,szDate);
strcat(buf1313," ");
strcpy(buf1313,szTime);
strcat(buf1313," ");
strcpy(buf1313,str);

log = fopen(DataFilePath, "a+");
if (log == NULL)
return -1;
fprintf(log, "%s\n", buf1313);
fclose(log);
return 0;
}the above is my function to write to the file i did something like this:-sprintf(bufff,"%s,%d,",curretnt_data.mc_no,fill.Dialysate_flow);WriteToDataFile(bufff); But still i am not getting the date and time can u tell me the problem
Santhosh G_ 20-Sep-12 7:04am    
strcpy(buf1313,szDate);
strcat(buf1313," ");
strcpy(buf1313,szTime);
after copying date, you are overwriting it with time.
strcpy(buf1313,szTime); use strcat instead of strcpy.
Richard MacCutchan 20-Sep-12 7:54am    
I really worry that people's lives may be depending on this person's code. I just hope someone finds out before they try and implement this in live mode.
pasztorpisti 20-Sep-12 18:50pm    
:-) :-) :-)
pasztorpisti 20-Sep-12 18:57pm    
strftime() would be a nice replacement to sprintf() in this solution.

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