在C中编码时,无法使用VS express 2013创建文件

在C中编码时,无法使用VS express 2013创建文件,c,C,我最初使用代码块在C中创建程序,使用结构写入文件,但我有一台使用Windows7的64位计算机。一旦我意识到我无法编写二进制文件,我就开始尝试将除了写出来的垃圾之外的同一个程序放入我复制到VS express中的文件中。在适应了运行所需的更改之后,我无法创建txt或二进制文件。遵循是我的准则。请原谅我注释掉的所有代码,但这是一项正在进行的工作 int GetNewFile() { struct sumCamp allrecs[50]; char ans; char rec

我最初使用代码块在C中创建程序,使用结构写入文件,但我有一台使用Windows7的64位计算机。一旦我意识到我无法编写二进制文件,我就开始尝试将除了写出来的垃圾之外的同一个程序放入我复制到VS express中的文件中。在适应了运行所需的更改之后,我无法创建txt或二进制文件。遵循是我的准则。请原谅我注释掉的所有代码,但这是一项正在进行的工作

int GetNewFile()
{
    struct sumCamp allrecs[50];
    char ans;
    char recName[20];

    printf("\tYou are now in the create a file.\n");
    printf("\tIf this is where you want to be enter Y in not enter N.\n");
    fflush(stdin);

    ans = getchar();

    fflush(stdin);
    printf("\tThis is your answer %c \n", ans);
    if (toupper(ans) == 'Y')
    {
        puts("\tWhat is the name of the file you want to create?\n ");
        fgets(recName, 20, stdin);
        /*(gets(recName);*/
        printf("\tThis is the the file you created %s.\n", recName);
        /* clientPtr = fopen_s(recName, "w");*/
        /*errno_t errorCode = fopen_s(&clientPtr, sizeof(recName), "w+b");*/
        fopen_s(&clientPtr, (recName), "w");
    }

    if (clientPtr == NULL)
    {
        printf("\tError in creating the file.\n");
        exit(1);
    }

    fclose(clientPtr);

    printf("\tHello world!6\n");

    return(0);
}  

/*The structure and file also is the following;*/

FILE *clientPtr;      /*This is the file pointer*/

struct sumCamp                   /*sumCamp if the structure tag*/
{
    char name[25];
    char dateIn[6];
    char dateOut[6];
    int mealAll;                   /*These are the stucture members*/
    int mealBreak;
    int mealLunch;
    int mealDinner;
    long int contract;
    char patDate[5];
};

/*void GetInfo(void);  */         /*allrecs is the stucture varibles*/
/*struct sumCamp GetInfo (struct sumCamp allrecs[50]);*/
/* GetUpdate (void);*/
“改变:

      fopen_s(&clientPtr, (recName), "w");
致:

然后,您可以确定文件是否已成功打开;如果未成功打开,它将打印一个错误代码,这将有助于进一步诊断问题


编辑:

对于问题代码,这意味着“fopen_s()”不喜欢&clientPtr或(readName)

我的第一个猜测是(readName)有问题。要验证这是否是问题所在,您可能需要(暂时)更改:

例如:

int rCode=fopen_s(&clientPtr, "testfile.tmp", "w");
如果rCode是“0”而不是“22”,则问题在于(recName)

我猜情况就是这样。如果是这样,recName的问题在于它的末尾包含一个换行符“\n”(可能还有一个回车符“\r”)。这在使用“fgets()”获取用户输入时非常常见

“\n”(和“\r”)可以从“recName”中删除,如下所示:

...
{
    puts("\tWhat is the name of the file you want to create?\n ");
    fgets(recName, 20, stdin);
...
为此:

....
{
    char *cp;

    puts("\tWhat is the name of the file you want to create?\n ");
    fgets(recName, 20, stdin);

    for(cp=recName, *cp, ++cp)
       if(('\r' == *cp) || ('\n' == *cp))
          *cp='\0';
....
这应该已经修复了“recName”问题。现在将线路更改回原位并重新测试:

    int rCode=fopen_s(&clientPtr, (recName), "w");
顺便说一下,“recname”周围不需要括号:

int rCode=fopen_s(&clientPtr, recName, "w");

输出是什么(控制台、文件)?任何错误(编译时、运行时)?您的问题是什么?这不是答案。谢谢,我已按照您的建议添加,结果错误消息是fopen()返回失败代码:22@user3613841,答案现在更新为关于故障代码22的更多详细信息。
....
{
    char *cp;

    puts("\tWhat is the name of the file you want to create?\n ");
    fgets(recName, 20, stdin);

    for(cp=recName, *cp, ++cp)
       if(('\r' == *cp) || ('\n' == *cp))
          *cp='\0';
....
    int rCode=fopen_s(&clientPtr, (recName), "w");
int rCode=fopen_s(&clientPtr, recName, "w");