为什么我的程序在写入文件时崩溃? [英] Why does my program crash upon writing to a file?

查看:60
本文介绍了为什么我的程序在写入文件时崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know I really shouldn't ask for bug solutions here in Stack exchange, but I'm new to C and I made a program that unrolls code and prints the code out in a .c file. The problem is that when I want to print out some lines of code, the fputs function to codeOutput crashes the program(I've debugged it this far). I've tried changing the codeOutput file from .c to .txt, but it hasn't changed anything. I know this code excerpt is not really repeatable, but I'm sure I just made a dumb mistake in writing the code. heres my code:

#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#定义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; \ nlongint recordEndTime; \ nlong int经过时间; \ nFILE * arrangedArray; \ nFILE *秒表; \ 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()以避免缓冲区溢出.

You should make buf much larger and use snprintf() to avoid buffer overflows.

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

You should break all these strings into fragments that fit on regular lines:

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 直接输入字符串.

But note however that you do not need sprintf() at all for some of these: since you are not substituting any variables, so you could just call fputs with the string directly.

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

Furthermore, the way you test for end of file is incorrect: while (buf[0] != EOF) cannot test if you have reached the end of file, checking the return value of fgets() is the correct way to do this:

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

这篇关于为什么我的程序在写入文件时崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆