现在的位置: 首页 > 综合 > 正文

IO操作

2018年08月31日 ⁄ 综合 ⁄ 共 1968字 ⁄ 字号 评论关闭

以文本方式写入读出文件

/ vc 2005
// 问题:写入数据与读出数据不一致
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int write_file()
{
     ofstream out_file("out.txt");
     if( !out_file )
     {
         cerr << " can't open out.txt/n";
         return 0;
     }

     int n =80;
     float f = 3.98;
     string str("acceleration");
     out_file << n << f << str;
     out_file.close();
     return 1;
}

int read_file()
{
    ifstream in_file("out.txt");
    if( !in_file)
    {
        cerr << "can't read out.txt or no exist/n" ;
        return 0;
    }
    int n;
    float f;
    string s;
    in_file>>n>>f>>s;
    in_file.close();
    cout << " read data fromm out.txt"
         << n << "," << f << "," << s << endl;
}
void main()
{
    write_file();
    read_file();
    system("pause");
}

//////////////////////////////////////////////////////////////////////////////////////

以规定字符结束输入

#include <iostream>
#include <string>
using namespace std;

int main()
{     
    char chArray[30];
    cin.get(chArray,30,'/n');
    for( int i=0;chArray[i] != '/0'; i++)
    {
        cout << chArray[i] << "  ";
    }
    return 0;
}

//////////////////////////////////////////////////////////////////////////////////////

@font-face{
font-family:"Times New Roman";
}
@font-face{
font-family:"宋体";
}
@font-face{
font-family:"Symbol";
}
@font-face{
font-family:"Arial";
}
@font-face{
font-family:"黑体";
}
@font-face{
font-family:"Courier New";
}
@font-face{
font-family:"Wingdings";
}
@font-face{
font-family:"新宋体";
}
@font-face{
font-family:"Courier New CYR";
}
p.0{
margin:0pt;
margin-bottom:0.0001pt;
layout-grid-mode:char; text-align:justify;
font-size:10.5000pt; font-family:'Times New Roman'; }
div.Section0{
margin-top:72.0000pt;
margin-bottom:72.0000pt;
margin-left:90.0000pt;
margin-right:90.0000pt;
size:612.0000pt 792.0000pt;
}

文件拷贝的代码 C版

#include <stdio.h> 

int main(int argc, char* argv[]) 

printf("Hello World!/n"); 

FILE* in; 

FILE* out; 

in=fopen("d://1.txt","rb"); 

out=fopen("d://2.txt","wb"); 

char ch=fgetc(in); 

while(!feof(in)) 

  fputc(ch,out); 

  ch=fgetc(in); 

fclose(in); 

fclose(out); 

return 0; 

//////////////////////////////////////////////////////////////////////////////////////

【上篇】
【下篇】

抱歉!评论已关闭.