Answer the question
In order to leave comments, you need to log in
How to read data from a C++ text file?
While studying how to work with files in C ++, I came across an interesting problem: there are two categories of football teams: weak and strong, you need to display three from strong and three from weak, respectively. The condition said that in the first case, you need to enter the data into a text file, and in the second, read the already entered data from the same file. But there was a snag with the second condition: how exactly is the data read from the file? Thanks in advance for your response.
Here is the code itself:
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
struct gamers
{
char Name[30];
int coin;
int status;
};
int main()
{
setlocale(LC_ALL, "Russian");
int sap;
cout << "Введите количество футбольных команд : ";
cin >> sap;
gamers *pas = new gamers[sap];
for(int i(0); i < sap; i++)
{
cin.sync();
cout << "Введите название команды:";
cin.getline(pas[i].Name,30);
cout << "Введите количество очков команды " << pas[i].Name << ":";
cin >> pas[i].coin;
cout << "К какой группе относится команда " << pas[i].Name << " (1-сильная,2-слабая):";
cin >> pas[i].status;
cin.get();
system("cls");
}
ofstream outfile;
outfile.open("cppstudio.txt");
for(int i(0); i < sap; i++)
{
outfile << pas[i].Name << '\t' << pas[i].coin << '\t' << pas[i].status << '\n';
}
outfile.close();
delete [] pas;
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
#include<iostream>
#include<fstream>
int main() {
std::ifstream textFile ("MyFile.txt");
std::string buffer;
while ( std::getline( textFile, buffer ) ) {
std::cout<< buffer <<std::endl;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question