使用fopen_s打开文件流时出现未处理的异常访问冲突

时间:2018-10-16 02:01:37

标签: c fopen access-violation unhandled-exception

弄清楚为什么我的代码不起作用,我遇到了很多麻烦。

该程序的目的是从给定的数组中生成一系列随机句子,然后将其输出到屏幕或文本文件中。

我不确定是什么问题,但是当我为文件输入标题时,出现了无法处理的异常错误。

在将FILE**流参数更改为NULL而不是在fopen_s中写入的情况下,我收到调试声明错误。

我相信问题在于我声明指针的方式。

#include <.stdio.h>
#include <.conio.h>

int main()
{
    char * article[5] = { "the", "one","some","any","a" };
    char * noun[5] = { "boy","girl","dog","town","car" };
    char * verb[5] = { "drove","jumped","ran","walked","skipped" };
    char * preposition[5] = { "to","from","over","under","on" };
    int x = 0;
    char * output[100] = {0};

    //char output = { "" };

    FILE ** write = "C:\Users\dilli\Downloads\test.txt";

    while (5) {
        printf("Enter one(1) to output to screen, two(2) to output to file:\n");
        scanf_s("%d",&x);
        if(x==1)
            printf_s("%s %s %s %s %s %s.\n", article[rand() % 5], noun[rand() % 5], verb[rand() % 5],
                preposition[rand() % 5], article[rand() % 5], noun[rand() % 5]);
        else if (x == 2)
        {
            printf("Enter name of output file:\n");
            scanf_s("%s",&output,100);
            printf("output:\n%s",output);
            fopen_s(write,output, "w");//This is where we are getting an unhandled exception.
            fprintf("%s %s %s %s %s %s.\n", article[rand() % 5], noun[rand() % 5], verb[rand() % 5],
                preposition[rand() % 5], article[rand() % 5], noun[rand() % 5]);
            fclose(write);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您不能将字符串文字分配给FILE**变量。那甚至不应该编译。而且,您甚至都没有使用字符串文字,因为您是在提示用户输入输出文件名。因此,只需除去字符串文字即可。

第二,您滥用fopen_s()scanf_s(),这就是代码崩溃的原因。

  • 当提示用户输入文件名时,您正在要求scanf_s()读入一个指针数组而不是一个字符数组。单独这样做可能会崩溃,或者至少在以后尝试访问数组内容时会导致未定义的行为。

  • 之后,将无效的FILE**指针和无效的char[]数组传递给fopen_s()。它希望您传递一个指向有效FILE*变量的指针,以及一个以空值结尾的char字符串,而不是char*指针数组。

第三,您根本不会将打开的FILE*传递给fprintf()

话虽如此,请尝试以下方法:

#include <stdio.h>
#include <conio.h>

int main()
{
    const char* article[5] = { "the", "one","some","any","a" };
    const char* noun[5] = { "boy","girl","dog","town","car" };
    const char* verb[5] = { "drove","jumped","ran","walked","skipped" };
    const char* preposition[5] = { "to","from","over","under","on" };
    char fileName[260] = {0};
    FILE *write = NULL;
    int i, x;

    for (i = 0; i < 5; ++i) {
        printf("Enter one(1) to output to screen, two(2) to output to file:\n");
        x = 0;
        scanf_s("%d", &x);
        if (x == 1) {
            printf_s("%s %s %s %s %s %s.\n", article[rand() % 5], noun[rand() % 5], verb[rand() % 5], preposition[rand() % 5], article[rand() % 5], noun[rand() % 5]);
        }
        else if (x == 2) {
            printf("Enter name of output file:\n");
            scanf_s("%s", fileName, 260);
            printf("output:\n%s", fileName);
            if (fopen_s(&write, fileName, "w") == 0) {
                fprintf(write, "%s %s %s %s %s %s.\n", article[rand() % 5], noun[rand() % 5], verb[rand() % 5], preposition[rand() % 5], article[rand() % 5], noun[rand() % 5]);
                fclose(write);
            }
        }
    }

    return 0;
}