Why is my C executable not working properly?

  • Thread starter Aurel
  • Start date
In summary, the code is trying to open a text file, but is returning a NULL pointer. If you put a return 1 after the printf("ERROR"); line, the executable window flashes as if system("pause") wasn't specified--how do you still get to view the executable after specifying return 1?
  • #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;
}
}
 
Computer science news on Phys.org
  • #2
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.
 
  • #3
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"
 
  • #4
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:
 

Related to Why is my C executable not working properly?

1. What is a C executable?

A C executable is a compiled program written in the C programming language. It is a file that can be run on a computer and performs specific tasks or functions.

2. How do I create a C executable?

To create a C executable, you need to write your code in a text editor or an integrated development environment (IDE) and then compile it using a C compiler. The compiler will convert your code into a machine-readable format, which can be executed by the computer.

3. Why is my C executable not running?

There could be several reasons why your C executable is not running. Some common issues include errors in the code, missing libraries or dependencies, or incorrect compiler settings. It is best to check for any error messages and troubleshoot accordingly.

4. How can I debug my C executable?

To debug a C executable, you can use a debugger tool that allows you to step through your code and identify any errors or bugs. You can also use print statements or logging to help narrow down the issue and fix it.

5. Can I distribute my C executable to others?

Yes, you can distribute your C executable to others. However, make sure to adhere to any licensing requirements or restrictions for any libraries or code used in your program. You may also need to provide instructions for running the program on different systems or platforms.

Similar threads

Replies
2
Views
2K
Replies
8
Views
2K
Replies
2
Views
180
Replies
3
Views
1K
Back
Top