-1
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class Data
{
public:
char name[20];
long long int ph;
Data()
{
strcpy(name," ");
for(int i=0;i<10;i++)
ph=0;
}
void getdata()
{
cin>>name;
cin>>ph;
}
};
int main()
{
int n;
fstream file;
file.open("test1.txt",ios::app|ios::out);
if(!file)
{
cout<<"file open error"<<endl;
return 0;}
cout<<"number of data you want to enter"<<endl;
cin>>n;
char a[1000];
//Data temp;
Data d[n];
for(int i=0;i<n;i++)
{
d[i].getdata();
file<<d[i].name<<" ";
file<<d[i].ph<<endl;
}
file.close();
file.open("test1.txt",ios::in);
file.seekg(0);
file>>a;
while(!file.eof())
{
cout<<a<<endl;
file>>a;
//cout<<a<<endl;
}
cout<<"Enter the index"<<endl;
int in;
cin>>in;
int pos=(in-1)*(sizeof(d[0]));
file.seekg(pos);
file>>a;
//cout<<a<<endl;
file.read((char *)(&d[pos]),sizeof(d[0]));
cout<<a<<endl;
file.close();
system("pause");
return 0;
}
에서 읽는 동안 특정 출력을 얻을 방법 : 입력 할 데이터의
수
2
ABC (88)
XYZ 99
인덱스를 입력 :
를 xyz 99 //이 출력을 얻어야합니다.
99 //이 출력을 얻고 있습니다
이 코드는 해당 파일에있는 문자 배열을 읽지 않습니다.나는 다음과 같은 데이터를 입력하고 경우 파일
이 태그를 C++로 태그 했으므로 C 스타일 문자열을'std :: string'으로 대체하십시오. –