- #1
Aurel
- 2
- 0
I've written the following code to experiment on reading from text files...however, the "fopen" statement is apparently returning a NULL pointer. What could be the problem here?...trial.txt is in the same directory as the executable. In addition, if I put return 1 after the printf("ERROR"); line...the executable window flashes as if system("pause") wasn't specified--how do i still get to view the executable after specifying return 1?? Also when i do a debug, it returns the error
MAIN-T~1.EXE: No such file or directory.
<gdb>
Now the part I really don't get...if i run this program from DOS...it works!...it prints Success and the content of the trial.txt file...how could that be?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{ FILE *ptr;
char c;
ptr=fopen("trial.txt", "r");
if(!ptr)
{ printf("ERROR");
}
else{ printf("Success");
while(1)
{
c=fgetc(ptr);
if(c!=EOF)
{
printf("%c",c);
}
else {break;}
}
printf("Now closing file...\n");
fclose(ptr);
system("PAUSE");
return 0;
}
}
MAIN-T~1.EXE: No such file or directory.
<gdb>
Now the part I really don't get...if i run this program from DOS...it works!...it prints Success and the content of the trial.txt file...how could that be?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{ FILE *ptr;
char c;
ptr=fopen("trial.txt", "r");
if(!ptr)
{ printf("ERROR");
}
else{ printf("Success");
while(1)
{
c=fgetc(ptr);
if(c!=EOF)
{
printf("%c",c);
}
else {break;}
}
printf("Now closing file...\n");
fclose(ptr);
system("PAUSE");
return 0;
}
}