0
텍스트 파일 (sortarraysin.txt)에서 값을 읽고 해당 값을 배열에 저장하는 프로그램이 있습니다. 그러나 콘솔에 배열을 인쇄하려고하면 출력에 텍스트 파일의 숫자가 표시되지 않습니다. 내 텍스트 파일과 프로그램은 아래와 같습니다.텍스트 파일에서 읽기 및 배열에 저장 C++
텍스트 파일 :
45 59 302 48 51 3 1 23
프로그램 :
int array[8];
int i = 0;
string inFileName, getcontent;
cout << "Enter input file name -> ";
cin >> inFileName;
fstream inFileStr(inFileName.c_str(), ios::in);
if(inFileStr.fail()){
cerr << "Unable to open " << inFileName << endl;
exit(-1);
}
if(inFileStr.is_open()){
while(!inFileStr.eof()){
getline(inFileStr, getcontent);
cout << getcontent << endl;
array[i++] << atoi(getcontent.c_str());
for(i=0;i<=8;i++){
cout << array[i] << " ";
}
}
}
출력 :
Enter input file name -> sortarraysin.txt
45 59 302 48 51 3 1 23
-2145069216 1 -13232 0 -2145069216 1 -12816 0 -13136
왜 배열 값 대신 텍스트 파일에서 값이 숫자를 인쇄?
그래서 숫자를 숫자로 읽지 않으시겠습니까? 이것을 읽을 수도 있습니다 : https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong 모든 전화 번호가 같은 줄에 있고 전체를 읽으면 라인 당신이 번호로 변환하기 전에 그 라인을 분할하는 무언가를해야합니다. 하지만 실제로 숫자를 읽으십시오. –
@RetiredNinja 어떻게해야합니까? –
'int num; while (file >> num) {숫자로 무엇인가하십시오}' –