저는 C++을 처음 사용하고 있으며 텍스트 파일의 정수를 읽고 각 변수를 한 줄씩 다른 변수에 저장하는 코드를 작성하려고합니다. 구문 및 코드 정렬 방법에 문제가 있습니다. 기본적으로 텍스트 파일은 행당 4 개의 정수를 포함하며, 값은 아래에 표시된 것처럼 클래스 행성의 좌표 및 ID로 읽습니다. 나는 코드 beloe가 불완전하다는 것을 알고있다. 그러나 이것은 처음으로 C++로 프로그래밍하고 도움이 필요하다. 제발 당신이 행성이나 아무것도 사용하여 이것을 설명 할 필요가 없습니다. 난 단지 일반적인 이해가 필요해.텍스트 파일 읽기 C++
#include <iostream>
#include <fstream>
using namespace std;
class planet{
public :
float x_coordinates;
float y_coordinates;
float z_coordinates;
int id;
};
planet*generate_planet(istream &fin)
{
planet *x= new planet;
fin >> x->id >> x->x_coordinates >> x->y_coordinates >> x->z_coordinates;
return (x);
}
void report_planet(planet &p)
{
cout<<"planet "<<p.id<<" has coordinates (" << p.x_coordinates<<","<< p.y_coordinates<<","<< p.z_coordinates<<")"<<endl;
}
int main()
{
planet p;
planet *x;
ifstream fin("route.txt");
generate_planet(fin);
report_planet(*x);
return 0;
}
일부 문제 : generate_planet에서 반환 된 행성 *을 report_planet()에 저장하지 마십시오. 이 기억이 새 었어. 그런 다음 generate_planet()이 반환하는 것이 아닌 ofstream을 report_planet()에 전달합니다. –