写个C++代码读取txt文件,txt文件有两列数据
时间: 2023-08-29 22:08:16 浏览: 112
好的,以下是一个简单的C++代码示例,可以读取txt文件中的两列数据:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream file("data.txt"); // 打开文件
string line;
while (getline(file, line)) { // 逐行读取文件内容
int a, b;
sscanf(line.c_str(), "%d %d", &a, &b); // 使用sscanf解析每行的两个整数
cout << "a: " << a << ", b: " << b << endl; // 输出读取到的数据
}
file.close(); // 关闭文件
return 0;
}
```
其中,`data.txt`是我们要读取的txt文件,每行有两个整数,用空格隔开。程序使用`ifstream`打开文件,然后使用`getline`逐行读取文件内容。在每行读取到的字符串中,我们使用`sscanf`按照`%d %d`的格式解析出两个整数,然后输出到控制台上。最后,我们使用`close`关闭文件。
相关问题
用C++写一段把csv文件里面的两列浮点型数据读取到容器里面
当然!下面是一个示例代码,它可以读取一个包含两列浮点数的 CSV 文件,并将它们存储在一个容器中:
```cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <utility>
int main() {
std::ifstream file("data.csv");
std::vector<std::pair<double, double>> data;
if (file) {
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string value;
double number1, number2;
if (std::getline(iss, value, ','))
number1 = std::stod(value);
if (std::getline(iss, value, ','))
number2 = std::stod(value);
data.emplace_back(number1, number2);
}
file.close();
// 打印容器中的数据
for (const auto& pair : data) {
std::cout << pair.first << ", " << pair.second << std::endl;
}
} else {
std::cout << "无法打开文件" << std::endl;
}
return 0;
}
```
在这个示例中,我们创建了一个存储两列浮点数的 `std::pair<double, double>` 类型的容器 `data`。我们逐行读取 CSV 文件中的内容,并使用逗号作为分隔符将每行拆分成两个值。然后,我们将这两个值转换为浮点数,并使用 `emplace_back` 函数将它们作为一对插入到容器中。
最后,我们遍历容器,并打印出每对浮点数。
请确保将示例代码中的文件名替换为你实际使用的 CSV 文件的路径和名称。此外,还需要包含 `<iostream>`, `<fstream>`, `<sstream>`, `<vector>` 和 `<utility>` 头文件来使用相应的标准库函数、容器和工具。
用c++读取execl中指定两列的数据
可以使用第三方库libxl来读取Excel文件中指定两列的数据,以下是一个简单的示例代码:
```c++
#include <iostream>
#include <libxl.h>
using namespace std;
using namespace libxl;
int main() {
Book* book = xlCreateBook();
if (book) {
if (book->load("example.xls")) {
Sheet* sheet = book->getSheet(0);
int rowCount = sheet->lastRow();
for (int i = 0; i < rowCount; ++i) {
int column1 = sheet->readNum(i, 0);
double column2 = sheet->readNum(i, 1);
cout << column1 << "\t" << column2 << endl;
}
}
book->release();
}
return 0;
}
```
在这个示例代码中,我们首先创建一个`Book`对象,并使用`load`方法加载Excel文件。然后获取第一个工作表,并通过`lastRow`方法获取行数。接下来使用一个循环,读取第一列和第二列的数据,并输出到控制台上。最后释放`Book`对象并返回0。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)