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]);
	}
}

}
Re: how to copy an array to a file? 80 80

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.

Re: how to copy an array to a file? 80 80

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.