在C编程中写入.txt文件 [英] Write on .txt file in C programming

查看:162
本文介绍了在C编程中写入.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以这种方式写文件



我尝试过:



例如:

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

推荐答案

使用fwrite: C库函数fwrite() [ ^ ]
Use fwrite: C library function fwrite()[^]


引用:

如何以这种方式写入文件

how can writing on file in this way



在这个网站上,我们帮助您修复代码,但是您必须显示代码并解释您的问题。

写入文件并不复杂而不是写入控制台,实际上大部分是相同的。

由于教你如何做是超出了本论坛的主题,你应该重新阅读你的课程或找到互联网上众多的教程之一。 br $>
-----

我们不做你的家庭作业。

HomeWork不会测试你的技能,乞求别人做你的工作,它会让你希望并帮助您的老师检查您对所学课程的理解,以及您应用这些课程时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请展示您的代码并解释这个问题,我们可能会提供帮助。


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.


这篇关于在C编程中写入.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆