zoukankan      html  css  js  c++  java
  • C++读写文件的简单例子

    #include <iostream>
    #include <fstream>
    using namespace std;
    
    void main()
    {
        ofstream in;
        in.open("com.txt",ios::trunc); //ios::trunc表示在打开文件前将文件清空,由于是写入,文件不存在则创建
        int i;
        char a='a';
        for(i=1;i<=26;i++)//将26个数字及英文字母写入文件
        {
            if(i<10)
            {
                in<<"0"<<i<<"	"<<a<<"
    ";
                a++;
            }
            else
            {
                in<<i<<"	"<<a<<"
    ";
                a++;
            }
        }
        in.close();//关闭文件
    }
    
    
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    void main()
    {
        char buffer[256];
        fstream out;
        out.open("com.txt", ios::in);
        cout << "com.txt" << " 的内容如下:" << endl;
        while (!out.eof())
        {
            out.getline(buffer, 256, '
    ');//getline(char *,int,char) 表示该行字符达到256个或遇到换行就结束
            cout << buffer << endl;
        }
        out.close();
        cin.get();//cin.get() 是用来读取回车键的,如果没这一行,输出的结果一闪就消失了
    }
  • 相关阅读:
    php或JS中输出判断项
    拿大神的博客来记一下
    2017.6.8 项目进展
    2017.6.8
    2017.5.18
    2017.5.17
    2017.5.16
    如何实现从php传数据到js
    项目笔记
    tp框架之Model类与命名空间
  • 原文地址:https://www.cnblogs.com/rain-1/p/5597843.html
Copyright © 2011-2022 走看看