
1、编写C++程序,将“Hello world!”字符串输出到“hello.txt”文本文件中。
1、编写程序,将“Helloworld!”字符串输出到“hello.txt”文本文件中。2、以第1题中生成的“hello.txt”文件作为输入文件,编写程序,从屏幕上输出...
1、编写程序,将“Hello world!”字符串输出到“hello.txt”文本文件中。
2、以第1题中生成的“hello.txt”文件作为输入文件,编写程序,从屏幕上输出“hello.txt”文件中的内容。 展开
2、以第1题中生成的“hello.txt”文件作为输入文件,编写程序,从屏幕上输出“hello.txt”文件中的内容。 展开
展开全部
这是很简单的文件读写,自己看书就可以了 ,这是我写的,可以运行成功,不过这么简单最好自己写。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
/* 写数据入文件时ifstream file_out("hello.txt");改为 ofstream file_out("hello.txt");*/
ifstream file_out("hello.txt");
char s[20]="hello world!";
char p[20];
int i =0;
/* 写hello world 入文件 */
if(file_out.fail())
{
cerr<<"错误"<<endl;
return 1;
}
while(s[i] != '\0')
{
file_out.put(s[i]);
i++;
}*/
/* 重文件中读出*/
for(i=0;i<50;i++);
file_out.seekg(0,ios::beg);
file_out.getline(p,20,'\0');
cout<<p<<endl;
file_out.close();
}
分别注释开就是2文件了,注意读写要改的地方
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
/* 写数据入文件时ifstream file_out("hello.txt");改为 ofstream file_out("hello.txt");*/
ifstream file_out("hello.txt");
char s[20]="hello world!";
char p[20];
int i =0;
/* 写hello world 入文件 */
if(file_out.fail())
{
cerr<<"错误"<<endl;
return 1;
}
while(s[i] != '\0')
{
file_out.put(s[i]);
i++;
}*/
/* 重文件中读出*/
for(i=0;i<50;i++);
file_out.seekg(0,ios::beg);
file_out.getline(p,20,'\0');
cout<<p<<endl;
file_out.close();
}
分别注释开就是2文件了,注意读写要改的地方

2025-09-24 广告
URule Pro Java 规则引擎,一款给业务人员使用的可视化商业决策规则引擎系统,打开浏览器即可开始设计业务规则;URule Pro是一款自主研发纯Java规则引擎,亦是一款国产智能风控决策引擎,可以运行在Windows、Linux、...
点击进入详情页
本回答由锐道提供
展开全部
额,我只会C语言,不过C++对C完全兼容,这个也可以用的。
1.
#include<stdio.h>
#include<string.h>
FILE *p;
char a[]="Hello world";
if((p=fopen("hello.txt","w"))==0)
{
printf("无法创建hello.txt,请检查原因“):
exit(0);
}
fwrite(a,strlen(a),1,p);
fclose(p);
printf("Done\n");
}
2.
#include<stdio.h>
#include<string.h>
FILE *p;
char a[20];
short i;
if((p=fopen("hello.txt","r"))==0)
{
printf("无法读取hello.txt,请检查原因“):
exit(0);
}
fread(a,20,1,p);
fclose(p);
printf("%s\n",a);
printf("Done\n");
}
1.
#include<stdio.h>
#include<string.h>
FILE *p;
char a[]="Hello world";
if((p=fopen("hello.txt","w"))==0)
{
printf("无法创建hello.txt,请检查原因“):
exit(0);
}
fwrite(a,strlen(a),1,p);
fclose(p);
printf("Done\n");
}
2.
#include<stdio.h>
#include<string.h>
FILE *p;
char a[20];
short i;
if((p=fopen("hello.txt","r"))==0)
{
printf("无法读取hello.txt,请检查原因“):
exit(0);
}
fread(a,20,1,p);
fclose(p);
printf("%s\n",a);
printf("Done\n");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string s;
ofstream outfile("helle.txt",ios::app);
if(!outfile)
{cout<<"存入失败!"<<endl;
exit(1);}
outfile<<"Hello world"<<endl;
outfile.close();
ifstream infile("hello.txt");
if(!infile)cout<<"error"<<endl;
while(getline(infile,s))
{
cout<<s<<endl;
}
infile.close();
return 0;
}
#include<fstream>
#include<string>
using namespace std;
int main()
{
string s;
ofstream outfile("helle.txt",ios::app);
if(!outfile)
{cout<<"存入失败!"<<endl;
exit(1);}
outfile<<"Hello world"<<endl;
outfile.close();
ifstream infile("hello.txt");
if(!infile)cout<<"error"<<endl;
while(getline(infile,s))
{
cout<<s<<endl;
}
infile.close();
return 0;
}
追问
你好,第二题呢?
追答
第二道题我写到一块了,在程序里已经实现了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这么简单的分,俺也不好意思拿啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询