0

ファイルを文字列に読み取りたいと思います。

コンパイルできるが実行できない次のコードがあります。

私の考えは、while ループを使用して、ファイル内のすべての文字を EOF まで文字列の末尾に追加することです。

char string[50000];    

FILE *file;
file = fopen("filename.txt", "r");

char buffer;


while((buffer=fgetc(file)) != EOF)
{
    strcat(string, buffer);
}
4

3 に答える 3

1

最初に設定string[0]してみてください\0。また、null で終了するbuffer必要があります。char *

ページからman:

 NAME
 strcat, strncat -- concatenate strings

 LIBRARY
 Standard C Library (libc, -lc)

 SYNOPSIS
 #include <string.h>

 char *
 strcat(char *restrict s1, const char *restrict s2);

 char *
 strncat(char *restrict s1, const char *restrict s2, size_t n);

 DESCRIPTION
 The strcat() and strncat() functions append a copy of the null-terminated
 string s2 to the end of the null-terminated string s1, then add a termi-
 nating `\0'.  The string s1 must have sufficient space to hold the
 result.
于 2013-12-01T15:26:12.657 に答える
0

読み込むバッファは配列でなければなりません。

char buffer[50000];
于 2013-12-01T15:26:14.347 に答える