⑴ c語言屏幕輸出的內容如何保存到文件中
C語言中用流替換函數freopen可以從文件中讀取數據或將數據輸出到文件中。
需要引用庫"stdio.h",即
#include<stdio.h>
freopen的聲明如下:
FILE *freopen(const char * restrict filename, const char * restrict mode, FILE * restrict stream);
形參說明:
filename:需要重定向到的文件名或文件路徑。
mode:代表文件訪問許可權的字元串。例如,"r"表示「只讀訪問」、"w"表示「只寫訪問」、"a"表示「追加寫入」。
stream:需要被重定向的文件流。
返回值:如果成功,則返回該指向該輸出流的文件指針,否則返回為NULL。
用法:
將輸入流從stdin替換成指定文件可以從文件中讀取數據;
將輸出流從stdout替換成指定文件可以將數據輸出到文件中。
下面舉個例子:
#include<stdio.h>
int main(){
freopen("in.txt","r",stdin); //從in.txt中讀數據
freopen("out.txt","w",stdout);//向out.txt中寫數據
int a,b;
while(~scanf("%d%d", &a, &b)){
printf("%d %d\n");
}
return 0;
}
⑵ 如何將C語言運行的結果存到TXT文本中
簡單,直接用管道的方式,比如你的程序為a.exe
那麼直接, ./a.exe > 1.txt 直接寫就入 1.txt中了
⑶ C語言怎樣將定義的函數中程序運行的結果寫到文件中
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct student
{
char name[20];
int num;
float score;
struct student* pnext;
};
int count;
struct student* creat()
{
struct student* phead=NULL;
struct student* ptail,*p;
count=0;
ptail=p=(struct student*)malloc(sizeof(struct student));
printf("依次輸入學生學號,姓名,成績:\n");
scanf("%d%s%f",&p->num,p->name,&p->score);
while(p->num!=10)
{
count++;
if(count<=10)
{
p->pnext=phead;
ptail=p;
phead=p;
}
else{
p->pnext=NULL;
ptail->pnext=p;
ptail=p;
}
p=(struct student*)malloc(sizeof(struct student));
scanf("%d%s%f",&p->num,p->name,&p->score);
}
free(p);
return phead;
}
void print(struct student* phead)
{
struct student *ptemp;
int index=1;
printf("--這個鏈表中有%d個學生--\n",count);
printf("\n");
ptemp=phead;
while(ptemp!=NULL)
{
printf("學生%d是: ",index);
printf("學號%d ",ptemp->num);
printf("姓名%s ",ptemp->name);
printf("成績%f \n",ptemp->score);
printf("\n");
ptemp=ptemp->pnext;
index++;
}}
struct student* insert(struct student* phead)
{
struct student* p;
printf("插入一個成員\n");
p=(struct student*)malloc(sizeof(struct student));
scanf("%d%s%f",&p->num,p->name,&p->score);
p->pnext=phead;
phead=p;
count++;
return phead;
}
void delet (struct student* phead,int index)//把delete改成了delet,其他沒變,否則我的系統編譯出錯。
{
int i;
struct student* ptemp;
struct student* ppre;
ptemp=phead;
ppre=ptemp;
printf("刪除第%d個學生\n",index);
for(i=1;i<index;i++)
{
ppre=ptemp;
ptemp=ptemp->pnext;
}
ppre->pnext=ptemp->pnext;
free(ptemp);
count--;
}
void file(struct student* phead)
{
FILE *fp;
if((fp=fopen("D:\\cx.txt","wb"))==NULL)
{printf("file can not open!\n"); exit(1);}
int index=1;//加上這一句
while(phead!=NULL)
{
//fwrite(phead,sizeof(struct student),1,fp);//屏蔽這一句
//加上下面這幾句
fprintf(fp,"學生%d是: ",index);
fprintf(fp,"學號%d ",phead->num);
fprintf(fp,"姓名%s ",phead->name);
fprintf(fp,"成績%f \n",phead->score);
fprintf(fp,"\n");
index++;
//ok
phead=phead->pnext; }
fclose(fp);
printf("-----成功保存信息!-----\n");
}
int main()
{
struct student* phead;
phead=creat();
phead=insert(phead);
delet(phead,2);
print(phead);
file(phead);
return 0;
}
試試看可以文本列印沒?,一個小提醒:你的程序運行似乎有點小問題,自己看看吧
改成下面可能容錯性會好一點,也試試:(我也初學,請見諒!)
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct Student//這里我用了首字母大寫
{
char name[20];
int num;
float score;
struct Student * pnext;//這里如果把struct Student改成student會編譯出錯(以前沒注意)
}student;//利用typedef後面可以省力點
int count;
student * creat()
{
student * phead=NULL;
student * ptail, * p;
count=0;
ptail=p=(student *)malloc(sizeof(student));
printf("依次輸入學生學號,姓名,成績:\n");
scanf("%d%s%f",&p->num,p->name,&p->score);
while(p->num!=0)//這里我改了,容錯性會好些,輸入學號為0時會結束輸入,
//後面程序也考慮了空記錄的情況
{
count++;
if(count==1)//第一條記錄為頭記錄
phead=p;//設置第一條記錄為頭記錄
else
ptail->pnext=p;
ptail=p;
p=(student*)malloc(sizeof(student));
scanf("%d%s%f",&p->num,p->name,&p->score);
}
ptail->pnext=NULL;//設置尾記錄指向空地址
return phead;
}
void print(student* phead)
{
student *ptemp;
ptemp=phead;
int index=1;
printf("--這個鏈表中有%d個學生--\n",count);
printf("\n");
if (phead!=NULL)//判斷是不是為空記錄
{
do
{
printf("學生%d是: ",index);
printf("學號%d ",ptemp->num);
printf("姓名%s ",ptemp->name);
printf("成績%f \n",ptemp->score);
printf("\n");
ptemp=ptemp->pnext;
index++;
}while(ptemp!=NULL);//看看不用do...while換成while行不?有時會搞不明白的,呵呵
}
}
student* insert(student* phead)//插入函數未改動,但該函數是插到第一個之前的,可以改一下
{
student* p;
printf("插入一個成員\n");
p=(student*)malloc(sizeof(student));
scanf("%d%s%f",&p->num,p->name,&p->score);
p->pnext=phead;
phead=p;
count++;
return phead;
}
student* delet (student* phead,int index)//我把delete改成delet了否則我的編譯器沒法通過
{
int i;
student* ptemp;
student* ppre;
ptemp=phead;
ppre=ptemp;
printf("刪除第%d個學生\n",index);
if(index>count || index<=0)//提高了容錯性
printf("共有%d個記錄,無法刪除第%d個學生\n",count,index);
else
{
if (1==index)
{
phead=phead->pnext;
}
else
{
for(i=1;i<index;i++)
{
ppre=ptemp;
ptemp=ptemp->pnext;
}
ppre->pnext=ptemp->pnext;
}
free(ptemp);
count--;
}
return phead;
}
void file(student* phead)
{
FILE *fp;
if((fp=fopen("D:\\cx.txt","wb"))==NULL)
{printf("file can not open!\n"); exit(1);}
student *ptemp;
ptemp=phead;
int index=1;
fprintf(fp,"--這個鏈表中有%d個學生--\n",count);
fprintf(fp,"\n");
if (ptemp!=NULL)
{
do
{
fprintf(fp,"學生%d是: ",index);
fprintf(fp,"學號%d ",ptemp->num);
fprintf(fp,"姓名%s ",ptemp->name);
fprintf(fp,"成績%f \n",ptemp->score);
fprintf(fp,"\n");
ptemp=ptemp->pnext;
index++;
}while(ptemp!=NULL);
}
fclose(fp);
printf("-----成功保存信息!-----\n");
}
int main()
{
student* phead;
phead=creat();
phead=insert(phead);
phead=delet(phead,2);//分別改成0,1,2,末記錄,試試原來的程序這里有問題的
print(phead);
file(phead);
return 0;
}
⑷ 用C語言如何將結果輸出到一個文本文件中保存
文件的操作步驟:
#include <stdio.h> #include <stdlib.h> int main()
{
FILE *fp;
int i, d;
fp=fopen("data.txt","w");
if(fp==NULL)
{
printf("File cannot open! " );
exit(0);
}
for(i=0; i<10; i++)
{
scanf("%d", &d);
fprintf(fp,"%d ", d);
}
fclose(fp);
return 0;
}
格式化輸出:
#include <stdio.h> #include <stdlib.h> int main()
{
FILE *fp;
int i, No;
float salary;
fp=fopen("data.csv","w");
if(fp==NULL)
{
printf("File cannot open! " );
exit(0);
}
//輸入5名員工的工號,並保存到文件中
for(i=0; i<5; i++)
{
scanf("%d %f", &No, &salary);
fprintf(fp,"%d, %.2f ", No, salary);
}
fclose(fp);
return 0;
}
(4)在c語言中如何把結果存放文件中擴展閱讀:
從文件中讀取字元:
#include <stdio.h> #include <stdlib.h> int main()
{
FILE *fp;
char c;
if ((fp=fopen( "data.dat" , "r" ))==NULL)
{
printf("File cannot open!");
exit(0);
}
while((c=fgetc(fp))!=EOF)
putchar(c);
fclose(fp);
return 0;
}
⑸ 怎麼把c語言編的程序的結果輸入到一個文本文件中
c語言編的程序的結果輸入到一個文本文件中可以使用fprintf;
例:
#include<stdio.h>
main(){
FILE *fpt;
fpt = fopen("wendangming.txt","w");//打開文檔,寫入
fprintf(fpt,"Hello world");
fclose(fpt);
}
(5)在c語言中如何把結果存放文件中擴展閱讀
它打開一個文本文件,逐個字元地讀取該文件
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream testByCharFile;
int num;
char c;
testByCharFile.open("6.5.cpp",ios::in);
while(!testByCharFile.eof())
{
testByCharFile >> c;
num++;
}
testByCharFile.close();
cout << num << endl;
}
⑹ 如何保存c語言程序計算的結果
先打開一個文件:
FILE *fphzk
fphzk=("c:\\test.txt',"rt+")
其意義是打開C驅動器磁碟的根目錄下的文件test.txt。
文件使用方式 意 義
「rt」 只讀打開一個文本文件,只允許讀數據
「wt」 只寫打開或建立一個文本文件,只允許寫數據
「at」 追加打開一個文本文件,並在文件末尾寫數據
「rb」 只讀打開一個二進制文件,只允許讀數據
「wb」 只寫打開或建立一個二進制文件,只允許寫數據
「ab」 追加打開一個二進制文件,並在文件末尾寫數據
「rt+」 讀寫打開一個文本文件,允許讀和寫
「wt+」 讀寫打開或建立一個文本文件,允許讀寫
「at+」 讀寫打開一個文本文件,允許讀,或在文件末追加數 據
「rb+」 讀寫打開一個二進制文件,允許讀和寫
「wb+」 讀寫打開或建立一個二進制文件,允許讀和寫
「ab+」 讀寫打開一個二進制文件,允許讀,或在文件末追加數據
if((fp=fopen("c:\\test.txt","rt+")==NULL)
{
printf("\nerror on open file!");
getch();
exit(1);
}
這段程序的意義是,如果返回的指針為空,表示不能打開C盤根目錄下的test.txt文件,則給出提示信息「error on open file!」,下一行getch()的功能是從鍵盤輸入一個字元,但不在屏幕上顯示。在這里,該行的作用是等待, 只有當用戶從鍵盤敲任一鍵時,程序才繼續執行, 因此用戶可利用這個等待時間閱讀出錯提示。敲鍵後執行exit(1)退出程序。
下面的程序是完成讀入一個字元,然後保存在文件中。
#include<stdio.h>
main()
{
FILE *fp;
char ch;
if((fp=fopen("string","wt+"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
printf("input a string:\n");
ch=getchar();
while (ch!='\n')
{
fputc(ch,fp);
ch=getchar();
}
rewind(fp);
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
printf("\n");
fclose(fp);
}
⑺ c語言中怎樣把數據存入文件
原因:
使用fopen時參數不正確,你是用 w參數,若文件存在則文件長度清為0,即該文件內容會消失。每次都是重新清空並寫數據, 將w修改為a即可.
fopen函數說明見下方:
---------------
FILE * fopen(const char * path,const char * mode);
[編輯本段]函數說明
參數path字元串包含欲打開的文件路徑及文件名,參數mode字元串則代表著流形態。
mode有下列幾種形態字元串:
r 打開只讀文件,該文件必須存在。
r+ 打開可讀寫的文件,該文件必須存在。
rb+ 讀寫打開一個二進制文件,只允許讀寫數據。
rt+ 讀寫打開一個文本文件,允許讀和寫。
w 打開只寫文件,若文件存在則文件長度清為0,即該文件內容會消失。若文件不存在則建立該文件。
w+ 打開可讀寫文件,若文件存在則文件長度清為零,即該文件內容會消失。若文件不存在則建立該文件。
a 以附加的方式打開只寫文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數據會被加到文件尾,即文件原先的內容會被保留。(EOF符保留)
a+ 以附加方式打開可讀寫的文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數據會被加到文件尾後,即文件原先的內容會被保留。 (原來的EOF符不保留)
wb 只寫打開或新建一個二進制文件;只允許寫數據。
wb+ 讀寫打開或建立一個二進制文件,允許讀和寫。
wt+ 讀寫打開或著建立一個文本文件;允許讀寫。
at+ 讀寫打開一個文本文件,允許讀或在文本末追加數據。
ab+ 讀寫打開一個二進制文件,允許讀或在文件末追加數據。
⑻ C語言中,如何把一個函數的結果存放在文本文件中,並且不寫在主程序內
看了你和別人的交流 直接上個代碼吧
folatfun(floatx);
floata,b;
main(){
scanf("輸入x",&a);
b=fun(a);
printf("x=%f",b);}
floatfun(floatx){
floatret;
FILE*fp;
if(x<-1)ret=x;
if(x>=-1)ret=x*2;
fp=fopen("result.txt","w");
fprintf(fp,"x=%f",ret);
returnret;
}
這樣就是你要求的了
主函數不變 在fun中返回同時輸入到文件 然後主函數正常輸出
⑼ 怎麼樣將C語言程序執行的結果保存在一個文件里
在dos模式下,可以使用「應用程序名
=>
文本文件」回車。
例如要把一個名為test.exe的c語言可執行程序的運行結果保存下來,則可以在dos的命令提示符下:test
=>
c:\result.txt
回車,這樣運行結果就保存在文本文件中了。
⑽ c語言如何用文件存儲數據
具體操作步驟如下:
1、首先,創建一個新文件夾,在該文件夾中創建一個文檔,如下圖所示,然後進入下一步。