gpt4 book ai didi

关于我的文件声明的 C2015 错误

转载 作者:行者123 更新时间:2023-11-30 17:08:54 24 4
gpt4 key购买 nike

我的 Visual Studio 2013 出现 C2015 错误:

A character constant contains more than two characters. The limit is one character for standard character constants and two characters for long character constants.

关于:

spMyOutput = fopen("C:\\MyOutput.txt", "w");

根据该错误的一些搜索,我不确定我做错了什么,因为 spMyOutput 没有声明为 char 变量,而是声明为 FILE .

可能还有其他地方我做错了,但我不确定可能是什么。

#include<stdio.h>
int nine();
void txtdoc(int count);
void main()
{
int count;
count = nine();
txtdoc(count);
system("pause");
return;
}
int nine()
{
int loopcount = 9;
int i;
while (loopcount >= 0)
{

for (i = 1; i <= loopcount; i++)
{
printf("%d", i);
}
loopcount--;
printf("\n");
}
return count;
}
void txtdoc(int count)
{
FILE* spMyOutput;
int close;
spMyOutput = fopen("C:\\MyOutput.txt", "w");
if (!spMyOutput)
{
printf("Could not open file.\a\n");
}
fprintf(spMyOutput, "%d\n", count);
close = fclose(spMyOutput);
if (close == EOF)
{
printf("Could not close file.\a\n");
}
return;
}

最佳答案

更正编译错误并应用注释后,您的代码现在可以干净地编译。

注意:pause 是 Windows 批处理文件命令。更好的选择是:getchar()

#include <stdio.h>
#include <stdlib.h>

int nine( void );
void txtdoc(int count);

int main()
{
int count;
count = nine();
txtdoc(count);
getchar();
return 0;
}


int nine()
{
int loopcount = 9;
int i;
while (loopcount >= 0)
{

for (i = 1; i <= loopcount; i++)
{
printf("%d", i);
}
loopcount--;
printf("\n");
}
return loopcount;
}


void txtdoc(int count)
{
FILE* spMyOutput;


spMyOutput = fopen("C:\\MyOutput.txt", "w");
if (!spMyOutput)
{
perror("Could not open file.\a\n");
exit( EXIT_FAILURE );
}

fprintf(spMyOutput, "%d\n", count);
fclose(spMyOutput);
}

这是运行上述代码的结果。代码将暂停,等待用户最后一次击键,然后退出。

123456789
12345678
1234567
123456
12345
1234
123
12
1

关于关于我的文件声明的 C2015 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33468720/

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