在C++中读取文本文件的正确方法是什么?
What's the correct way to read a text file in C++?
我需要用c++编写一个程序,该程序必须以特定格式逐行读写文本文件,但问题是,在我的PC上,我使用Windows,而在大学里,他们使用Linux,我遇到了问题,因为这些操作系统的行结尾不同。
我是c++的新手,不知道我能不能让我的程序能够读取文件,不管它们是用Linux还是Windows写的。有人能给我点提示吗?谢谢!
输入如下:
James White 34 45.5 10 black
Miguel Chavez 29 48.7 9 red
David McGuire 31 45.8 10 blue
如果您已经可以读取文件,只需检查所有换行字符,如"n"
和"r"
。我很确定linux使用"rn"
作为换行符。
你可以阅读此页:http://en.wikipedia.org/wiki/Newline
,这里是所有ASCII码的列表,包括换行符:
http://www.asciitable.com/编辑:Linux使用"n"
, Windows使用"rn"
, Mac使用"r"
。感谢Seth Carnegie
使用没有最后(即分隔符)参数的std::getline
重载应该自动处理行结束符转换:
std::ifstream in("TheFile.txt");
std::string line;
while (std::getline(in, line)) {
// Do something with 'line'.
}
这里有一个简单的方法来删除字符串中的额外"r":
std::ifstream in("TheFile.txt");
std::string line;
std::getline(input, line));
if (line[line.size() - 1] == 'r')
line.resize(line.size() - 1);
由于结果将是CR LF,因此我将添加如下内容来消耗额外的内容(如果存在的话)。因此,一旦你读了,你记录调用这个,然后再尝试读下一个。
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
如果您知道要为每条记录读取的值的数量,您可以简单地使用">>"方法。例如:
fstream f("input.txt" std::ios::in);
string tempStr;
double tempVal;
for (number of records) {
// read the first name
f >> tempStr;
// read the last name
f >> tempStr;
// read the number
f >> tempVal;
// and so on.
}
这还不够吗
你好,我会分阶段给你答案。为了理解代码,请通读一遍。
阶段1:设计程序:我们的程序根据需求应该…
- …包括用于保存数据的数据类型的定义。也就是我们的6个变量的结构。
- …提供用户交互,即用户应该能够提供程序、文件名及其位置。
- …能够打开所选文件。
- …能够读取文件数据和将它们写入/保存到我们的结构中。
- …能够关闭文件读取数据后。
- …可以打印出保存的数据。
通常你应该把你的代码分成代表上述的函数。
阶段2:创建一个所选结构体的数组来保存数据
...
#define MAX 10
...
strPersonData sTextData[MAX];
...
第三阶段:允许用户输入文件位置和文件名:
.......
string sFileName;
cout << "Enter a file name: ";
getline(cin,sFileName);
ifstream inFile(sFileName.c_str(),ios::in);
.....
->第3阶段注1。用户提供的可接受的格式应该是:
c:\SomeFolder\someTextFile.txt
我们使用两个反斜杠而不是一个,因为我们希望它被视为文字反斜杠。
->第3阶段注2。我们使用ifstream,即输入文件流,因为我们想从文件中读取数据。这期望文件名为c类型字符串而不是c++字符串。因此,我们使用:
..sFileName.c_str()..
阶段4:读取所选文件的所有数据:
...
while (!inFile.eof()) { //we loop while there is still data in the file to read
...
}
...
最后的代码如下:
#include <iostream>
#include <fstream>
#include <cstring>
#define MAX 10
using namespace std;
int main()
{
string sFileName;
struct strPersonData {
char c1stName[25];
char c2ndName[30];
int iAge;
double dSomeData1; //i had no idea what the next 2 numbers represent in your code :D
int iSomeDate2;
char cColor[20]; //i dont remember the lenghts of the different colors.. :D
};
strPersonData sTextData[MAX];
cout << "Enter a file name: ";
getline(cin,sFileName);
ifstream inFile(sFileName.c_str(),ios::in);
int i=0;
while (!inFile.eof()) { //loop while there is still data in the file
inFile >>sTextData[i].c1stName>>sTextData[i].c2ndName>>sTextData[i].iAge
>>sTextData[i].dSomeData1>>sTextData[i].iSomeDate2>>sTextData[i].cColor;
++i;
}
inFile.close();
cout << "Reading the file finished. See it yourself: n"<< endl;
for (int j=0;j<i;j++) {
cout<<sTextData[j].c1stName<<"t"<<sTextData[j].c2ndName
<<"t"<<sTextData[j].iAge<<"t"<<sTextData[j].dSomeData1
<<"t"<<sTextData[j].iSomeDate2<<"t"<<sTextData[j].cColor<<endl;
}
return 0;
}
我现在要给你一些练习:D:D
1)最后一个循环:
for (int j=0;j<i;j++) {
cout<<sTextData[j].c1stName<<"t"<<sTextData[j].c2ndName
<<"t"<<sTextData[j].iAge<<"t"<<sTextData[j].dSomeData1
<<"t"<<sTextData[j].iSomeDate2<<"t"<<sTextData[j].cColor<<endl;}
为什么我使用变量I而不是让我们说MAX?
2)能否在第1阶段的基础上修改程序,如
int main(){
function1()
function2()
...
functionX()
...return 0;
}
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 通过JNI传递数据数组的最快方法是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 在C++中包含原型文件的正确方法是什么?
- 在 OpenCV C++ 中估计基本矩阵之前对相应点进行归一化的正确方法是什么?
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- 在OSX上使用CMake将Adobe的XMP工具包构建为共享库的最简单方法是什么?
- 将一系列整数放入类的最佳方法是什么?
- 从长整整转换为uint64_t的推荐方法是什么?
- 将此布尔值传递给此函数的最有效方法是什么?
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?