我需要帮助我是新手。我的下面的文字是在一个单独的文件(football.txt
)上创建的。我正在尝试创建一个程序,可以读取该数据并显示它。
Player name: Rusell William
Salaray: $8000000
Age: 20
Team: Seahawks
Position: Quarterback
这就是我所拥有的,
//This program will read the text files into display
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//Declaring Variables
int salary, age;
string firstName, lastName, team, position;
//Delcaring a file.
ifstream infile;
infile.open("football.txt", ios::in);
if (!infile)
{ cout<<"File does not exist";
}
infile.close();
ofstream outfile;
//Declare on display
cout<<"**********Player Summary*********\n";
outfile.open("football.txt", ios::app);
outfile<<"football.txt";
outfile.close();
return 0;
}
没有错误,但也没有显示。
答案 0 :(得分:0)
试试这个示例程序并在其上构建:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void)
{
ifstream infile;
infile.open("football.txt", ios::in);
if (!infile)
{
cout<<"File does not exist";
return EXIT_FAILURE;
}
std::string text_from file;
while (getline(infile, text_from_file))
{
cout << text_from_file << "\n";
}
cout << "Paused. Press enter to continue.\n"
cin.ignore(10000, '\n');
return EXIT_SUCCESS;
}
请注意,正在使用getline
函数从文件中读取文本。