Writing directory listing to a file

Hi All,

I need to get the directory listing from the below program to a file but I tried many ways & it failed.

Can someone give me the code or suggestion to get this output to a file?


#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int
main (void)
{
DIR *dp;
struct dirent *ep;

dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
puts (ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");

return 0;
}

Mathew
Assume your program is called a.out then all you got to do is a.out > myfile
myfile will now contain what you want.
Hi Sohguanh,

Thank you for the reply. What I want is to do that within the code. Reason is my final code is more than this. So I am looking for away to do it within the code.

Thanks to let me know if you have any idea.
Any more replies are welcome.

Mathew
Given your existing code, you could just swap the puts to fputs
http://www.cplusplus.com/reference/clibrary/cstdio/fputs/

This would require the minimum amount of change to your program. (you will also need fopen and fclose)

Or you could use ofstream instead.

But I'm not that well versed in the Linux way of doing thing : I do mainly Windows plus some cross-platform...

If you just want to create a file with the directory listing in it, and have no other use for the info in your program, it might be better to just get the system to run the ls command, redirecting its output to a file.

Last edited on
Hi Andywestken,


Wow, thanks "fputs" worked as below.

As suggested, I can not do "ls" as this code is going to be part of a bigger code that I am planning to develop. In fact, I have a program built with BASH & is working. Now that I am learning C++, I am trying to do the same with C++.

In short, I am having the below with BASH & tying to get it with C++

for ii in *.txt
do
...
...

done

Now I am facing with the requirement to take out what I needed from this file (from below - file-list.txt).

The contents of new file is as below; I lost all new line with "fputs" but I am fine if I can find a way to capture all the strings that matchs "Test_*.txt". These are file names that I want to capture into a raw so that I can search some strings in those in the directory.

If you have any idea how to capture into a new file with a "\n" at the end, I will have a new file with a all the file names that matches "Test_*.txt" so I can do my rest of the code.

I will also post this in a new topic if I could not find an answer in this.

...read-all-files.cca.outTest_ABCDEF1.txtTest_ABCDEF2.txtTest_HGHJJ29.txtTest_dfedd4.txtTest_fgtty6.txtread-all-files-1.ccread-all-files-2.ccread-all-files-3.ccread-all-files-to-file-1.ccread-all-files-4.cctest1w-list.txt...read-all-files.cca.outTest_gghhh5.txtTest_jjjjk7.txtTest_KJKKL7.txtTest_hjggh9.txtTest_hjsss3.txtread-all-files-1.ccread-all-files-2.ccread-all-files-3.ccread-all-files-to-file-1.ccread-all-files-4.cctest1w-list.txt...read-all-files.cca.outTest_hghjj5.txtTest_hhggg4.txtTest_ASDDD5.txtTest_DDDDDEE3.txtTest_Hkkkkr5.txtread-all-files-1.ccread-all-files-2.ccread-all-files-3.ccread-all-files-to-file-1.ccread-all-files-4.cctest1w-list.txtCE-LIST-file-1.ccread-all-files-5.cc


The below code worked to write the output of directory list to file "file-list.txt"

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main (void)
{
DIR *dp;
struct dirent *ep;

FILE * pFile;
pFile = fopen ("file-list.txt","a");

dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
fputs (ep->d_name,pFile);
(void) closedir (dp);
fclose (pFile);
}
else
perror ("Couldn't open the directory");

return 0;
}


Mathew
You could use fputs or fputc to output the required ´\n´s
Topic archived. No new replies allowed.