Hi,

Is there any way in C++ to select and read the last modified file from a unix directory?

Let say the o/p of the ls -t is :

abc.txt
myfile.txt
....
....

last_file.txt

I want to select this last file ( can be of any name) and read the same using C++.

Thanks in advance for your quick help.

Thanks,
Parvez

I want to do it in unix system not windows and also not want to use any specific header file (dirent.h).

A quick reply form any of the user highly appreciated.

how will ls work?
ls only give the names of the files present in that directrory that too shorted by name and not by modified time :(

Since you already use Linux you probably have use ls from the shell many times. Look at the man pages for ls and you will find all it's options. I'm sure there will be an option to list the modivied time as well as file name. You might have to sort the list by modified time yourself.

Edited 2 Years Ago by Ancient Dragon

Typically, I use the following on the command line to get the name of the last modified file.
ls -ltr | grep ^- | tail -1 | awk '{ print $(NF) }'

To include files beginning with ., I use
ls -latr | grep ^- | tail -1 | awk '{ print $(NF) }'

Edited 2 Years Ago by Moschops

Thanks everyone for your reply.
I'm aware of basic unix commands. But that is not what I want.

I want to write a code/program in C++ that will find the latest modified file in the unix/linux directory. Please note that there may be n number of files in that directory.

example of the dir:

ls -rt:

abc.txt
file.txt
get.txt
myfile.txt
first.txt
here.txt
kit.txt
last_modified.txt

Also note that, I'm not aware the file name of the last modified file.

You can call the basic linux command from within c or c++. For example you could use pipe() to run the ls -rt command and instead of displaying the results on the console screen it will send the results to your program so that you can process them however you wish. You may have to save the results in an array then sort the array by last modified time.

AFAIK there are no other ways to accomplish what you want without resorting to one of the suggestions already posted in this thread. That is, unless you want to write your own operating system in which case you can do whatever you want.

Edited 2 Years Ago by Ancient Dragon

For God sake you could export it in txt file and read it from there!

Sorry if it sounds bit like, way did you call his name in wain.
I will go to curch and do all thos ....

getting error while using system call

#include <iostream>

using namespace std;


int main()
{
char str[100];
 str = system("ls -ltr | grep ^- | tail -1 | awk '{ print $(NF) }'");
cout << str;
return 0;
}

sunfsd17 linus> g++ eq.cpp
eq.cpp: In function int main()': eq.cpp:9: error: incompatible types in assignment ofint' to `char[100]'

please someone can help. want to read the last file in the present directory in a variable str.

You are trying to write the returned value from the function system into a char array. Let's see what system returns; http://www.cplusplus.com/reference/cstdlib/system/

int system (const char* command);

System returns an int. A single number. It does not return whatever the system call put to the screen.

If you want to do this in a C++ program, you need to:

1) Get list of files in a directory.

There is no way to do this without a system specific header. The linux system is a specific system and you want to work with it. Even if you don't use a system specific header yourself, anything you use to get the list of files will. You can only get this list by ultimately asking the operating system for it, and there is no way to ask the operating system in a way that is not somehow system specific.

If you decide that you are willing to do this in a system specific way, you can do it yourself with dirent.h, or you could use somethign like Boost::filesystem which is much nicer to work with.

2) Get the modified time of each file.

3) Pick the one with the latest modification time.

Edited 2 Years Ago by Moschops

duskoKoscica: why are you making this soooo difficult for yourself???

  1. include stdlib.h header file

  2. redirect the output the string you put in the system call to a file.

    "ls -ltr | grep ^- | tail -1 | awk '{ print $(NF) }' >tempfile.txt"

  3. Read the file created by #2 above.

Edited 2 Years Ago by Ancient Dragon

I feel that I should say insisting on no system specific header files and then doing it with a command line specific not to just the system, but also to the command shell in use on that system, seems rather odd.

Edited 2 Years Ago by Moschops

So decided to write this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>


 /**
  *  @Description: Returns last modified file for unix systems
  *       @Params: path as char string
  *
  *      @returns: eg. Tue Jan 21 21:57:29 2014
  */
void getFileCreationTime(char *path) 
{
    struct stat attr;
    stat(path, &attr);
    printf("Last modified time: %s", ctime(&attr.st_mtime));
}


int main(void)
{
  DIR           *d;
  struct dirent *dir;
  d = opendir("."); //set path to directory
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
        //exclude "." and ".."
        if( strcmp( dir->d_name, "." ) == 0 || 
        strcmp( dir->d_name, ".." ) == 0 ) 
        {
            continue;
        }

        printf("%s\n", dir->d_name);
        getFileCreationTime(dir->d_name);

    }

    closedir(d);
  }

  return(0);
}

Then it is simply a case of sorting the output to get the most recent date.

This article has been dead for over six months. Start a new discussion instead.