c++ 读入txt文件
时间: 2023-11-03 07:22:07 浏览: 87
你可以使用C语言中的标准库函数来读取文本文件。下面是一个示例代码,演示了如何读取一个名为 "example.txt" 的文本文件:
```c
#include <stdio.h>
int main() {
FILE *file;
char ch;
// 打开文本文件
file = fopen("example.txt", "r");
// 检查文件是否成功打开
if (file == NULL) {
printf("无法打开文件\n");
return 1;
}
// 读取并打印文件内容
while ((ch = fgetc(file)) != EOF) {
printf("%c", ch);
}
// 关闭文件
fclose(file);
return 0;
}
```
上述代码中,通过调用 `fopen` 函数来打开文件,第一个参数是文件名,第二个参数是打开模式,"r" 表示以只读方式打开。然后使用 `fgetc` 函数逐个字符地读取文件内容,并使用 `printf` 函数打印出来。最后,通过调用 `fclose` 函数关闭文件。
请注意,这只是一个简单的示例,可能无法处理大型文件或特殊字符。在实际应用中,你可能需要进行更多的错误处理和适应不同情况的代码编写。
相关问题
c++读入文件创建对象
在C++中读取文件并创建对象,可以通过以下步骤实现:
1. 首先,需要定义一个类来表示文件中的数据。
2. 接着,定义一个函数,用于从文件中读取数据并创建对象。
3. 在函数中,需要打开文件并逐行读取数据。
4. 对于每一行数据,需要解析出各个字段,并使用这些字段创建一个对象。
5. 最后,将创建好的对象添加到一个数组或其他数据结构中,以便后续使用。
以下是一个简单的示例代码,仅供参考:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Person {
public:
string name;
int age;
string gender;
Person(string n, int a, string g) {
name = n;
age = a;
gender = g;
}
};
vector<Person> readFromFile(string filename) {
vector<Person> people;
ifstream inFile(filename);
if (!inFile) {
cout << "Unable to open file";
exit(1);
}
string line;
while (getline(inFile, line)) {
string name;
int age;
string gender;
// 解析每一行数据,获取各个字段
// 假设文件格式为:name,age,gender
size_t pos = 0;
string token;
pos = line.find(",");
name = line.substr(0, pos);
line.erase(0, pos + 1);
pos = line.find(",");
age = stoi(line.substr(0, pos));
line.erase(0, pos + 1);
gender = line;
// 创建一个Person对象,并将其添加到people数组中
Person p(name, age, gender);
people.push_back(p);
}
inFile.close();
return people;
}
int main() {
vector<Person> people = readFromFile("data.txt");
for (auto p : people) {
cout << p.name << " " << p.age << " " << p.gender << endl;
}
return 0;
}
```
C++从文件读入读出
您好!要从文件中读取和写入数据,您可以使用C++的文件流(fstream)库。下面是一个示例,演示如何从文件中读取数据并将其写入到另一个文件中:
```cpp
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream inputFile("input.txt"); // 打开输入文件
std::ofstream outputFile("output.txt"); // 创建输出文件
if (inputFile.is_open() && outputFile.is_open()) { // 确保文件已成功打开
std::string line;
while (std::getline(inputFile, line)) { // 逐行读取输入文件
outputFile << line << std::endl; // 将每行写入输出文件
}
inputFile.close(); // 关闭输入文件
outputFile.close(); // 关闭输出文件
std::cout << "数据已成功从文件读取并写入到另一个文件中。" << std::endl;
} else {
std::cout << "无法打开文件。" << std::endl;
}
return 0;
}
```
在此示例中,我们使用`ifstream`打开一个名为`input.txt`的输入文件,并使用`ofstream`创建名为`output.txt`的输出文件。然后,我们使用`std::getline`函数逐行读取输入文件,并使用`<<`运算符将每行写入输出文件。最后,我们关闭输入和输出文件,并输出一条成功消息。
请确保将`input.txt`文件放在与您的源代码相同的目录中,以便成功运行此示例。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)