c语言要保存一系列数据是放到文件里面存下来,还是放到哪里比较合适,断电重新上电后这些数据也要在?

如题所述

这个,你只要是保存在文件里面的,肯定都是一直存在的啊,而且,现在的电脑对于你编辑的文件都会进行保护的,如果你在操作一个文件的时候即将关机的话,你的文件内容会自动保存下来的,这个和软件园哦有一定的关系。如果你问的是计算机硬件的工作的话,这个应该是保存在一系列的寄存器中。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-11-25
可以使用freopen
#include<cstdio>

freopen("文件名.in","r",stdin); //将程序输入定向到同目录到1.in文件
freopen("文件名.out","w",stdout);//将程序输出定向到同目录到1.out文件
举例1

#include<stdio.h>int main(){ /* redirect standard output to a file */ if(freopen("D:\\output.txt", "w", stdout) == NULL) fprintf(stderr,"error redirecting stdout\n"); /* this output will go to a file */ printf("This will go into a file.\n"); /*close the standard output stream*/ fclose(stdout); return 0;}

举例2
如果上面的例子您没看懂这个函数的用法的话,请看这个例子。这个例子实现了从stdout到一个文本文件的重定向。即,把输出到屏幕的文本输出到一个文本文件中。

#include<stdio.h>int main(){ int i; if (freopen ("D:\\output.txt", "w", stdout) == NULL) fprintf(stderr, "error redirecting stdout\n"); for (i = 0; i < 10; i++) printf("%3d", i); printf("\n"); fclose(stdout); return 0;}

编译运行一下,你会发现,十个数输出到了D盘根目录下文本文件output.txt中。
举例3
从文件in.txt中读入数据,计算加和输出到out.txt中
#include<stdio.h>int main(){ int a, b; freopen("in.txt","r",stdin); /* 如果in.txt不在连接后的exe的目录,需要指定路径如D:\in.txt */ freopen("out.txt","w",stdout); /*同上*/ while (scanf("%d%d", &a, &b) != EOF) printf("%d\n",a+b); fclose(stdin); fclose(stdout); return 0;}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网