在c++中读取多个.dat文件
Read multiple .dat files in C++
我使用下面的代码读取一个。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");
}
要在Windows上列出目录中的文件,请参阅此链接:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200 (v = vs.85) . aspx
关于代码的说明:
-
不使用
fin.eof()
来测试输入的结束,看看为什么:eof of istream in c++ -
读取多个文件,如果使用相同的
fin
读取多个文件,请记住fin.clear()
在fin.close
之前。
下面的代码打印出目录D:\Test
中的文件名。如果您需要每个文件或子文件夹中的文件的绝对路径,请更改GetFiles
以实现此目的。根据我提供的链接,这是非常简单的。该代码在VS2012 Win7 Pro上进行了测试。
#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();
}
}
我觉得这样比较容易:
1-如果您提出读取文件并将其内容处理为自己的函数的代码:
void process_file( char* filename );
2-增加一个新的功能来列出目录的内容:char** list_dir( char* dir );
3-在main()中合并这两个函数
这使得代码更干净,更易于测试
我同意对这一点进行概括的建议。在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 );
相关文章:
- 在 c++ 中读取和写入.dat文件
- 在 c++ 中错误地读取.dat文件
- 使用ifstream从DAT文件加载数据
- 从函数中的dat文件创建一个2维数组
- 无法读取一个.dat文件以创建第二个.dat文件
- 读取.DAT文件并创建一个数组C
- 读取和写入.dat文件
- 使用 std::write 和 std::read C++ 读取包含 std::string 到.dat文件的结构
- C++:如何将数据输出到多个.dat文件
- 一次读取两个字节的.dat文件
- C++ 如何从.dat文件构建字符串的 2D 矩阵?5 列 x 行
- 为什么我的程序写入.dat文件中的同一行
- 正在从.dat文件中读取2个字节
- 创建.dat文件 (fstream) c++
- 正在从dat文件读取到数组中
- 如何在不显示十六进制的情况下读取dat文件中的文本?C++
- 如何使用条带化算法读取住宅.dat文件C++
- 写入和读取记录以.dat文件C++
- C++ 将字符串向量转换为给定.dat文件中的双精度向量
- 使用c++从.dat文件中选择“随机行”