In: Computer Science
#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
FILE *fp;
char c;
errno_t err;
err = 0;
err =
fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but
I err=2 but I don't think it is opening the file?
printf("%d\n", err);
if (err == 2)
{
printf("The file
was opened\n");
while (1)
{
c = fgetc(fp) ?? Getting error when I try to
read a file.;
if (c == EOF)
{
break;
}
printf("%c", c);
}
}
else
{
printf("The file
was not opened\n");
}
fclose(fp);
return(0);
}
I did it in my own way and I got it right.
//#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
FILE *fp;
char c;
fp = fopen("/home/jack/Desktop/test.txt", "r"); //Opening file.
enter the correct path to open the file
if (fp != NULL)
{
printf("The file was opened\n");
while (1)
{
c = fgetc(fp);// ?? Getting error when I try to read a file.;
if (c == EOF)
{
break;
}
printf("%c", c);
}
}
else
{
printf("The file was not opened\n");
}
fclose(fp);
return(0);
}
Here I changed the path of the file only. Maybe the path you entered is incorrect.
Check the properties of the file to get the path of the file.