本篇文章给大家谈谈c语言无法写入txt文件内容,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、c语言关于文件的写入。部分代码如下,为什么文件里面什么都没有?2、C语言如何将一串字符和数字写入txt文本文档3、c语言怎么把结构体数组写入txt文件4、C语言创建TXT文档并写入内容5、C语言中,把数据储存在txt文件中的代码怎么写的?
c语言关于文件的写入。部分代码如下,为什么文件里面什么都没有?
调试完成,运行结果:
#include stdio.h
typedef struct usersorder
{
char tel[20];//tellphone number
char name[11];//收货人
char address[20];//收货地址
char pwd[20];//支付密码
}uorder;
main()
{
uorder q;
FILE *fp;
printf(“please input tellphone number :\n”);
scanf(“%s”,q.tel);
printf(“please input username :\n”);
scanf(“%s”,q.name);
printf(“please input address :\n”);
scanf(“%s”,q.address);
printf(“please input pwd :\n”);
scanf(“%s”,q.pwd);
if((fp = fopen(“order.txt”,”w”))==NULL)
{
printf(“failure !\n”);
return 0;
}
fprintf(fp,”%s\n”,q.tel);
fprintf(fp,”%s\n”,q.name);
fprintf(fp,”%s\n”,q.address);
fprintf(fp,”%s\n”,q.pwd);
fclose(fp);
}
C语言如何将一串字符和数字写入txt文本文档
#include stdio.h
main()
{
FILE *fp = NULL; //声明并初始化文件指针
char str[100] = “123456”; //声明并赋值str字符串数组变量
fp = fopen(“data.txt”,”w”); //只读方式打开文件
fputs(str,fp); // 写入str内容
fclose(fp); //关闭文件
}
c语言怎么把结构体数组写入txt文件
fopen(“student.txt”,”wb”)
b就是二进制的意思。你用二进制形式将student.txt打开
而fwrite也是将内存中的数据不经转换直接写入文件,所以文件中保存的是二进制形式,当然看到乱码了。
你试试:
if((fp1=fopen(“student.txt”,”w”))==NULL)
{printf(“cannot open this file\n”);
exit(0);
}
printf(“No Name Eng point Math point Phy point\n”);
for(i=0;iN;i++)
{scanf(“%s,%s,%d,%f,%d,%f,%d,%f”,student[i].no,student[i].name,student[i].eng,student[i].point1,
student[i].math,student[i].point2,student[i].phy,student[i].point3);
student[i].ave=(student[i].eng+student[i].math+student[i].phy)/3.0;
fprintf(fp1,”%s,%s,%d,%f,%d,%f,%d,%f”,student[i].no,student[i].name,student[i].eng,student[i].point1,
student[i].math,student[i].point2,student[i].phy,student[i].point3);
}
就是用fprintf函数写入文件,你试试看吧。
C语言创建TXT文档并写入内容
下面所有file模式,如果文件不存在,就创建文件。模式你自己选。另外还有很多其他函数,你自己看书。下面是最基本得的
file
*pfile
=
fopen(“d:\\001.txt”,
“rb”);//以二进制方式打开d盘得001.txt文件
fseek(pfile,0l,seek_end);//将文件指针移动到文件末尾
unsigned
long
buflen
=
ftell(pfile);//获取文件长度
unsigned
char
*buf
=
new
unsigned
char[buflen];//创建改文件长度大小的一块内存用来放要读进来的文件内容
rewind(pfile);//指针移动到文件头
fread(buf,1,buflen,pfile);//将文件的内容全部读入buf中
fclose(pfile);//关闭文件
上面为读文件,下面为写文件
file
*pfile
2=
fopen(“d:\\002.txt”,
“wb”);//以写二进制方式打开002.txt文件
fwrite(buf,1,buflen,pfile2);//将buf写入002.txt
fclose(pfile2);//关闭文件
C语言中,把数据储存在txt文件中的代码怎么写的?
c语言,把数据存在txt文件里,需要使用fopen函数以写文件的方式打开文件。
然后可以使用fprintf,fputc,fputs,fwrite等函数,把相应类型的数据写入文件。
最后,写入完成后使用fclose函数关闭文件。
下面的C语言程序源程序展示了合并A.txt和B.txt两个TXT文件的内容存储到到新建的一个TXT文件,C.txt。
#include stdio.h
#include stdlib.h
using namespace std;
typedef struct Student {
char name[32];
char sex[6];
int age;
float score;
} stu;
int main(int argc, char* argv[]) {
stu a[48];
FILE* ra =fopen(“A.txt”,”r”);
FILE* rb =fopen(“B.txt”,”r”);
FILE* wc =fopen(“C.txt”,”w”);
if(ra==NULL || rb==NULL || wc==NULL) {
printf(“failed to open file\n”);
system(“pause”);
return 0;
}
int i=0;
while(fscanf(ra,”%s%s%d%f”,a[i].name,a[i].sex,a[i].age,a[i].score)!=EOF) {
i++;
}
fclose(ra);
while(fscanf(rb,”%s%s%d%f”,a[i].name,a[i].sex,a[i].age,a[i].score)!=EOF) {
i++;
}
fclose(rb);
int n=i;
for(i=0;in;i++) {
fprintf(stdout,”%s\t%s\t%d\t%g\n” ,a[i].name,a[i].sex,a[i].age,a[i].score);
fprintf(wc,”%s\t%s\t%d\t%g\n” ,a[i].name,a[i].sex,a[i].age,a[i].score);
}
fclose(wc);
system(“pause”);
return 0;
}
其中A.txt中的内容如下:
B.txt中的内容如下:
编译运行后,程序执行的结果如下:
同时,存储到C.txt中的数据如下:
c语言无法写入txt文件内容的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、c语言无法写入txt文件内容的信息别忘了在本站进行查找喔。