0

Hello every one?I have a program use to copy the contents of an array to a File.But it does not work. when it runs Visual Studio starts to debug. But nothing happening.File contents does not change.Please help me tho solve this problem.Thank you very much.
Here is the program.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
FILE *f;
int i=0;
int data[]={1,2,3,4,5};


if((f=fopen("D:\data\def.txt","w"))==NULL){
	printf("File can't open.\n");
}
else{
	for(i=0;i<5;i++){
		fprintf(f,"%s\n",data[i]);
	}
}

}
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by chathu12
0

Hello every one?I have a program use to copy the contents of an array to a File.But it does not work. when it runs Visual Studio starts to debug. But nothing happening.File contents does not change.Please help me tho solve this problem.Thank you very much.
Here is the program.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
FILE *f;
int i=0;
int data[]={1,2,3,4,5};


if((f=fopen("D:\data\def.txt","w"))==NULL){
	printf("File can't open.\n");
}
else{
	for(i=0;i<5;i++){
		fprintf(f,"%s\n",data[i]);
	}
}

}

In your fopen() function call, change '\' characters to '/' in your path. Alternatively, you can escape the '\' character by using "\\".

Other issues:

Your main is supposed to return an integer - you're missing a return statement at the end of your code in main().

Your format specifier in fprintf (i.e. the %s) is wrong. You are writing integers so you need to use %d.

0

Thanks Yellow Snow.you are a genius.I am using Japanese Keyboard.here "\" print as "yen"mark.Little different to the English key board.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.