0

以下のコードを使用して1つの.datファイルを読み取り、実行時間を見つけました。これは非常にうまく機能しました。異なる名前のファイルが20個以上あるため(名前を保持する必要があるため)、複数のファイルを読み取るループを作成しようとしましたが、機能しませんでした。このコードを開発して、特定のフォルダーにあるすべてのファイルを、ファイルの数に関係なく読み取るにはどうすればよいですか?(以下のコードに基づく)

#include <Windows.h>
#include <ctime>
#include <stdint.h>
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;

#include <cstring>

/* Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both
 * windows and linux. */

uint64_t GetTimeMs64()
{


 FILETIME ft;
 LARGE_INTEGER li;

 /* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
  * to a LARGE_INTEGER structure. */
 GetSystemTimeAsFileTime(&ft);
 li.LowPart = ft.dwLowDateTime;
 li.HighPart = ft.dwHighDateTime;

 uint64_t ret;
 ret = li.QuadPart;
 ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
 ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */

 return ret;

}




const int MAX_CHARS_PER_LINE = 512;
const int MAX_TOKENS_PER_LINE = 20;
const char* const DELIMITER = "|";

int main()
{
  // create a file-reading object
  ifstream fin;
  fin.open("promotion.txt"); // open a file
  if (!fin.good()) 
    return 1; // exit if file not found

  // read each line of the file
  while (!fin.eof())
  {
    // read an entire line into memory
    char buf[MAX_CHARS_PER_LINE];
    fin.getline(buf, MAX_CHARS_PER_LINE);

    // parse the line into blank-delimited tokens
    int n = 0; // a for-loop index

    // array to store memory addresses of the tokens in buf
    const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0

    // parse the line
    token[0] = strtok(buf, DELIMITER); // first token
    if (token[0]) // zero if line is blank
    {
      for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
      {
    token[n] = strtok(0, DELIMITER); // subsequent tokens
        if (!token[n]) break; // no more tokens
  }
}

    // process (print) the tokens
    for (int i = 0; i < n; i++) // n = #of tokens
      cout << "Token[" << i << "] = " << token[i] << endl;
    cout << endl;
  }
  uint64_t z = GetTimeMs64();
  cout << z << endl;
  system("pause");
}
4

3 に答える 3

1

これをカプセル化する提案に同意します。Windowsでは、コードは次のようになります

HANDLE h;
WIN32_FIND_DATA find_data;
h = FindFirstFile( "*.dat", & find_data );
if( h == INVALID_HANDLE_VALUE ) {
    // Error
    return;
}
do {
    char * s = find_data.cFileName;
            // Your code here
} while( FindNextFile( h, & find_data ) );
FindClose( h );
于 2013-03-23T04:17:55.293 に答える
1

Windowsのディレクトリ内のファイルを一覧表示するには、次のリンクを参照してください。

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx

コードに関する注意:

  1. fin.eof()入力の終わりをテストするために使用しないでください。理由を確認してください: C++でのistreamのeof

  2. 複数のファイルを読み取るには、同じものを使用して複数のファイルを読み取るかどうかをfin.clear()前に覚えておいてください。fin.closefin

アップデート:

次のコードは、ディレクトリ内のファイル名を出力しますD:\\Test。すべてのファイルまたはサブフォルダー内のファイルの絶対パスが必要な場合は、それを行うように変更GetFilesします。私が提供したリンクによると、これは非常に簡単です。コードはVS2012Win7Proでテストされています。

#include <windows.h>
#include <Shlwapi.h>

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

#pragma comment(lib, "Shlwapi.lib")

int GetFiles(const string &path, vector<string> &files, const string &wildcard = "\\*")
{
    wstring basepath(path.begin(), path.end());
    wstring wpath = basepath + wstring(wildcard.begin(), wildcard.end());

    WIN32_FIND_DATA ffd;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    DWORD dwError = 0;

    hFind = FindFirstFile(wpath.c_str(), &ffd);

    if (INVALID_HANDLE_VALUE == hFind) {
        // display error messages
        return dwError;
    }

    TCHAR buf[MAX_PATH];
    do {
        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            // directory
        } else {
            PathCombine(buf, basepath.c_str(), ffd.cFileName);
            wstring tmp(buf);
            files.push_back(string(tmp.begin(), tmp.end()));
        }
    } while (FindNextFile(hFind, &ffd));

    dwError = GetLastError();
    if (ERROR_NO_MORE_FILES != dwError) {
        // some errors
    }
    FindClose(hFind);
    return dwError;
}

int main()
{
    string path("D:\\Documents\\Visual Studio 2012\\Projects\\SigSpatial2013");
    vector<string> files;
    GetFiles(path, files);
    string line;
    ifstream fin;
    for (int i = 0; i < files.size(); ++i) {
        cout << files[i] << endl;

        fin.open(files[i].c_str());
        if (!fin.is_open()) {
            // error occurs!!
            // break or exit according to your needs
        }

        while (getline(fin, line)) {
            // now process every line
        }

        fin.clear();
        fin.close();
    }
}
于 2013-03-23T04:08:51.723 に答える
1

私はそれが簡単だと思います:

1-ファイルを読み取り、そのコンテンツを独自の関数に処理するコードを除外する場合:
void process_file( char* filename );
2-ディレクトリのコンテンツを一覧表示する新しい関数を追加します:char** list_dir( char* dir );
3- main()で2つの関数を結合します

これにより、よりクリーンでテスト可能なコードが作成されます

于 2013-03-23T04:10:18.653 に答える