❶ c語言 將數據輸出到txt文檔
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
voidmain()
{
inti,j;
FILE*fp;
fp=fopen("random1.txt","w");
if(fp==NULL)
printf("不能打開文件 ");
srand((unsigned)time(NULL));
for(i=0;i<100;i++)
{
j=rand()%100;
if(j<25) //6到10的25%
{
j=rand()%10;
while(j<6)
j=rand()%10;
fprintf(fp,"%d",j);
}
elseif(j<75) //11到14的50%
{
j=rand()%15;
while(j<10)
j=rand()%15;
fprintf(fp,"%d",j);
}
else //15到18的25%
{
j=rand()%19;
while(j<15)
j=rand()%19;
fprintf(fp,"%d",j);
}
printf("%d ",j);
}
}
新建一個random1.txt就可以了
❷ 請問C語言的結果如何將其運行結果輸出到文本文件,請高手寫出詳細的解答過程,高分獎賞!!!
你說的這個用到文件操作就可以了,,
主要用到格式化輸出函數fprintf()
格式:fprintf(文件指針名,格式段,數據項)
例:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("F:\\file.txt","w"); /*文件寫打開*/
printf("%d",1+3);/* 黑認情況下輸出到標准屏幕文件stdout,原來是fprintf(stdout,"%d",1+3),後簡化*/
fprintf(fp,"%d",1+3); /*到這句只要把屏幕當作一個文件看待,這里fprintf()就好理解了,就是把數據放到一個文件fp上,而不是屏幕文件stdout*/
fclose(fp);/*文件關閉*/
}
❸ 用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;
}
(3)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語言輸出到txt文本
修改如下:
#include <stdio.h>
#include <math.h>
#define PI 3.1415926
main()
{
FILE *fp;
fp=fopen("f:\\1.txt","w"); //根據你的文件位置修改路徑。
int i,j;
double w,w1,w2,a,a1,z,z1,h,q,t,m;
double A1,A2,C1,C2,M1,M2,N1,N2;
for(i=0;i<95;i++)
{ for(j=0;j<10;j++)
{ a=-23.5+0.5*i;
a1=a/180*PI;
h=90-40+a;
m=0.5+0.5*j;
z1=atan(m);
z=z1/PI*180;
q=40.0/180*PI;
M1=1/m*sin(q)+cos(q)*cos(PI);
M2=1/m*sin(q)+cos(q)*cos(0);
N1=1/m*cos(q)-sin(q)*cos(PI);
N2=1/m*cos(q)-sin(q)*cos(0);
A1=-M1*N1*sin(a1);
A2=-M2*N2*sin(a1);
C1=N1*N1*cos(a1);
C2=N2*N2*cos(a1);
w1=acos(A1/C1);
w2=acos(A2/C2);
if(a>0)
{ if(h>=z) w=2*w1;
else w=2*fabs((w2-w1));
}
else
{ if(h<=z) w=0;
else w=2*w2;
}
w=w/PI*180;
t=w/15;
fprintf(fp,"%6.1f",t);
}
fprintf(fp,"\n");
}
fclose(fp);
}
執行結果如下:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.6 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2.3 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2.8 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3.3 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3.7 0.0 0.0 0.0 0.0 0.0 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8.5 5.6 4.4 3.6 3.1 2.7
12.6 11.8 11.3 10.9 9.5 5.8 4.5 3.8 3.2 2.8
12.6 11.8 11.3 10.9 10.6 6.1 4.7 3.9 3.4 3.0
12.6 11.8 11.2 10.9 10.6 6.5 4.9 4.1 3.5 3.1
12.6 11.8 11.2 10.8 10.5 6.8 5.2 4.2 3.6 3.2
12.7 11.8 11.2 10.8 10.5 7.2 5.4 4.4 3.8 3.3
12.7 11.8 11.2 10.8 10.5 7.7 5.6 4.6 3.9 3.4
12.7 11.7 11.1 10.7 10.4 8.3 5.9 4.8 4.1 3.6
12.7 11.7 11.1 10.7 10.4 9.5 6.2 5.0 4.2 3.7
12.7 11.7 11.1 10.7 10.3 10.1 6.5 5.2 4.4 3.8
12.8 11.7 11.1 10.6 10.3 10.0 6.9 5.4 4.6 4.0
12.8 11.7 11.0 10.6 10.2 10.0 7.4 5.7 4.8 4.1
12.8 11.7 11.0 10.6 10.2 9.9 8.0 6.0 5.0 4.3
❺ c語言程序的結果怎麼輸出到.txt文件上
樓主的意思是輸出你的結果把?
看我寫的
==================================================================
#include
#include
int
main()
{
double
x,
a0,a1,f,fd;
x=1.0;
file
*fp;//建立一個文件操作指針
fp=fopen("1.txt","w+");//以追加的方式建立或打開1.txt,默認位置在你程序的目錄下面
do
{a0=x;
f=((a0-18)*a0+95)*a0-150;
fd=(3*a0-36)*a0+95;
x=a0-f/fd;
}
while(x-a0>=1.e-5);
printf("\nx=%.6f",x);
fprintf(fp,"\nx=%.6f",x);//同輸出printf一樣,以格式方式輸出到文本中
fclose(fp);//關閉流
}
❻ 如何將C語言運行的結果存到TXT文本中
簡單,直接用管道的方式,比如你的程序為a.exe
那麼直接, ./a.exe > 1.txt 直接寫就入 1.txt中了
❼ 怎麼把c語言編的程序的結果輸入到一個文本文件中
c語言編的程序的結果輸入到一個文本文件中可以使用fprintf;
例:
#include<stdio.h>
main(){
FILE *fpt;
fpt = fopen("wendangming.txt","w");//打開文檔,寫入
fprintf(fpt,"Hello world");
fclose(fpt);
}
(7)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語言程序計算出來的結果輸出到txt
很同意樓主的觀點。我也很鄙視那些嫌分數少而不回答問題的人。我感覺那和大家都很憎惡的問路要錢沒什麼兩樣。
閑來無事編了一個程序,參考一下吧:
新建一工程,窗體名AlarmForm.
放置一個Timer,名字為Timer1。放置一個Label,名字為lblTime
代碼如下:
Option Explicit
Dim AlarmTime
Const conMinimized = 1
Private Sub Form_Click()
AlarmTime = InputBox("輸入啟動鬧鍾的時間", "VB 鬧鍾", AlarmTime)
If AlarmTime = "" Then Exit Sub
If Not IsDate(AlarmTime) Then
MsgBox "您輸入的時間無效。"
Else ' 從 InputBox 返回的字元串使有效時間,
AlarmTime = CDate(AlarmTime) ' 將它作為一個日期/時間值存儲在 AlarmTime 中。
End If
End Sub
Private Sub Form_Load()
AlarmTime = ""
End Sub
Private Sub Form_Resize()
If WindowState = conMinimized Then ' 如果窗體被最小化, 在標題處顯示時間。
SetCaptionTime
Else
Caption = "鬧鍾"
End If
End Sub
Private Sub SetCaptionTime()
Caption = Format(Time, "Medium Time") ' 使用中等時間格式顯示時間。
End Sub
Private Sub Timer1_Timer()
Static AlarmSounded As Integer
If lblTime.Caption <> CStr(Time) Then
' 當前秒數與顯示秒數不同。
If Time >= AlarmTime And Not AlarmSounded Then
Beep
MsgBox "啟動鬧鍾在 " & Time
AlarmSounded = True
ElseIf Time < AlarmTime Then
AlarmSounded = False
End If
If WindowState = conMinimized Then
' 如果處於最小化狀態, 每分鍾都需更新窗體標題。
If Minute(CDate(Caption)) <> Minute(Time) Then SetCaptionTime
Else
' 否則每秒鍾對標簽的標題進行更新。
lblTime.Caption = Time
End If
End If
End Sub
樓主要努力啊!
❾ C語言改寫輸出方式用文本
#include<stdio.h>
intt=0;
intans[33],num[33],n,m;
voiddfs(intg,intmin,FILE*p){
inti;
if(g==m){
t++;
for(i=0;i<m-1;i++)
{printf("%d",num[ans[i]]);fprintf(p"%d",num[ans[i]])}
printf("%d ",num[ans[i]]);
fprintf(p,"%d ",num[ans[i]])}
else{
for(i=min;i<n;i++){
ans[g]=i;
dfs(g+1,i+1,p);}}}
intmain(){
FILE*pFile=fopen("text.txt","w");
if(!pFile)
{
return-1;
}
inti;
printf("輸入n,m ");
fprintf(pFile,"輸入n,m ");
scanf("%d%d",&n,&m);
printf("輸入%d個數字 ",n);
fprintf(pFile,"輸入%d個數字 ",n);
for(i=0;i<n;i++)scanf("%d",num+i);
dfs(0,0,pFile);
printf("共有%d個組合 ",t);
fprintf(pFile,"共有%d個組合 ",t);
fclose(pFile);
return0;}
我在每個printf後面加入了一個fprintf, 並且在main頭部定義了一個文件指針(指向文件流的指針), 然後把它當作參數傳給了你的自定義函數.
你寫的代碼有點亂, 要是文件輸出有問題, 你自己排查排查, 哪個地方的塊我弄錯了.
❿ 怎麼把C語言的結果輸出為txt文件
樓主的意思是輸出你的結果把?
看我寫的
==================================================================
#include <stdio.h>
#include <math.h>
int main()
{
double x, a0,a1,f,fd;
x=1.0;
FILE *fp;//建立一個文件操作指針
fp=fopen("1.txt","w+");//以追加的方式建立或打開1.txt,默認位置在你程序的目錄下面
do
{a0=x;
f=((a0-18)*a0+95)*a0-150;
fd=(3*a0-36)*a0+95;
x=a0-f/fd;
} while(x-a0>=1.e-5);
printf("\nx=%.6f",x);
fprintf(fp,"\nx=%.6f",x);//同輸出printf一樣,以格式方式輸出到文本中
fclose(fp);//關閉流
}