gpt4 book ai didi

c - 为什么我的程序在写入文件时崩溃?

转载 作者:太空宇宙 更新时间:2023-11-04 02:33:31 25 4
gpt4 key购买 nike

我知道我真的不应该在 Stack exchange 中寻求错误解决方案,但我是 C 的新手,我制作了一个程序来展开代码并将代码打印在 .c 文件中。问题是,当我想打印出一些代码行时,codeOutput 的 fputs 函数使程序崩溃(到目前为止我已经调试过了)。我试过将 codeOutput 文件从 .c 更改为 .txt,但它没有任何改变。我知道这段代码摘录并不是真正可重复的,但我确信我在编写代码时犯了一个愚蠢的错误。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

int main()
{
char buf[500];

int numberOfSamples = 5;
int numberOfElements = 10;
int currentTest = 1;

FILE *arrayAssignmentReader[numberOfSamples];
FILE *codeSnippetReader[numberOfSamples];
FILE *arrayPrintReader[numberOfSamples];
FILE *codeOutput[numberOfSamples];

for (int i = 0; i < numberOfSamples; i++) {
sprintf(buf, "C:/Users/Erlandas/Desktop/Research/C/TestNo%i/ProgramNo1/SampleNo%i/ArrayAssignment.txt", currentTest, i);
arrayAssignmentReader[i] = fopen(buf, "r+");
sprintf(buf, "C:/Users/Erlandas/Desktop/Research/C/TestNo%i/ProgramNo2/SampleNo%i/codeSnippet.txt", currentTest, i);
codeSnippetReader[i] = fopen(buf, "r+");
sprintf(buf, "C:/Users/Erlandas/Desktop/Research/C/TestNo%i/ProgramNo1/SampleNo%i/ProgramNo1PrintArray.txt", currentTest, i);
arrayPrintReader[i] = fopen(buf, "r+");

sprintf(buf, "C:/Users/Erlandas/Desktop/Research/C/TestNo%i/codeForNo3/codeNo%i/", currentTest, i);
struct stat st = {0};
if (stat(buf, &st) == -1) {
mkdir(buf);
}

sprintf(buf, "C:/Users/Erlandas/Desktop/Research/C/TestNo%i/codeForNo3/CodeNo%i/CodeNo%i.c", currentTest, i, i);
codeOutput[i] = fopen(buf, "w+");
}

for (int currentSample = 0; currentSample < numberOfSamples; currentSample++) {

sprintf(buf, "#include <stdio.h>\n#include <time.h>\n#include <stdlib.h>\n#include <sys/types.h>\n#include <sys/stat.h>\n#include <math.h>\n#include <unistd.h>\n#define BILLION 1000000000L\nint main()\n{\nchar buf[500];\nint numberOfElements = %i;\nint currentTest = %i;\nint randomArray[numberOfElements];\nint minIndex;\nint minValue;\nstruct timespec requestStart;\nstruct timespec requestEnd;\nlong int recordStartTime;\nlong int recordEndTime;\nlong int elapsedTime;\nFILE *arrangedArray;\nFILE *stopwatch;\nsprintf(buf,\"C:/Users/Erlandas/Desktop/Research/C/TestNo%%i/ProgramNo3/ProgramNo3Stopwatch.txt\", currentTest);\nstopwatch = fopen(buf, \"a+\");\nstruct stat st = {0};\nsprintf(buf, \"C:/Users/Erlandas/Desktop/Research/C/TestNo%%i/ProgramNo3/\", currentTest);\nif (stat(buf, &st) == -1)\n{\nsprintf(buf, \"C:/Users/Erlandas/Desktop/Research/C/TestNo%%i/ProgramNo3/\", currentTest);\nmkdir(buf);\n}\nsprintf(buf, \"C:/Users/Erlandas/Desktop/Research/C/TestNo%%i/CodeForNo3/SampleNo%i/ArrangedArray.txt\", currentTest, currentSample);\narrangedArray = fopen(buf, \"w+\");\n", numberOfElements, currentTest, currentSample);
fputs(buf, codeOutput[currentSample]);

while (buf[0] != EOF){
fgets(buf, 500, arrayAssignmentReader[currentSample]);
fputs(buf, codeOutput[currAentSample]);
fputs("\n", codeOutput[currentSample]);
}

sprintf(buf, "clock_gettime(CLOCK_MONOTONIC, &requestStart);\nrecordStartTime = requestStart.tv_nsec + requestStart.tv_sec * BILLION;\n");
fputs(buf, codeOutput[currentSample]);

while (buf[0] != EOF) {
fgets(buf, 500, codeSnippetReader[currentSample]);
fputs(buf, codeOutput[currentSample]);
fputs("\n", codeOutput[currentSample]);
}

sprintf(buf, "clock_gettime(CLOCK_MONOTONIC, &requestEnd);\nrecordEndTime = (requestEnd.tv_nsec + requestEnd.tv_sec * BILLION);\nelapsedTime = recordEndTime - recordStartTime;\nsprintf(buf, \"%%li\\n\", elapsedTime);\nfputs(buf, stopwatch);\n");
fputs(buf, codeOutput[currentSample]);

while (buf[0] != EOF) {
fgets(buf, 500, arrayPrintReader[currentSample]);
fputs(buf, codeOutput[currentSample]);
fputs("\n", codeOutput[currentSample]);
}

sprintf(buf, "\nfclose(output);\nfclose(arrayOutput);\nfclose(arrangedArray);\n}\nfclose(stopwatch);\nreturn 0;\n}\n");
fputs(buf, codeOutput[currentSample]);
}

return 0;
}

最佳答案

buf对于这个语句来说不够大:

sprintf(buf, "#include <stdio.h>\n#include <time.h>\n#include <stdlib.h>\n#include <sys/types.h>\n#include <sys/stat.h>\n#include <math.h>\n#include <unistd.h>\n#define BILLION 1000000000L\nint main()\n{\nchar buf[500];\nint numberOfElements = %i;\nint currentTest = %i;\nint randomArray[numberOfElements];\nint minIndex;\nint minValue;\nstruct timespec requestStart;\nstruct timespec requestEnd;\nlong int recordStartTime;\nlong int recordEndTime;\nlong int elapsedTime;\nFILE *arrangedArray;\nFILE *stopwatch;\nsprintf(buf,\"C:/Users/Erlandas/Desktop/Research/C/TestNo%%i/ProgramNo3/ProgramNo3Stopwatch.txt\", currentTest);\nstopwatch = fopen(buf, \"a+\");\nstruct stat st = {0};\nsprintf(buf, \"C:/Users/Erlandas/Desktop/Research/C/TestNo%%i/ProgramNo3/\", currentTest);\nif (stat(buf, &st) == -1)\n{\nsprintf(buf, \"C:/Users/Erlandas/Desktop/Research/C/TestNo%%i/ProgramNo3/\", currentTest);\nmkdir(buf);\n}\nsprintf(buf, \"C:/Users/Erlandas/Desktop/Research/C/TestNo%%i/CodeForNo3/SampleNo%i/ArrangedArray.txt\", currentTest, currentSample);\narrangedArray = fopen(buf, \"w+\");\n", numberOfElements, currentTest, currentSample);

你应该制作buf大得多并使用snprintf()以避免缓冲区溢出。

您应该将所有这些字符串分解成适合规则行的片段:

sprintf(buf, "clock_gettime(CLOCK_MONOTONIC, &requestEnd);\n"
"recordEndTime = (requestEnd.tv_nsec + requestEnd.tv_sec * BILLION);\n"
"elapsedTime = recordEndTime - recordStartTime;\n"
"sprintf(buf, \"%%li\\n\", elapsedTime);\n"
"fputs(buf, stopwatch);\n");
fputs(buf, codeOutput[currentSample]);

但请注意,您不需要 sprintf()对于其中的一些完全没有:因为你没有替换任何变量,所以你可以调用 fputs直接使用字符串。

此外,您测试文件结尾的方式不正确:while (buf[0] != EOF)无法测试是否已到达文件末尾,检查 fgets() 的返回值是执行此操作的正确方法:

    while (fgets(buf, 500, arrayAssignmentReader[currentSample]) != NULL) {
fputs(buf, codeOutput[currentSample]);
fputs("\n", codeOutput[currentSample]);
}

关于c - 为什么我的程序在写入文件时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40432898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com