您的位置:首页 > 编程语言 > C语言/C++

C++的输入输出流、文件操作

2010-12-20 21:57 477 查看
1.  C++保留C的输入输出单个字符的函数
  (1)  putchar(c)  —— 字符输出
   例程:
#include
using namespace std;
int main()
{
    char a,b,c;
    a='B'; b='O';c='Y';
    putchar(a); putchar(b); putchar(c);putchar('/n');
    putchar(66);putchar(79);putchar(89);putchar(10);    //10是换行的ASCII码
    return 0;
}
  (2) getchar()  ——字符输入函数
  例程:
  #include
  using namespace std;
int main()
{
   char c;
   c=getchar();
   putchar(c+32);   //转化为小写字母,大小写之间包括[ / ] ^ - 、6个字符,因此不加26加32。'A'=65
   putchar('/n');
   return  0;
}
  也可用cout来输出: cout<<(c=getchar()+32);
2. scanf()、printf()函数的输入和输出
    scanf(格式控制,输出表列);
    printf(格式控制,输出表列);
  例程:
   #include
  using namespace std;
int main()
{
     int a;
     float b;
     char c;
     scanf("%d % c % f", &a,&c,&b);
     printf("a=%d, b=%f, c=%c /n",a,b,c);
     return 0;
}
3. 标准输入输出流cin、cout
   包括头文件#include
   输入输出流的控制符要加入头文件 #include
   cout<<"dec:"<
      using namespace std;
      int main()
     {
          char ch[20];
          cout<<"in put a string:"<
      using namespace std;
      int main()
     {
         char ch[20];
         cout<<"input a string:"<>ch;
        cout<<"The string read with cin is:"<
       using namespace std;
      int main()
      {
          char c;
          while(!cin.eof())            //若未遇到文件结束符
                if((c=cin,get())!=' ')  //检查读入的字符是否为空格字符
                    cout.put(c);
          return 0;
      }
4. 磁盘文件的打开和关闭
     打开
     ofstream outfile;
      if(outfile.open("f1.data",ios::app)==0)    // 打开
            cout<<"打开失败!";
         ……………………
      outfile.close();                                          //关闭 
5. 文件写入
#include
#include
#include         //一定不能少了,否则报错 error C2679
using namespace std;
int main()
{
   string str;
   ofstream out("d.txt",ios::out);       //等价于ofstream out("d.txt")   
   if(!out)              //若打开失败,out返回0值
  {
      cerr<<"打开失败!"<
#include
#include
using namespace std;
int main()
{
    ifstream infile("d://new//d.txt",ios::in); //定义输入文件的流对象,以输入方式打开磁盘文件d.txt,第二个参数可去
   if(!infile)
    {
        cerr<<"打开失败!"<
#include
#include
using namespace std;
int main()
{
  ifstream in("d.txt");
  if(!in)
  {
     cerr<<"打开源文件失败!"<
#include
#include
using namespace std;
void display(char *filename)
{
    ifstream infile(filename,ios::in);
    if(!infile)
   {
      cerr<<"打开失败!"<
#include
using namespace std;
//从键盘读入字母并存入文件“d.txt”中
void save_to_file()
{
  ofstream ofile("d.txt");
  if(!ofile)
  {
   cerr<<"打开失败d.txt!"<=65 && c[i]<=90||c[i]>=97 && c[i]<=122)
   {
    ofile.put(c[i]);   //将字母存入磁盘文件d.txt
    cout<=97 &&ch<=122)
   ch=ch-32;
   outfile.put(ch);
   cout<>         
用法1:最基本,也是最常用的用法,输入一个数字:
#include
using namespace std;
main ()
{
int a,b;
cin>>a>>b;
cout<
using namespace std;
main ()
{
char a[20];
cin>>a;
cout<
using namespace std;
main ()
{
char ch;
ch=cin.get();               //或者cin.get(ch);
cout<
using namespace std;
main ()
{
char a[20];
cin.get(a,20);
cout<
using namespace std;
main ()
{
char m[20];
cin.getline(m,5);
cout<
#include
using namespace std;
main ()
{
char m[3][20];
for(int i=0;i<3;i++)
{
cout<<"/n请输入第"<”
#include
#include
using namespace std;
main ()
{
string str;
getline(cin,str);
cout<”
#include
#include
using namespace std;
main ()
{
char m[20];
gets(m);                       //不能写成m=gets();
cout<
#include
using namespace std;
main ()
{
char m[3][20];
for(int i=0;i<3;i++)
{
cout<<"/n请输入第"<>也可以适用,原因是这里输入的没有空格,如果输入了空格,比如“ks kr jkl[回车]”那么cin就会已经接收到3个字符串,“ks,kr,jkl”;再如“kskr 1[回车]kskr 2[回车]”,那么则接收“kskr,1,kskr”;这不是我们所要的结果!而cin.getline()和gets()因为可以接收空格,所以不会产生这个错误;
6、getchar()   //接受一个字符,需包含“#include”
#include
#include
using namespace std;
main ()
{
char ch;
ch=getchar();                        //不能写成getchar(ch);
cout<
#include
using namespace std;
main ()
{
string str;
getline(cin,str);
cout<getchar()   //接受一个字符,需包含“#include”
       char ch =getchar();
cin.get(char c) 或者 cin.get()
      char ch=cin.get();  或者cin.get(ch)
              getline()和cin.getline()类似,但是cin.getline()属于istream流,而getline()属于string流,是不一样的两个函数
cin.getline(char a[],int 5,'/n') //将字符数组a的前5个字符读入,遇换行符结束;如:a bc12ds,将会读入4个字符 a bc,外加一个'/0';
                                        // 一般第三个参数不写,默认为'/0'
         cin.getline(m[i],20) //可用于多维数组中
getline() // 接受一个字符串,可以接收空格并输出,需包含“#include"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: