TAGS :Viewed: 9 - Published at: a few seconds ago

[ C2015 error regarding my declaration of a FILE ]

My Visual Studio 2013 gives me an C2015 error:

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

with regards to:

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

Based on some of the searches of that error, I am not sure what I have done wrong, since spMyOutput was not declared as a char variable but as a FILE.

There might be something else that I'm doing wrong, but I'm not sure what it might be.

#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;
}

Answer 1


after correcting the compile errors and applying the comments, your code now cleanly compiles.

Note: pause is a windows batch file command. a better choice would be: 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);
}

here the result of running the above code. where the code will pause for a final keystroke from the user before exiting.

123456789
12345678
1234567
123456
12345
1234
123
12
1