How to get the last n files in a directory [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I need help with getting the last 3 files in a directory.
I have an application that creates files in it and they have dates in the file names displayed as MMDD eg 0301.
There are a few files that have differents names but all have the dates on them and I'm new to C programming and don't know how to display only the last 3 file names.
If someone could help me it would be really appreciated.
Thank you

The typical and most robust way would be:
Read the contents of the directory into an in-memory array (see opendir() and friends if you're on POSIX)
Sort the array based on the filenames (using qsort() of course)
Extract the three last elements
Of course this would be brittle if files can be created while you're doing this, that is typical for this kind of filesystem-inspection.

Related

Count amount of words, characters, newlines in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I know it is not recommended to post code as an image, since I am getting a formatting error when trying to post code on here. Anyway, I am trying to write a simple C program to count number of words, characters, newlines using do while loop. However, the output is not as expected. Please help!
Instead of a do-while, you should try using while. Since your code starts off by checking whether a is any of your if cases, it goes to the else case, and increments the New line variable. If possible, could you share the output screen.

Converting a character into a space in C arrays [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So lets say I have received a message that resembles the following
"L2N5*8R10!11T0A1K3Y14#4W7O6O9C12R13"
and I am expected to sort out the characters in accordance to the numbers succeeding them and change the characters that are not letters into a space. I have no problem doing the sorting out part, I am only having a trouble while trying to write a function that will change those characters into space.
The out put should be something like this
TALK NOW OR CRY
but I am getting
TALK#NOW*OR!CRY
Can anyone help me figure out what my function should look like so that I can be able to change the characters into space??
Unless you show your code, we'll only be able to guess!!
However, as a general suggestion, I would recommended, you should check each entry against isalpha(). In case you got a return value of 0, replace the entry with a .

copy web informations in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm new to C so please explain your answers as much as you can :)
What i would like to do is this:
i want to create in C a program that goes to a specific site and copies certain data to a txt file from where i can analyze them.
The question is how i copy these informations?
is this something that can be done in C?(if yes tell me the library that i have to use or the code)
for example i want to make my program do this:
go to this site - copy the lines(1-10)
thanx for your help
Simply use curl link to the webpage -o file or wget link to page in your program with system(),
But by default you will get HTML source of page. From that you can copy your required info.
if your link is a text file like this
Then it will fetches as it is. Then you can open that file with fopen().read first ten lines with fgets(). copy into another file.
you can also use libcurl library

how to print a flat file list windows [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I thought this would be easy to do, but I can't wrap my head arond it.
I need to print a report of all the files in a dir, including sub dirs on a windows server.
Folder01\File01.txt
Folder01\File02.txt
Folder02\File01.txt
etc
Is there an easy way to spit that out to the screen?
Obviously i would pipe to file eventually, but I am still trying to figure out how to get a flat file list of a windows hierarchy printed out
You didn't give a language. There are many options. The simplest is probably dir/b/s in the Windows shell.

Find latest file name in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
Would like to fopen() the latest file in a directory
(with naming scheme file1.txt, file2.txt, file3.txt, etc.)
Is there an API function in Visual Studio for this?
If not, is it a good idea to read in all the .txt file names and sort to get the one I need? Is there a better algorithm I could be pointed to?
Thanks.
I'm going to assume by "latest" you mean "most recently modified file."
There is a C run time library function _fstat and _fstati64 (for large files > 4GB). The function signature for _fstat is:
int _fstat(int file_handle, struct _stat *file_info);
The _stat structure has a bit of useful information about the file, but you likely want the st_mtime member, which has the last modified time as a time_t (time in seconds since 00:00:00 UTC, January 1, 1970).
It may work to use the win32 functions FindFirstFile() and FindNextFile() to walk the directory, store the files in an array of a structure (containing the file name modified time) and then call qsort_s() on the array, sorting by time, in descending order.
I hope that helps.

Resources