C language: I cannot find the output file.

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 3K views
nenyan
Messages
67
Reaction score
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:
Physics news on Phys.org
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:
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"
 
I like Serena said:
Hi nenyan! :smile:

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

yes...I can...
 
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!