C language: I cannot find the output file.

AI Thread Summary
The code provided has a critical error in the file opening statement. The variable `fname`, which is intended to hold the dynamically generated filename, is enclosed in double quotes in the `fopen` function, causing the program to attempt to open a file literally named "fname" instead of using the value stored in the `fname` variable. To correct this, the double quotes should be removed from around `fname` in the `fopen` call. This change will allow the program to create a file with the expected name based on the formatted string generated by `sprintf`.
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:
Technology 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!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top