1 = 0 0 97 218
2 = 588 0 97 218
3 = 196 438 97 218
4 = 0 657 97 218
5 = 294 438 97 218
위와 같은 txt 파일이 있습니다.이 파일의 정수는 =없이 읽을 수 있습니까?=를 건너 뛰어 txt 파일에서 정수를 읽는 방법?
1 = 0 0 97 218
2 = 588 0 97 218
3 = 196 438 97 218
4 = 0 657 97 218
5 = 294 438 97 218
위와 같은 txt 파일이 있습니다.이 파일의 정수는 =없이 읽을 수 있습니까?=를 건너 뛰어 txt 파일에서 정수를 읽는 방법?
기본 프레임 워크는 다음과 같습니다. 줄 단위 읽기 및 구문 분석.
for (std::string line; std::getline(std::cin, line);)
{
std::istringstream iss(line);
int n;
char c;
if (!(iss >> n >> c) || c != '=')
{
// parsing error, skipping line
continue;
}
for (int i; iss >> i;)
{
std::cout << n << " = " << i << std::endl; // or whatever;
}
}
는
./myprog < thefile.txt
같은 프로그램으로,
std::cin
를 통해 파이프를 파일을 읽을 수 있습니다.
그는 아무런 노력을하지 않았습니다. writemycodeforme.com –
공백으로 =
분류 패싯 될 또 다른 가능성 :
class my_ctype : public std::ctype<char>
{
mask my_table[table_size];
public:
my_ctype(size_t refs = 0)
: std::ctype<char>(&my_table[0], false, refs)
{
std::copy_n(classic_table(), table_size, my_table);
my_table['='] = (mask)space;
}
};
그런 다음이 전혀 아니었다 =
것처럼 다음 숫자를 읽고,이면을 포함하여 로케일로 스트림을 스며들게 : 그건 그냥 빠른 드의 -
int main() {
std::istringstream input(
"1 = 0 0 97 218\n"
"2 = 588 0 97 218\n"
"3 = 196 438 97 218\n"
"4 = 0 657 97 218\n"
"5 = 294 438 97 218\n"
);
std::locale x(std::locale::classic(), new my_ctype);
input.imbue(x);
std::vector<int> numbers((std::istream_iterator<int>(input)),
std::istream_iterator<int>());
std::cout << "the numbers add up to: "
<< std::accumulate(numbers.begin(), numbers.end(), 0)
<< "\n";
return 0;
}
, 각 행의 첫 번째 행 번호로 보이기 때문에, 모든 숫자 을 추가 아마 매우 현명한 아니다 우리가 정말로 문제를 일으키는 여분의 "물건"없이 숫자를 읽고 있다는 것을 보여주기 위해.
+1 사이트 이것은 쉽게 최고의 솔루션입니다 :-) –
내가 ifstream와 루프를 만들 조금 낮은 기술 :( –
@NominSim 그게 전부지만 읽을 때 = 문제가 발생하지만 난 이해할 수 없다 – droidmachine
@droidmachine을 :.. 코드를보기 하드 – ildjarn