1

Hi I have the following code

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

int main()
{
    FILE *fp;
    char c;
    printf("Contents of the file before appending:\n");
    fp=fopen("E:\Append.txt","r");

    while(!feof(fp))
    {
        c=fgetc(fp);
        printf("%c",c);
    }

    fp=fopen("E:\Append.txt","a");

    if(fp==NULL)
    {
        printf("The File cannot be appended");
        exit(1);
    }
    printf("Enter String to Append:");

    fp=fopen("E:\Append.txt","w");

    while(c!='.')
    {
        c=getche();
        fputc(c,fp);
    }
    fclose(fp);

    printf("Contents  of the file after Appending");

    fp=fopen("E:\Append.txt","r");

    while(!feof(fp))
    {
        c=fgetc(fp);
        printf("%c",c);
    }

}

But when i try to run the code in VSTS2010 , i was getting the following message

"Debug Assertion Failed! Program :E:\Programs\VSTS\14.1\Debug\14.1exe File:f:\dd\vctool\crt_bld\self_X86\crt\src\feoferr.c Line:44

Expression(Stream !=NULL)"

Please help me what went wrong. Thanks In Advance.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
user2733944
  • 37
  • 1
  • 5

1 Answers1

0

You're not checking the return value of any of your fopen calls. You then call functions which act upon the file stream, and they assert that stream != NULL, which fails.

You need to check return values.

fp = fopen(some_path);
if (!fp) {
    // error
}

Looks like one of your calls to feof.

Ed S.
  • 122,712
  • 22
  • 185
  • 265