這裏編程noob。請檢查下面的代碼...這是一個給定詞彙量的強力組合生成器。這不是編譯。你能指出錯誤(S)嗎?如果可能的話,請告訴我如何在這種情況下爲文件和終端輸出編寫一個單獨的函數。謝謝你的時間!如何從遞歸函數內寫入文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static const char alphabet[] = "abcd";//vocabulary
static const int alphabetSize = sizeof(alphabet) - 1;
void bruteImpl(char* str, int index, int maxDepth)//main recursive function
{
for (int i = 0; i < alphabetSize; ++i)
{
str[index] = alphabet[i];
if (index == maxDepth - 1)
{
printf("%s\n", str);
fprintf(fp, "%s\n", str);// error
}
else bruteImpl(str, index + 1, maxDepth);
}
}
void bruteSequential(int maxLen)
{
char* buf = malloc(maxLen + 1);
for (int i = 1; i <= maxLen; ++i)
{
memset(buf, 0, maxLen + 1);
bruteImpl(buf, 0, i);
}
free(buf);
}
int main(void)
{
FILE *fp;//warning
fp = fopen("output", "w");//warning
bruteSequential(5);
return 0;
}
請告訴我們你得到正是你展示的代碼片段的確切的錯誤消息。 – 2014-11-09 05:54:31
爲什麼不嘗試使用編譯器進行編譯?如果您有特定的問題,請提問。 – eyalm 2014-11-09 05:54:52
甚至不要在程序內部做文件I/O。只需寫入標準輸出並讓shell處理重定向。停止將文件輸出視爲與終端輸出不同。 – 2014-11-09 05:55:51