怎么把结果输入到记事本上~最好举个例子
c语言编的程序的结果输入到一个文本文件中可以使用fprintf;
例:
#include<stdio.h>
main(){
FILE *fpt;
fpt = fopen("wendangming.txt","w");//打开文档,写入
fprintf(fpt,"Hello world");
fclose(fpt);
}
扩展资料
它打开一个文本文件,逐个字符地读取该文件
#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;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2017-09-01
可以使用fprintf()等函数。
#include <stdio.h>
int main(void)
{
FILE *fp=fopen("c:\\a.txt","w");/*打开文件*/
int s=9;
fprintf(fp,"%d\n",s);/*按指定格式将指定的内容输出到指定的文件,fprintf()函数除了第一个参数是个文件指针之外,其余用法和printf()一样*/
fprintf(fp,"HE\n");
fclose(fp);/*关闭文件*/
return 0;
}本回答被提问者采纳
#include <stdio.h>
int main(void)
{
FILE *fp=fopen("c:\\a.txt","w");/*打开文件*/
int s=9;
fprintf(fp,"%d\n",s);/*按指定格式将指定的内容输出到指定的文件,fprintf()函数除了第一个参数是个文件指针之外,其余用法和printf()一样*/
fprintf(fp,"HE\n");
fclose(fp);/*关闭文件*/
return 0;
}本回答被提问者采纳
第2个回答 2010-02-23
用FILE这个结构体好了,然后用fputc或fputs或write这些函数写入到记事本
#include <stdio.h>
#include <stdlib.h>
#define LINE 1024
int main(){
FILE *pFile;
char* pFilePath;
char* str="hello world!";
printf("input the file path: ");
scanf("%s",pFilePath);
if(NULL == pFilePath)
return 3;
pFile = fopen(pFilePath, "wt");
if ( pFile == NULL)
{
cout<<"file open faild"<<endl;
return 4;
}
fputs(str,pFile);//将数据写入到文件
fclose(pFile);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define LINE 1024
int main(){
FILE *pFile;
char* pFilePath;
char* str="hello world!";
printf("input the file path: ");
scanf("%s",pFilePath);
if(NULL == pFilePath)
return 3;
pFile = fopen(pFilePath, "wt");
if ( pFile == NULL)
{
cout<<"file open faild"<<endl;
return 4;
}
fputs(str,pFile);//将数据写入到文件
fclose(pFile);
return 0;
}
第3个回答 2010-02-23
fstream os;
os.open(1.txt,ios::out | ios::in);
os.open(1.txt,ios::out | ios::in);