私はファイル(char、char、fgetc()を使用して)から行を読み込んでいますが、すべてのフィールド(firstname、lastname、...)は;
。私が今したいことは、char**
を作成し、すべての文字を追加して;
を\0
に置き換えて、すべてのフィールドのリストを効果的に取得するようにします。c char *をcharに分割する**
実際には可能でしょうか? char **を作成すると、 char ** buf = malloc(80)
私は1次元配列のように扱うことができますか? mallocによって返されたメモリが連続していれば?
EDIT
SRYは、\0
、ボット\n
によって;
に代わるもの。
EDIT 2
このコードは、私が(ささいなことを明確にすること)するつもり何を証明する必要があります
int length = 80; // initial length char *buf = malloc(length); int num_chars = 0;
// handle malloc returning NULL
// suppose we already have a FILE pointer fp for (int ch = fgetc(fp); ch != EOF && ferror(fp) == 0; ch = fgetc(fp)) {
if (length == size) { // expand array if necessary
length += 80;
buf = realloc(buf, length);
// handle realloc failure
}
if (ch == ';') {
buf[num_chars] = '\0'; // replace delimiter by null-terminator
} else {
buf[num_chars] = ch; // else put char in buf
} }
// resize the buffer to chars read buf
= realloc(buf, num_chars);
// now comes the part where I'm quite unsure if it works/is possible
char **list = (char **)buf; // and now I should be able to handle it like a list of strings?