주어진 파일의 모든 줄을 읽으려고합니다.
어떤 이유로 인해 std::getline
이 예상대로 작동하지 않습니다.
MAIN.CPP :파일에서 읽을 수 없습니다, 이상한 동작
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::string filePath = "../init.txt";
std::ifstream inputFile(filePath);
std::string str;
int i = 0;
while (std::getline(inputFile,str)) {
std::cout << str << std::endl;
i++;
}
std::cout << i << std::endl;
inputFile.close();
return 0;
}
../init.txt :
Game
battlefieldSize,100,200
players,2
soldiers,3
p1,human
normal,[2 3],M16
paramedic,[10 31]
sniper,[5 12],UZI
p2,computer,0
normal,[90 112],Missile
sniper,[90 113],M16
normal,[65 100],M16
Objects
weapon,M16,[5 5]
Armor,BodyArmor,0.8,[1 2]
weapon,Missile,[15 115]
solid,Tree,4,4,[20 20]
당신이 볼 수 있듯이, 나는 그것이 변수 i
로, 루프를 입력 얼마나 많은 시간을 알고 싶어. 출력은 다음과 같습니다.
solid,Tree,4,4,[20 20]
1
왜 그런가요?
내 바탕 화면에 복제 할 수 없습니다. –
제 친구들 중 한 명은'std :: getline'과 함께 이런 종류의 행동을 실제로했습니다. 그의 코드도 완벽하게 괜찮 았지만 이전 버전의 IDE로 인해 컴파일이 잘못되었습니다. 당신, OP에 대해 모르겠지만, 어떤 경우에는 Code :: Blocks에 대한 작업을하고 원래 개발자와 다른 소스에서 다운로드 한 경우 다시 다운로드하여 다시 설치하십시오. – Fureeish
복제 할 수 없습니다. 'std :: string filePath = "../ init.txt"에 문제가 있습니까? 다른 디렉토리 인'init.txt'를 읽고 계신지 모르십니까? – j4nu5