C language: I cannot find the output file.

Click For Summary

Discussion Overview

The discussion revolves around a coding issue in C language related to file output. Participants are examining a code snippet that attempts to create a file but appears to not generate the expected output file.

Discussion Character

  • Technical explanation, Debate/contested

Main Points Raised

  • One participant shares a code snippet and asks what is wrong with it, indicating an issue with file creation.
  • Another participant questions whether a file named "fname" can be found, suggesting that the file name is being treated as a literal string rather than a variable.
  • A suggestion is made to remove the double quotes around "fname" in the fopen statement to correctly use the variable fname for the file name.
  • One participant confirms they can find a file named "fname" literally, indicating a potential misunderstanding or different interpretation of the issue.
  • A later reply reiterates the suggestion to remove the quotes to achieve the expected file output.

Areas of Agreement / Disagreement

Participants express differing views on the interpretation of the file name issue, with some focusing on the literal string versus variable usage, but no consensus is reached on the overall problem.

Contextual Notes

The discussion does not resolve the underlying issue with the code, and assumptions about the expected behavior of the fopen function and file naming conventions remain unaddressed.

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!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
5
Views
2K
Replies
4
Views
5K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
6
Views
2K