Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can writing on file in this way

What I have tried:

for example:
seq          name        type2          type3                type4
1             z           33.4          5.3                  444.7
2             w           66.4          6.1                   99.4
3             m 
.              .              
.              .
.              .


I have array of struct and I want to write it on file what is wrong in this code?

int write_to_file( chrom *data, char *filename,int ch,int m) 
{ 
 
printf("\n Creating %s.csv file",filename);
 
FILE *fp;
 
int i,j;
 
filename=strcat(filename,".csv");
 
fp=fopen(filename,"w+");
 
fprintf(fp,"Student Id, Physics, Maths");
for(i=0;i<m;i++){
 
    for(j=0;j<ch;j++){
 
fprintf(fp," %s  %d     \n", data[i].name[j] , data[i].id[j]);
         
 
    }
 }
fclose(fp);
 
printf("\n %sfile created",filename);
 
}
Posted
Updated 22-Jun-18 7:02am
v5

 
Share this answer
 
Comments
Lilyanaa 21-Jun-18 14:28pm    
I want in the same format in the example how can do that?
Richard MacCutchan 22-Jun-18 2:42am    
Just put spaces or tabs between fields, or write in .CSV format. You can use fprintf, _fprintf_l, fwprintf, _fwprintf_l[^] to write formatted text.
Lilyanaa 22-Jun-18 9:22am    
thanks for your helping, can you see my update question?
Richard MacCutchan 22-Jun-18 9:32am    
Yes, but that is not .csv format. CSV stands for comma separated variables, so you need to add commas between individual fields rather than spaces. You also have five format specifiers in your format string, but only four data fields.

In general you should not use formatting in your backing store (file, database, etc.). The only time you need the data to be formatted is when it is displayed or printed for a user.
Lilyanaa 22-Jun-18 9:33am    
why Segmentation fault ?
Quote:
how can writing on file in this way

On this site, we help you to fix your code, but you have to show your code and explain your problem.
Writing to file is no more complicated than writing to console, in fact mostly the same.
Since teaching you how to do is out of topic of this forum, you should reread your lessons or find one of the numerous tutorials available on internet.
-----
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900