Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 19-Sep-12 21:18pm
Comments
Richard MacCutchan at 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 at 20-Sep-12 3:39am
   
I have posted only this question with this subject
Richard MacCutchan at 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 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Parser will get three information(On, Active, Temperature) at time X, then parser write it to file like this.
 
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
 
printf( "%s %s,%s,%s", X, On, Active, Temperature );
  Permalink  
v2
Comments
Tarun Batra at 20-Sep-12 3:40am
   
sir i have to write in log file not on console
Richard MacCutchan at 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 at 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 at 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 at 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
Richard MacCutchan at 20-Sep-12 5:52am
   
Well, the main problem that I see is that you really do not understand how to format output using the printf()/fprintf() functions.
 
Similarly you are using strcpy() incorrectly, in that you call it three times to the same buffer which just overwrites what you have previously copied.
Tarun Batra at 20-Sep-12 3:40am
   
should i paste my log file code
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
  Permalink  
v2
Comments
Tarun Batra at 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 at 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 at 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 at 20-Sep-12 18:50pm
   
:-) :-) :-)
pasztorpisti at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 901
1 OriginalGriff 445
2 CPallini 255
3 Richard Deeming 175
4 George Jonsson 161
0 OriginalGriff 5,540
1 Sergey Alexandrovich Kryukov 4,668
2 CPallini 4,540
3 George Jonsson 3,102
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100