이 코드를 유지하고 싶습니다하지만 지금은 내 while 루프에서 파일을 읽을 때 방법이 있는지 궁금합니다 내가 그 루프간단한 텍스트 파일에서 읽기 및 선행 공백 및 빈 줄이 제거 된 다른 텍스트 파일에 쓰기
내가 공백 제거에 문제의 톤을 데 내에서 공백을 제거 할 경우이 나를 위해 매우 어려웠다 그래서 내가 할 수있는, 내 프로그램에 파일 읽기에 큰 이해가없는 아무도 내게 말하지 말아요 내 실수를 저질렀습니까?
#include <iostream>
#include <cassert>
#include <string>
#include <fstream>
#include <cstdio>
using namespace std;
int main (void)
{
int i=0;
int current=0;
int len;
int ch;
string s1;
string s2;
ifstream fileIn;
cout << "Enter name of file: ";
cin >> s1;
fileIn.open(s1.data());
assert(fileIn.is_open());
while (!(fileIn.eof()))
{ ch=fileIn.get();
s1.insert(i,1,ch);
s1.end();
i++;}
cout << s1;
len=s1.length();
cout << len;
while (current < len-1)
{
if (!(s1[current] == ' ' && s1[current + 1] == ' ') &&
!(s1[current] == '\n' && s1[current + 1] == '\n')
)
{
s2.append(s1[current]);
}
current++;
}
return 0;
}
먼저 자신의 기능 대신 [std :: isblank'] (http://en.cppreference.com/w/cpp/string/byte/isblank)를 사용해야합니다. 또한 C++ 표준 라이브러리에는 [알고리즘] (http://en.cppreference.com/w/cpp/algorithm)과 같은 멋진 세트가 있습니다. 예를 들어 ['std :: remove_if'] (http : // en.cppreference.com/w/cpp/algorithm/remove). –
링크를 클릭하십시오. –
주된 문제는 한 번에 한 줄씩 파일을 읽지 않는다는 것입니다.선행 공백 (및 빈 줄)을 제거하려는 경우 필수적입니다. 대신 한 번에 한 글자를 읽는 것입니다. – john