IO操作

以文本方式写入读出文件

/ 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; 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值