如何读取文件中的多行?
How to read from multiple lines in a file?
我正在学习 C++,但在处理文件方面遇到了一些困难。这是我正在尝试做的一个小练习。该程序旨在从文件中读取并将其设置为适当的变量。文本文件是这样的:
阿达拉·斯塔尔 94
大卫·斯塔尔 91
索菲亚·斯塔尔 94
玛丽亚·斯塔尔 91
丹妮尔·德菲诺 94
#include <fstream>
#include <iostream>
using namespace std;
const int MAXNAME = 20;
int main()
{
ifstream inData;
inData.open("grades.txt");
char name[MAXNAME + 1]; // holds student name
float average; // holds student average
inData.get(name, MAXNAME + 1);
while (inData)
{
inData >> average;
cout << name << "has an average of " << average << endl;
//I'm supposed to write something here
}
return 0;
}
当我尝试 运行 代码时,只有第一行被读取和显示,然后程序结束。更具体地说,输出是
Adara Starr has an average of 94
Adara Starr has an average of 0
如何从 txt 文件中读取下一行?我也做了 while (inData >> average)
代替 inData 条件,但它也做同样的事情减去第二个“Adara Starr 的平均值为 0”
您只读取一个名字,然后循环读取平均值。这与文件的内容不匹配。
使用getline
读取行,然后使用字符串流解析行:
#include <sstream>
#include <string>
#include <iostream>
int main()
{
std::stringstream ss{ R"(Adara Starr 94
David Starr 91
Sophia Starr 94
Maria Starr 91
Danielle DeFino 94)"};
std::string line;
while (std::getline(ss,line)) {
std::stringstream linestream{line};
std::string first;
std::string last;
double avg;
linestream >> first >> last >> avg;
std::cout << first << " " << last << " " << avg << "\n";
}
}
希望它能对您有所帮助,但我想警告您,由于内部 for
循环仅检查数字,如果 average
由浮动组成,您将丢失数据点数。
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
const int MAXNAME = 20;
int main()
{
ifstream inData;
inData.open("grades.txt");
for(string line; getline(inData, line);) {
// parse line
// get number index
size_t num_index = str.length() - 1;
for(; num_index >= 0 && is_digit(str[num_index]); --num_index)
;
string name = line.substr(0, num_index - 1); // skip space char
if(name.length() > MAXNAME)
throw runtime_error("Name could not be more that " + MAXNAME + " length");
float avarageStr = stof(line.substr(num_index + 1));
cout << name << "has an average of " << average << endl;
//I'm supposed to write something here
}
return 0;
}
希望对您有所帮助:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int MAXNAME = 20;
int main() {
ifstream inData("input.txt");
string str = "";
while (!inData.eof()) {//use eof=END OF FILE
//getline(inData, str); use to take the hole row
/*can use string also
string firstName;
string lastName;
string avg;*/
char firstName[MAXNAME];
char lastName[MAXNAME];
float avg;
//you can read from a file like you read from console(cin)
inData >> firstName >> lastName >> avg;//split the row
cout << firstName << " " << lastName << " " << avg<<"\n";
}
inData.close();
return 0;
}
输出:
阿达拉·斯塔尔 94
大卫·斯塔尔 91
索菲亚·斯塔尔 94
玛丽亚·斯塔尔 91
丹妮尔·德菲诺 94
我正在学习 C++,但在处理文件方面遇到了一些困难。这是我正在尝试做的一个小练习。该程序旨在从文件中读取并将其设置为适当的变量。文本文件是这样的:
阿达拉·斯塔尔 94
大卫·斯塔尔 91
索菲亚·斯塔尔 94
玛丽亚·斯塔尔 91
丹妮尔·德菲诺 94
#include <fstream>
#include <iostream>
using namespace std;
const int MAXNAME = 20;
int main()
{
ifstream inData;
inData.open("grades.txt");
char name[MAXNAME + 1]; // holds student name
float average; // holds student average
inData.get(name, MAXNAME + 1);
while (inData)
{
inData >> average;
cout << name << "has an average of " << average << endl;
//I'm supposed to write something here
}
return 0;
}
当我尝试 运行 代码时,只有第一行被读取和显示,然后程序结束。更具体地说,输出是
Adara Starr has an average of 94
Adara Starr has an average of 0
如何从 txt 文件中读取下一行?我也做了 while (inData >> average)
代替 inData 条件,但它也做同样的事情减去第二个“Adara Starr 的平均值为 0”
您只读取一个名字,然后循环读取平均值。这与文件的内容不匹配。
使用getline
读取行,然后使用字符串流解析行:
#include <sstream>
#include <string>
#include <iostream>
int main()
{
std::stringstream ss{ R"(Adara Starr 94
David Starr 91
Sophia Starr 94
Maria Starr 91
Danielle DeFino 94)"};
std::string line;
while (std::getline(ss,line)) {
std::stringstream linestream{line};
std::string first;
std::string last;
double avg;
linestream >> first >> last >> avg;
std::cout << first << " " << last << " " << avg << "\n";
}
}
希望它能对您有所帮助,但我想警告您,由于内部 for
循环仅检查数字,如果 average
由浮动组成,您将丢失数据点数。
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
const int MAXNAME = 20;
int main()
{
ifstream inData;
inData.open("grades.txt");
for(string line; getline(inData, line);) {
// parse line
// get number index
size_t num_index = str.length() - 1;
for(; num_index >= 0 && is_digit(str[num_index]); --num_index)
;
string name = line.substr(0, num_index - 1); // skip space char
if(name.length() > MAXNAME)
throw runtime_error("Name could not be more that " + MAXNAME + " length");
float avarageStr = stof(line.substr(num_index + 1));
cout << name << "has an average of " << average << endl;
//I'm supposed to write something here
}
return 0;
}
希望对您有所帮助:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int MAXNAME = 20;
int main() {
ifstream inData("input.txt");
string str = "";
while (!inData.eof()) {//use eof=END OF FILE
//getline(inData, str); use to take the hole row
/*can use string also
string firstName;
string lastName;
string avg;*/
char firstName[MAXNAME];
char lastName[MAXNAME];
float avg;
//you can read from a file like you read from console(cin)
inData >> firstName >> lastName >> avg;//split the row
cout << firstName << " " << lastName << " " << avg<<"\n";
}
inData.close();
return 0;
}
输出: 阿达拉·斯塔尔 94
大卫·斯塔尔 91
索菲亚·斯塔尔 94
玛丽亚·斯塔尔 91
丹妮尔·德菲诺 94