Skip to Content
21st Century C, 2nd Edition
book

21st Century C, 2nd Edition

by Ben Klemens
May 2025
Intermediate to advanced
408 pages
5h 55m
Japanese
O'Reilly Media, Inc.
Content preview from 21st Century C, 2nd Edition

第9章. より簡単なテキスト・ハンドリング

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

最後には、その言葉がセメントを壊すと信じている。

プッシー・ライオット、2012年8月8日の声明でアレクサンドル・ソルジェニーツィンの言葉を引用している。

文字列は不定長の配列であり、自動的に確保された配列(スタック上に確保されたもの)はサイズを変更することができない。幸運なことに、私たちよりも前に多くの人がすでにこの問題に直面し、少なくとも部分的な解決策をプロデューサしている。一握りのC標準関数とPOSIX標準関数があれば、文字列作成に必要な多くのことを十分に処理できる。

また、C言語が設計されたのは1970年代で、英語以外の言語が発明される前だった。繰り返しになるが、適切な関数があれば(そして言語がどのようにエンコーディングされるかを正しく理解していれば)、C言語が元々英語にフォーカスしていたことは本当の問題ではない。

asprintfで文字列の扱いを楽にする

asprintf 関数は、必要な文字列スペースを確保し、文字列を埋める。つまり、文字列の確保について心配する必要がなくなるということだ。

asprintf はC言語標準の一部ではないが、GNUまたはBSD標準ライブラリのあるシステムで利用できるため、幅広いユーザをカバーしている。さらに、GNU Libibertyライブラリは、 のバージョンを提供し、自分のコードベースにカット&ペーストするか、リンカに フラグを付けてライブラリから呼び出すことができる。Libibertyは、Windows版MSYSのように、ネイティブの を持たないシステムにも同梱されている。また、 からカット・アンド・ペーストするのが面倒な場合は、標準の 関数を使った簡単な再実装を紹介しよう。asprintf -liberty asprintf libiberty vsnprintf

なぜなら、まず埋めようとしている文字列の長さを求め、スペースを確保し、そして実際にそのスペースに書き込まなければならなかったからだ。ヌルターミネーター用の余分なスロットを忘れないでほしい!

例9-1は、外部ユーティリティを実行するためにCのsystem コマンドを使用する目的で、文字列をセットアップする手間のかかる方法を示している。テーマ別に適切なユーティリティ、strings は、印刷可能なプレーン・テキストをバイナリ検索する。get_strings 関数はargv[0] 、プログラム自体の名前を受け取るので、プログラムは文字列を検索する。これは、デモ・コードに求めることのできるすべてである。

例9-1. 文字列をセットアップする面倒な方法(sadstrings.c)
#include <stdio.h>
#include <string.h> //strlen
#include <stdlib.h> //malloc, free, system

void get_strings(char const *in){
    char *cmd;
    int len = strlen("strings ") + strlen(in) + 1;   1
    cmd = malloc ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learn C Programming - Second Edition

Learn C Programming - Second Edition

Jeff Szuhay
Tiny C Projects

Tiny C Projects

Daniel Gookin

Publisher Resources

ISBN: 9798341650848