Why is my C executable not working properly?

  • Thread starter Thread starter Aurel
  • Start date Start date
AI Thread Summary
The discussion centers around troubleshooting a C program that fails to open a text file using the "fopen" function, returning a NULL pointer. The user initially encounters issues because the file "trial.txt" is not found, despite being in the same directory as the executable. It is revealed that the executable's path may not include the current directory, which is why the program works when run from DOS but fails in the IDE. The solution involves specifying the correct file path, particularly noting that Windows requires double backslashes in file paths. Additionally, to keep the console window open after execution, the "system("pause")" command should be placed before the return statement. After addressing these issues, the user confirms that the code now works correctly.
Aurel
Messages
2
Reaction score
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;
}
}
 
Computer science news on Phys.org
probably where you run the executable the path is not set to include the current directory (while in DOS it is)

You do not get the pause because if you return the rest of the code (after the return statement) is not executed, so to still have the pause you have to place the system("pause") before the return statement.
 
If you're running Visual C++ it puts the executable in the 'debug' directory by default, or 'release' if you set it to that.

Put a path in the filename. If you're on Windows, remember that the backslash character is used as an escape character by C, so you have to double them up. For example:

"c:\\my files\\test\\debug\\trial.txt"
 
Thanks a lot ceptimus and gerben!

My code works now...the path was wrong...for some reason all my files are saved on the D: but DevC is in the C: drive...and I didn't think to put the system(pause) in that part of the code...but hey, all's well now...thanks alot!

Aurel.. :smile:
 
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...
Back
Top