Question

just had a general question about how to approach a certain problem I'm facing. I'm fairly new to C so bear with me here. Say I have a folder with 1000+ text files, the files are not named in any kind of numbered order, but they are alphabetical. For my problem I have files of stock data, each file is named after the company's respective ticker. I want to write a program that will open each file, read the data find the historical low and compare it to the current price and calculate the percent change, and then print it. Searching and calculating are not a problem, the problem is getting the program to go through and open each file. The only way I can see to attack this is to create a text file containing all of the ticker symbols, having the program read that into an array and then run a loop that first opens the first filename in the array, perform the calculations, print the output, close the file, then loop back around moving to the second element (the next ticker symbol) in the array. This would be fairly simple to set up (I think) but I'd really like to avoid typing out over a thousand file names into a text file. Is there a better way to approach this? Not really asking for code ( unless there is some amazing function in c that will do this for me ;) ), just some advice from more experienced C programmers.

Thanks :)

Edit: This is on Linux, sorry I forgot to metion that!

Was it helpful?

Solution

Under Linux/Unix (BSD, OS X, POSIX, etc.) you can use opendir / readdir to go through the directory structure. No need to generate static files that need to be updated, when the file system has the information you want. If you only want a sub-set of stocks at a given time, then using glob would be quicker, there is also scandir.

I don't know what Win32 (Windows / Platform SDK) functions are called, if you are developing using Visual C++ as your C compiler. Searching MSDN Library should help you.

OTHER TIPS

Assuming you're running on linux...

ls /path/to/text/files > names.txt

is exactly what you want.

There are no functions in standard C that have any notion of a "directory". You will need to use some kind of platform-specific function to do this. For some examples, take a look at this post from Cprogrammnig.com.

Personally, I prefer using the opendir()/readdir() approach as shown in the second example. It works natively under Linux and also on Windows if you are using Cygwin.

Approach 1) I would just have a specific directory in which I have ONLY these files containing the ticker data and nothing else. I would then use the C readdir API to list all files in the directory and iterate over each one performing the data processing that you require. Which ticker the file applies to is determined only by the filename.

Pros: Easy to code

Cons: It really depends where the files are stored and where they come from.

Approach 2) Change the file format so the ticker files start with a magic code identifying that this is a ticker file, and a string containing the name. As before use readdir to iterate through all files in the folder and open each file, ensure that the magic number is set and read the ticker name from the file, and process the data as before

Pros: More flexible than before. Filename needn't reflect name of ticker Cons: Harder to code, file format may be fixed.

but I'd really like to avoid typing out over a thousand file names into a text file. Is there a better way to approach this?

I have solved the exact same problem a while back, albeit for personal uses :)

What I did was to use the OS shell commands to generate a list of those files and redirected the output to a text file and had my program run through them.

In pseudo code it would look like this, I cannot define the code as I'm not 100% sure if this is the correct approach...

for each directory entry
    scan the filename
         extract the ticker name from the filename
         open the file
              read the data
              create a record consisting of the filename, data.....
         close the file
         add the record to a list/array...
> sort the list/array into alphabetical order based on 
  the ticker name in the filename...

You could vary it slightly if you wish, scan the filenames in the directory entries and sort them first by building a record with the filenames first, then go back to the start of the list/array and open each one individually reading the data and putting it into the record then....

Hope this helps, best regards, Tom.

On UNIX, there's the handy glob function:

glob_t results;
memset(&results, 0, sizeof(results));
glob("*.txt", 0, NULL, &results);
for (i = 0; i < results.gl_pathc; i++)
    printf("%s\n", results.gl_pathv[i]);
globfree(&results);

On Linux or a related system, you could use the fts library. It's designed for traversing file hierarchies: man fts,

or even something as simple as readdir

If on Windows, you can use their Directory Management API's. More specifically, the FindFirstFile function, used with wildcards, in conjunction with FindNextFile

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top