문제

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!

도움이 되었습니까?

해결책

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.

다른 팁

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);

SharePoint Designer 워크 플로우를 찾고있는 것입니다. 내가 생각할 수있는 것은이 논리이다. 워크 플로를 만들 때 시작 옵션을 선택하십시오.

를 선택합니다

  • "항목이 생성 될 때 자동으로 워크 플로우" - 항목이 생성 될 때 할당 된 열에서 사용자 A를 확인하고 이메일을 보내야합니다.

  • "항목이 변경되면 자동으로 워크 플로우 시작" - 목록 항목이 할당 된 열에서 사용자 B로 업데이트 될 때 옵션 덕분에 그 대신에 이메일을 보내야합니다. 항목이 변경 될 때 워크 플로를 시작하려면

    여기에 이미지 설명을 입력하십시오

    이 스레드가 더 이상 도움이 될 수 있습니다. 사람들이 이메일을 이메일로하는 디자인 워크 플로우 할당 된 (생성 된 것 제외)

    편집 : 나는 이것을 테스트하지 않았습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top