❶ 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 0.0 0.0 0.0 0.0
4.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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);//关闭流
}