C language: I cannot find the output file.

In summary, to generate a file with the expected file name, the double-quotes around "fname" in the open statement should be removed to use the variable name fname instead of the literal string "fname".
  • #1
nenyan
67
0
What is wrong with my code?
Code:
#include <stdio.h>int
main(int argc, char *argv[])
{
	int i=0;
	FILE *fp;
	char fname[100];

	sprintf(fname,"%04X.txt",i);	
	fp=fopen("fname", "w");
	fprintf(fp, "hello world!\n");
	fclose(fp);
}
 
Last edited:
Technology news on Phys.org
  • #2
nenyan said:
What is wrong with my code?
Code:
#include <stdio.h>int
main(int argc, char *argv[])
{
	int i=0;
	FILE *fp;
	char fname[100];

	sprintf(fname,"%04X.txt",i);	
	fp=fopen("fname", "w");
	fprintf(fp, "hello world!\n");
	fclose(fp);
}

Hi nenyan! :smile:

Can you find a file named "fname" (literally)?
 
Last edited:
  • #3
Or, in order to generate the file with the file name that you are expecting, you need to remove the double-quotes from around "fname" in the open statement so that you use the variable name fname and not the literal string "fname"
 
  • #4
I like Serena said:
Hi nenyan! :smile:

Can you find a file named "fname" (literally)?

yes...I can...
 
  • #5
gsal said:
Or, in order to generate the file with the file name that you are expecting, you need to remove the double-quotes from around "fname" in the open statement so that you use the variable name fname and not the literal string "fname"

Thank you very much!
 

FAQ: C language: I cannot find the output file.

Why am I unable to find the output file after compiling my C program?

The most common reason for not being able to find the output file is due to errors in the code that prevent the program from successfully compiling. Check for any syntax errors or missing libraries that may be causing the issue.

How can I specify the location of the output file in my C program?

You can specify the location of the output file by using the -o flag when compiling your program. For example, gcc program.c -o output will create an executable file named "output" in the same directory as your program.

Can I change the name of the output file in my C program?

Yes, you can change the name of the output file by using the -o flag when compiling your program. Just specify a different name after the flag, for example, gcc program.c -o new_output will create an executable file named "new_output".

Is there a default location for the output file in C programs?

No, there is no default location for the output file in C programs. It will be created in the current directory unless you specify a different location using the -o flag when compiling.

How can I view the contents of the output file in my C program?

You can view the contents of the output file by running the executable file that was created after compiling your program. This will display the output in your terminal or command prompt.

Similar threads

Back
Top