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.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top