注册 登录
编程论坛 C++教室

C++将文本文件中所有数字减100再输出在新的文本文件中,求大神检查

yumiao910 发布于 2013-05-05 15:46, 754 次点击
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    int i,a[21];;
    float sum;
    ifstream infile("1.txt", ios::in); // 定义输入文件流对象,以输入方式打开磁盘文件1.txt
    if(! infile)
    {
        cerr<<"open 1.txt error!"<<endl;
        exit(1);
    }
    ofstream outfile("2.txt", ios::out);
    if(! outfile)
    {
        cerr<<"open 2.txt error!"<<endl;
        exit(1);
    }
    for(i=0;i<21;i++)  //有多少数字 就将i<多少
    {
        infile>>a[i];
        sum=a[i]-100;
        outfile.put(sum);
    }
    cout<<endl;
    infile.close();
    outfile.close();
    return 0;
}
为什么我输出新文件中是乱码?是不是我定义的字符格式不对求解
新手学习中  只是自己笨拙的想法希望大家帮忙修改
3 回复
#2
rjsp2013-05-06 08:56
主要问题在 outfile.put(sum); 上

程序代码:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream infile( "1.txt" );
    ofstream outfile( "2.txt" );
    if( !infile || !outfile )
    {
        cerr << "open 1.txt error!\n";
        return 1;
    }

    for( int a; infile>>a; )
        outfile << (a-100) << ' ';

    return 0;
}

#3
yumiao9102013-05-09 21:18
回复 2楼 rjsp
谢谢您  您看我这写的  我汗颜  多向您学习
#4
ytiantian2013-05-10 09:08
文件流是我一直搞不定的问题。
1