如何在C 中读取一系列数据文件
How to read a sequence of data files in C++
你好,我有150个数据文件,每个文件中都有4列。我想知道如何将数据读取到C 程序中。我已经尝试在线查找,但是我遇到的所有资源都只是有关如何从一个来源输入数据的教程。数据也是一个顺序的,这意味着我正在阅读的文本文件,具有" line_u1.txt" ," line_u2.txt" 的格式>" line_u3.txt" ...,并且数据均一致地为:
column 1 = distances
column 2 = X_values
column 3 = Y_values
column 4 = Z_values
我想将这些数据导入C 程序,因为我尝试使用 MATLAB >,我认为数据是错误的,这就是为什么我要切换到C 程序,但是DON知道如何将150个数据文件读取到程序中。任何帮助将不胜感激。
根据此答案,C 17提供了一种在目录中获取所有文件的简便方法。
#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
std::string path = "/path/to/directory";
for (auto & filepath : fs::directory_iterator(path))
read_file(filepath); // You need to define this still
}
至于文件的实际读取,您应该能够使用您已经阅读的教程,但为了完整的答案
#include <fstream>
int distances = 0;
int x_values = 0;
int y_values = 0;
int z_values = 0;
void read_file(std::string filepath) {
/* Might want to do checking here to make sure
the file is actually one you want to read in */
std::ifstream infile(filepath);
while (infile >> distance >> x >> y >> z) {
distances += distance;
x_values += x;
y_values += y;
z_values += z;
}
}
在此答案中阅读更多文件。
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何对点云数据进行排序
- 如何在C 中读取一系列数据文件
- 设置一系列基/派生类来处理天气数据
- 如何将多组数据存储到一系列结构中
- 我可以使用STD ::复制将数据的位模式从整数向量复制到一系列未签名的字符