关于C++输入输出流?
#include<fstream.h>#include<iostream.h>#include<stdlib.h>voidmain(){ifstreaminfile;of...
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
void main()
{
ifstream infile;
ofstream outfile;
outfile.open("my.txt",ios::out);
char un[]="ActL",name[10];
int course=2401,number;
outfile<<un<<endl;
outfile<<course<<endl;
infile.open("my.txt",ios::in);
if(!infile)
{
cerr<<"can't open my.txt"<<endl;
exit(1);
}
char c;
infile>>name>>c>>number;
outfile<<"name:"<<name<<endl;
outfile<<"number:"<<number<<endl;
}
输出结果:
ActL
2401
name:ActL
number:401
我想知道我没给name,number赋值啊 ,怎么回事啊,还有2401怎么变成401了?刚学习流,希望大大门能够解答,不胜感激。 展开
#include <iostream.h>
#include <stdlib.h>
void main()
{
ifstream infile;
ofstream outfile;
outfile.open("my.txt",ios::out);
char un[]="ActL",name[10];
int course=2401,number;
outfile<<un<<endl;
outfile<<course<<endl;
infile.open("my.txt",ios::in);
if(!infile)
{
cerr<<"can't open my.txt"<<endl;
exit(1);
}
char c;
infile>>name>>c>>number;
outfile<<"name:"<<name<<endl;
outfile<<"number:"<<number<<endl;
}
输出结果:
ActL
2401
name:ActL
number:401
我想知道我没给name,number赋值啊 ,怎么回事啊,还有2401怎么变成401了?刚学习流,希望大大门能够解答,不胜感激。 展开
展开全部
ifstream //读入文件
ofstream //写出
outfile<<un<<endl;
outfile<<course<<endl; //向my.txt 写出uu course
ActL
2401
infile>>name>>c>>number;
//从my.txt 读入 ActL /2401
name="Actl" c='2' number=401 明白了吧 2 被你当做 一个字符 赋值给c了
然后
outfile<<"name:"<<name<<endl;
outfile<<"number:"<<number<<endl; //写出
ofstream //写出
outfile<<un<<endl;
outfile<<course<<endl; //向my.txt 写出uu course
ActL
2401
infile>>name>>c>>number;
//从my.txt 读入 ActL /2401
name="Actl" c='2' number=401 明白了吧 2 被你当做 一个字符 赋值给c了
然后
outfile<<"name:"<<name<<endl;
outfile<<"number:"<<number<<endl; //写出
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询