#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct game
{
int num;
int year;
float arr[3];
};
struct gj
{
int a;
game dz;
};
int count_lines(char *filename) {
int n = 0;
ifstream ReadFile;
string tmp;
ReadFile.open(filename,ios::in);
if(ReadFile.fail())
{
return 0;
}
else
{
while (getline(ReadFile, tmp, '\n'))
{
n++;
}
ReadFile.close();
cout<<"the rows of txt = " << n<<endl;
return n;
}
}
int main()
{
char filename[] = "g1.txt";
fstream file;
file.open(filename,ios::in);
ifstream myfile(filename);
game g2;
gj gj1;
// if(!myfile.is_open())
// {
// cout << "can not open this file" << endl;
// return 0;
// }
//int row = count_lines("g1.txt");
if (file.fail())
{
cout<< "file not exit"<<endl;
return 0;
}
string s ;
int line = 4;
int i = 0;
// myfile >> gj1.dz.num >>gj1.dz.year >>gj1.dz.arr[0]>>gj1.dz.arr[1]>>gj1.dz.arr[2];
// cout << "value = " << gj1.dz.year;
while (getline(myfile,s)&&i < line-2)
{
i++;
}
myfile >> gj1.dz.num >>gj1.dz.year >>gj1.dz.arr[0]>>gj1.dz.arr[1]>>gj1.dz.arr[2];
//myfile >> g2.num >> g2.year >> g2.arr[0] >>g2.arr[1]>>g2.arr[2];
cout << "value = " << gj1.dz.year;
cout <<" value g2 = " <<gj1.dz.arr[0]<<endl;
int years = gj1.dz.year;
cout << years;
myfile.close();
return 0;
}
c++ 从txt文件读取内容,读取某一行数据
最新推荐文章于 2023-03-16 15:49:44 发布