C ではこれは簡単ではありません。私はすべてを見ましたが、何も見つかりませんでした。
「Sample:」で始まる部分文字列を C 文字列で検索し、その後の最初の改行までのすべての文字で構成される新しい C 文字列を作成する必要があります。
C++ では、これをハートビートで実行できます。経験豊富な C プログラマーが道を教えてくれますか?
手で書き出すことでこれを行うことができると確信していますが、確かに役立つ組み込み関数がいくつかありますか?
strstr()
とstrndup()
あなたの友達です。終わったら出力を解放することを忘れないでください:
const char *input = "Hello Sample: This is a test\nTest";
const char *start = strstr(input, "Sample: ");
if (!start)
{
// report error here
}
const char *end = strstr(start, "\n");
if (!end)
{
// you have two options here.
// #1: use pure strdup on start and you have your output
// #2: make this an error, and report it to the user.
}
int length = end - start;
char *output = strndup(start, length);
printf("%s", output); // Prints "Sample: This is a test"
free(output);
適切な API 呼び出しを知っていれば、それほど難しくありません。