줄 당 명령이 들어있는 간단한 텍스트 파일이 있습니다. 예를 들어알 수없는 크기의 데이터를 구문 분석하는 방법
A 1 1
B 2 1 A
C 3 1 A
D 4 1 B C
기본 구문은 문자, 민, 민, 문자 (들)입니다
난 그냥 데이터를 분석하기 위해 호출하는 방법과 주어진에 구문 분석하는 방법해야 할 기능을 모르는 통사론. 나는 그것을 할 수있는 많은 방법이있는 것처럼 느껴집니다.
줄 당 명령이 들어있는 간단한 텍스트 파일이 있습니다. 예를 들어알 수없는 크기의 데이터를 구문 분석하는 방법
A 1 1
B 2 1 A
C 3 1 A
D 4 1 B C
기본 구문은 문자, 민, 민, 문자 (들)입니다
난 그냥 데이터를 분석하기 위해 호출하는 방법과 주어진에 구문 분석하는 방법해야 할 기능을 모르는 통사론. 나는 그것을 할 수있는 많은 방법이있는 것처럼 느껴집니다.
다음 C++ 예제 파일에서 하나의 캐릭터 라인의 제어 끝 읽을 수있는 가능한 방법 중 하나를 보여줍니다 여기에 istringstream
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
int main(void)
{
ifstream inpFile("test.txt");
string str;
char c;
while (inpFile.good()) {
// read line from file
getline(inpFile, str);
// make string stream for reading small pieces of data
istringstream is(str);
// read data ingnoring spaces
do
{
is >> c; // read a single character
if (!is.eof()) // after successful reading
cout << c << " "; // output this character
} while (is.good()); // control the stream state
cout << "[End of line]" << endl;
}
cout << "[End of file]" << endl;
}
이 getline
에 의해 가지고있다 한 줄을 처리하는 데 사용됩니다.
if (!is.eof()) // after successful reading
{
// analyze the content
if (isdigit(c))
cout << (c - '0') << "(number) "; // output as a digit
else
cout << c << "(char) "; // output as a non-number
}
참고 : 파일이 아닌 하나의 문자/숫자 만 숫자와 단어를 포함 할 경우, c
의 유형해야 c
에 is >> c
값으로 문자를 읽은 후
는 예를 들어, 콘텐츠를 확인할 수 있습니다 적절한 경우 (예 : string
)
C 헤더에서''std :: getline''과''getline'' 두가지가 있다는 사실은''네임 스페이스 std;를 사용하는 것이 보통 나쁜 스타일로 간주되는 좋은 예입니다. –
'while (std :: getline (inpFile, str))'과'while (is >> is c)'를 쓰는 게 어떨까요? 왜'char c'를 바깥쪽에 노출 시키죠? – Danh
@JonasWielicki 이런 간단한 예제를 위해'namespace std;를 사용하는 것은 꽤 가능합니다. John은 그가 할 수있는 것처럼 이것을 사용하지 않고 다시 쓸 수 있습니다. – VolAnd
C++의 경우 전체 행을 읽고 스트림을 만든 다음 해당 스트림에서 >>
으로 읽습니다.
예 :
std::ifstream file(filename);
std::string line;
while (file.getline(line))
{
std::istringstream in(line);
char letter;
int number1;
int number2;
std::vector<char> letters;
if (in >> letter >> number1 >> number2)
{
char letter2;
while (in >> letter2)
{
letters.push_back(letter2);
}
}
}
이 선 판독 C의 예는 다음 (32보다 큰 코드) 출력 판독 자 처음부터 (포인터를 사용하여) 이동 :
#include <stdio.h>
#include <ctype.h>
#define MAX_LINE_LEN 80
int main(void)
{
FILE * inpFile = fopen("test.txt", "r");
char buf[MAX_LINE_LEN];
char *p;
while (!feof(inpFile))
{
// read a line from file
if (fgets(buf, MAX_LINE_LEN, inpFile) != NULL)
{
p = buf; // start from the beginning of line
// reading data from string till the end
while (*p != '\n' && *p != '\0')
{
// skip spaces
while (isspace(*p) && *p != '\n') p++;
if (*p > 32)
{
// output character
printf("%c ", *p);
// move to next
p++;
}
}
}
printf("[End of line]\n");
}
printf("[End of file]\n");
return 0;
}
줄에서 숫자와 단어를 추출하려면 다음과 같이 할 수 있습니다.
// reading data from string till the end
while (*p != '\n' && *p != '\0')
{
// skip spaces
while (isspace(*p) && *p != '\n') p++;
if (*p > 32)
{
int num;
char word[MAX_LINE_LEN];
// trying to read number
if (sscanf(p, "%i", &num))
{
printf("%i(number) ", num);
}
else // read string
{
sscanf(p, "%s", word);
printf("%s(string) ", word);
}
// move to next space in the simplest way
while (*p > 32) p++;
}
}
네, 여러 가지 방법이 있습니다. 선호하는 방법은 코드를 C 또는 C++로 작성하는지에 따라 달라집니다. 따라서 언어를 고르고 이미 시도한 것을 말해 줄 수 있습니다. – user3386109
@ user3386109 Im getline, srtok을 사용하는 C에 익숙하다.하지만 C++을 사용할 수 있으므로 익숙해지고 싶다. 그래서 C++로 할 방법을 찾고 싶습니다. – John
C++에서는'getline' (하지만 C에서 사용하는 POSIX와 다른 것)과'std :: string'을 사용합니다. 문자열을 조각으로 잘라내는 방법에 대한 끝없는 옵션이 있습니다. –