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.
Related
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 9 days ago.
This post was edited and submitted for review 8 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
My husband owns a Bernina embroidery machine and I'm eager to explore the possibility of generating designs through programming. What programming language would be best suited for writing a program to output data in a format compatible with the Bernina embroidery machine's software, or for converting from a format such as HPGL to the appropriate format for the machine? My objective is to either figure out how to output data in a format compatible with the machine's software, or alternatively, find a free or low-cost tool that can convert from a format I can produce, such as HPGL or any other well-documented format, to the appropriate format for the machine. My aim is to create a file with one XY coordinate per stitch, including instructions for thread changes (pauses for thread switching), without relying on features like area fills, stitch spacing adjustments, or stitching order optimization, as I prefer to manage these elements myself.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
File data example line:
Sicilia 005 frenkco mastro 202020kkk 3 11-23-2155 12-44-6666
I want to overwrite that line with another same line copied but with some changed values:
Calabria 006 frenkco mastro 202020kkk 3 11-23-2155 12-44-6666
That's the result i want, but if I change values and then use "fprintf", it prints a new line without deleting the first one.
You cannot overwrite a specific line, because lines are just conventions related to \n bytes. Please read more about C programming and consider reading the n1570 C standard.
You either want to copy the source file to a target one (this is how sed(1) works on Linux; study its source code since it is free software) or use higher-level approaches such as gdbm or sqlite or databases.
For small amount of data practically fitting in RAM (e.g. less than a gigabyte) or in your page cache, a common approach is to generate a new textual file entirely. It might be a temporary file that you'll rename (e.g. using atexit(3) or simply later on)
You could want to read a textbook on operating systems and/or learn to use databases (perhaps MongoDB or PostGreSQL, but there are many other approaches too) or indexed files.
In some cases, you might consider serialization and parsing techniques, perhaps using textual formats like JSON or YAML (for which many open source libraries are available, and worth studying).
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I want to write a program that formats a disc to a new format (not NTFS or FAT32) and can read from and write to the disc, In order to do this, I have to have a way to write single bytes to a disc (without creating files). How do I do that?
EDIT: I found this. But I'm not sure if using CreateFile() as Eli Bendersky said in the first answer (when organized in order of votes) let's you write one byte (or even one bit) at a time, or do you have to write full sectors at a time.
Answer: Since I can't post an answer the question because it was closed, I will answer it write here. You don't need need API functions to do this. All you need to do is open the disk like you open any other file. Like this:
int hFile;
hFile=open("/dev/sdb",0_RDWR);
You program has to directly talk to the driver as you will be by-passing the file-system.
This question already has an answer here:
Determining if file has been copied or not in C [closed]
(1 answer)
Closed 9 years ago.
In Windows, if you go to a file's properties it shows the last access time right under the time last modified. This changes when I copy it.
How do I view this in C?
You can use the GetFileTime() function to get it. This MSDN article has more details about file times.
The portable way of getting the last modified timestamp is by using fstat or stat. If you want to go the Windows-only route (by directly calling a Windows API), see #xxbbcc's answer.
See How can I get a file's size in C++? for a short piece of sample code that uses stat/fstat - only change for your purpose is that you'll want to read the time_t st_mtime field.