get file recevied timestamp in C++


 
Thread Tools Search this Thread
Top Forums Programming get file recevied timestamp in C++
# 1  
Old 06-14-2011
get file recevied timestamp in C++

Hi,

Every time I have to get the file received time stamp using C++ code.
For that I have written a c++ script but I am having below error message.

Error information
Quote:
"sample.cpp", line 14.10: 1540-0802 (W) The character literal '/temp/new/temp1.txt' contains more than one character.
"sample.cpp", line 14.10: 1540-0256 (S) A parameter of type "const char * restrict" cannot be initialized with an expression of type "int".
"sample.cpp", line 14.10: 1540-1205 (I) The error occurred while converting to parameter 1 of "stat(const char * restrict, stat * restrict)".
Code
Code:
#include <stdio.h>
#include <stdint.h>
#include <iostream.h>
struct stat     statbuf;
struct tm      *tm;
char            datestring[256];
int  main()
{
    stat('/temp/new/temp1.txt', &statbuf);
    tm = localtime(&statbuf.st_mtime);

}

Any help greatly appreciated.
Thanks -onesuri
# 2  
Old 06-14-2011
You are confusing string constants with character literals. stat expects a null terminated string as its first argument, and In C and in C++, you use double quotes to represent a string constant.

Code:
stat("/temp/new/temp1.txt", &statbuf);

# 3  
Old 06-16-2011
Thanks flynn, that worked! But we ran into another trouble.
How can we return the values of char array as a value?

Code:
struct stat st;  // declaration of the stat
struct tm *tm; //declaration of tm pointer

char getts(char *filename1)
{
   char datestring[256];
   stat(filename1, &st);
   tm=localtime(&st.st_mtime);
   strftime(datestring,sizeof(datestring),"%Y-%m-%d-%H.%M.%S" , tm);
   return datestring;
}

For Ex:
cout<<datestring;
Output: 2011-05-24-05.46.30

I want to return this whole value without any pointers because the returned value is used by another application.

Any help greatly appriciated.
Thanks-onesuri
# 4  
Old 06-16-2011
There are two problems with this function:

Code:
struct stat st;  // declaration of the stat
struct tm *tm; //declaration of tm pointer

char getts(char *filename1)
{
   char datestring[256];
   stat(filename1, &st);
   tm=localtime(&st.st_mtime);
   strftime(datestring,sizeof(datestring),"%Y-%m-%d-%H.%M.%S" , tm);
   return datestring;
}

First, it is defined as returning a char, but actually what you are returning is a char* (datestring is a char*, not a char). Second, you are returning a pointer to a local variable (the datestring is an array local to the getts function). This could lead to unpredictable results. To correct this, you must make this array static or global to the program.

Code:
char* getts(char *filename1)
{                                                                               
   static char datestring[256]; /* or make it global */
   stat(filename1, &st);                                                        
   tm=localtime(&st.st_mtime);                                                  
   strftime(datestring,sizeof(datestring),"%Y-%m-%d-%H.%M.%S" , tm);
   return datestring;                                                        
}

but even with this corrections we still have potential problems. This is because this function is not thread safe, and you should not use this approach in multi-threaded programs. The following changes will make the function thread safe:

Code:
char* getts(char *filename1, char *datestring, size_t datestringsize)
{                                                                               
   struct stat st; /* st and tm can not be global if you want to make this function */  
   struct tm tm; /* thread safe, so make them local.                                          */

   stat(filename1, &st);
/* use the reentrant (thread safe) version of localtime, localtime_r */
   localtime_r(&st.st_mtime, &tm);
   strftime(datestring,datestringsize,"%Y-%m-%d-%H.%M.%S" , &tm);
  /* now you are returning a pointer to a user allocated buffer. User can do anything (change it) safely */ 
  return datestring;                                                        
}

Code:
int
main(int argc, char *argv[])
{ 
/* user allocated buffer */
char datestring[256];

  puts(getts("demo.c", datestring, sizeof(datestring)));
  return 0;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep lines between last hour timestamp and current timestamp

So basically I have a log file and each line in this log file starts with a timestamp: MON DD HH:MM:SS SEP 15 07:30:01 I need to grep all the lines between last hour timestamp and current timestamp. Then these lines will be moved to a tmp file from which I will grep for particular strings. ... (1 Reply)
Discussion started by: nms
1 Replies

2. Shell Programming and Scripting

Picking the latest file based on a timestamp for a Dynamic file name

Hi , I did the initial search but could not find what I was expecting for. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

3. Shell Programming and Scripting

AIX : Need to convert UNIX Timestamp to normal timestamp

Hello , I am working on AIX. I have to convert Unix timestamp to normal timestamp. Below is the file. The Unix timestamp will always be preceded by EFFECTIVE_TIME as first field as shown and there could be multiple EFFECTIVE_TIME in the file : 3.txt Contents of... (6 Replies)
Discussion started by: rahul2662
6 Replies

4. Shell Programming and Scripting

To check timestamp in logfile and display lines upto 3 hours before current timestamp

Hi Friends, I have the following logfile. Currently time in india is 07/31/2014 12:33:34 and i have the following content in logfile. I want to display only those entries which contain string 'Exception' within last 3 hours. In this case, it would be the last line only I can get the... (12 Replies)
Discussion started by: srkmish
12 Replies

5. UNIX for Dummies Questions & Answers

Deleting file basing on the timestamp substring in the file name

Hello, I have in my backup folder, files with names convention like this : randomFileNames_13-02-2014_23h13m09+1392333189 randomFileNames_14-02-2014_02h13m09+1392343989 randomFileNames_14-02-2014_04h13m09+1392351189 etc.... Base on timestamp at end of the filename, I would to delete all the... (7 Replies)
Discussion started by: thuyetti
7 Replies

6. Shell Programming and Scripting

Identifying files with a timestamp greater than a given timestamp

I need to be able to identify files with file timestamps greater than a given timestamp. I am using the following solution, although it appears to compare files at the "seconds" granularity and I need it at the milliseconds. When I tested my solution, it missed files that had timestamps... (3 Replies)
Discussion started by: nkm0brm
3 Replies

7. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

8. Shell Programming and Scripting

Getting a relative timestamp from timestamp stored in a file

Hi, I've a file in the following format 1999-APR-8 17:31:06 1500 3 45 1999-APR-8 17:31:15 1500 3 45 1999-APR-8 17:31:25 1500 3 45 1999-APR-8 17:31:30 1500 3 45 1999-APR-8 17:31:55 1500 3 45 1999-APR-8 17:32:06 1500 3 ... (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

9. UNIX for Dummies Questions & Answers

Need help finding a file where a pattern exists and the file has a timestamp

So, I know how to do some of this stuff on an individual level, but I'm drawing a blank as to how to put it all together. I have a pattern that I'm looking for in a log file. The log file I know came in yesterday, so I want to limit the search to that day's listing of files. How would I do... (5 Replies)
Discussion started by: kontrol
5 Replies

10. Shell Programming and Scripting

conversion of different timestamp to standard timestamp

hi i need a scrit to convert one date format to another. for example i have three columns in a file which gets a different format, but lastly i want output with stadard timestamp as "yyyy-mm-dd hh:mm:ss" column1 column2 ... (2 Replies)
Discussion started by: dprakash
2 Replies
Login or Register to Ask a Question