在最近的一个项目中,需要用c++读取6000多个文件。大家都知道,c++读取文件的能力没有Java等语言那么强大,但是在经过了一系列的尝试之后,成功了。在实施过程中将其分为两步,便于理解。
(1)获取需要读取的文件的所有文件名,并保存到filepath.txt中 ;
(2)每次从filepath.txt中读取一行作为路径,最后用一个“for循环”,或者feof()函数控制结束。
一、以下共享获取文件名的程序,保证直接可以运行。
#include "stdafx.h"
#include <iostream>
#include "fstream"
#include "string"
#include "StrmidO.h"
#include <io.h>
#include <vector>
using namespace std;
int main()
{
FILE *filepath;
filepath = fopen("E:\\Jsonpath.txt", "a");
long Handle;
struct _finddata_t FileInfo;
if ((Handle = _findfirst("E:\\工程管理数据\\jsons\\*.json", &FileInfo)) == -1L)
printf("没有找到匹配的项目\n");
else
{
printf("%s\n", File