Xcode 6 で C コマンドライン ツール プロジェクトを作成しましたが、1 つの小さなことを除いてすべてがうまく機能します。たとえば、次のコードを考えてみましょう。
#include <string.h>
int main()
{
int len = 10;
char str[len];
strcpy(str, "hello");
printf("%s", str);
}
これは正常にコンパイルされますが、デバッグすると、配列が割り当てられません! 7 行目にブレークポイントを設定し、実行ボタンを押して にカーソルを合わせるとstr
、名前の横に矢印が表示され、展開しても何も表示されません!
私が間違っている場合は訂正してください。しかし、これは私がここに書いている完全に有効な C99 コードだと信じています。GNU99 コンパイラ (デフォルト) と C99 コンパイラの両方を試しましたが、役に立ちませんでした。
MTIA :-)
編集: OK、私はここで数人を混乱させ、おそらくPOしたようです (この質問に対して少なくとも3つの反対票を獲得したため) ので、少し明確にさせてください.
私は実際に Mac OS X Yosemite で libcurl アプリを作成して、HTTP 経由で Web サーバーにファイルをアップロードしています。最後に、ターミナルで「[宛先URL] [ファイルまたはディレクトリ1] [ファイルまたはディレクトリ2] ... [ファイルまたはディレクトリN]」のようなものを入力し、プログラムにそれらを自動的にアップロードさせたいファイルとディレクトリを [destination url] に送信します。入力パスは、CWD に対する相対パスまたは絶対パスにすることができます。
ここに示すように、問題は私のuploadDirectory
関数に存在します:
void uploadDirectory(CURL* hnd, struct curl_httppost* post,
struct curl_httppost* postEnd, char* path, const char* url)
{
DIR* dir = opendir(path);
if (dir == NULL)
{
printf("\nopendir failed on path \"%s\"", path);
perror(NULL);
}
else
{
struct dirent* file;
while ((file = readdir(dir)) != NULL)
{
// skip the current directory and parent directory files
if (!strcmp(file->d_name, ".") ||
!strcmp(file->d_name, ".."))
continue;
if (file->d_type == DT_REG)
{
// file is an actual file; upload it
// this is the offending code
char filePath[strlen(path) + strlen(file->d_name) + 2];
strcpy(filePath, path);
strcat(filePath, "/");
strcat(filePath, file->d_name);
int res = uploadFile(hnd, post, postEnd, filePath, url);
printf("%d", res);
}
if (file->d_type == DT_DIR)
{
// file is a directory; recurse over it
// this section is omitted for brevity
}
}
closedir(dir);
}
}
問題を解決するために巨大な定数サイズを定義できることは知っていますfilePath
が、大きすぎるとはどのくらいですか? OS X のファイル パスの最大長は? 等々…なので、丁度いいサイズにしたいと思います。
この投稿の極端な長さに勇敢に立ち向かい、ここにたどり着いたのなら、辛抱強く待ってくれてありがとう! 最初は問題をできるだけ簡潔に説明しようとしましたが、明らかに混乱を招くだけだったので、申し訳ありません:-)