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

时间:2016-11-04 23:05:27

标签: c file debugging crash runtime-error

我知道我不应该在堆栈交换中请求bug解决方案,但我是C的新手,我制作了一个程序,可以展开代码并在.c文件中输出代码。问题是,当我想打印出一些代码行时,codeOutput的fputs函数会使程序崩溃(我已经调试了它)。我已经尝试将.Output文件从.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;
}

2 个答案:

答案 0 :(得分:2)

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]);
    }

答案 1 :(得分:0)

您应该检查fopen()函数的结果以确保它不是NULL:

codeOutput[i] = fopen(buf, "w+");
if (codeOutput[i] == NULL) {
    perror(buf); /* Assuming perror is supported and you
                    #include <errno.h> */
    exit();
}