1
저는 C++의 초보자입니다.텍스트 파일에서 배열로 읽기
텍스트 파일 (최대 1024 단어)을 배열로 읽고 모든 단일 문자 단어를 무시해야합니다. 당신은 기호, 특수 문자를 피하기 위해 하나의 문자 인 단어를 버리도록 도와 줄 수 있습니까?
이 내 코드입니다 : 당신이 std::vector
에 names
을 변경하는 경우
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
const int SIZE = 1024;
void showArray(string names[], int SIZE){
cout << "Unsorted words: " << endl;
for (int i = 0; i < SIZE; i++){
cout << names[i] << " ";
cout << endl;
}
cout << endl;
}
int main()
{
int count = 0;
string names[SIZE];
// Ask the user to input the file name
cout << "Please enter the file name: ";
string fileName;
getline(cin, fileName);
ifstream inputFile;
inputFile.open(fileName);
// If the file name cannot open
if (!inputFile){
cout << "ERROR opening file!" << endl;
exit(1);
}
// sort the text file into array
while (count < SIZE)
{
inputFile >> names[count];
if (names[count].length() == 1);
else
{
count++;
}
}
showArray(names, SIZE); // This function will show the array on screen
system("PAUSE");
return 0;
}
불행히도이 코드에는 vector를 사용할 수 없습니다. 다른 제안? –