Why is my C executable not working properly?

  • Thread starter Thread starter Aurel
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a C executable that fails to read from a text file, specifically addressing issues with file paths, error handling, and program execution behavior in different environments.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant reports that the "fopen" function returns a NULL pointer, suggesting a potential issue with the file path or directory settings.
  • Another participant proposes that the executable's running environment may not include the current directory in its path, unlike when running from DOS.
  • A suggestion is made to place the "system('pause')" command before the return statement to allow the user to view the output before the program exits.
  • A different participant notes that Visual C++ defaults to placing executables in a 'debug' or 'release' directory, which may affect file path accessibility.
  • It is mentioned that on Windows, backslashes in file paths need to be escaped, which could lead to further issues if not handled correctly.
  • The original poster later confirms that correcting the file path resolved the issue and expresses gratitude for the assistance received.

Areas of Agreement / Disagreement

Participants generally agree that the problem was related to file paths and execution environments, but there are multiple suggestions on how to handle the "pause" behavior and file path formatting, indicating some variation in approaches.

Contextual Notes

Limitations include potential misunderstandings about file path requirements and the behavior of different development environments, which may not have been fully explored in the discussion.

Who May Find This Useful

Individuals working with C programming, particularly those facing issues with file I/O operations, debugging, or environment-specific behavior in their code execution.

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 a lot!

Aurel.. :smile:
 

Similar threads

Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
5K
Replies
14
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K