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

关于文件的读写

习惯被动 发布于 2012-03-18 14:32, 322 次点击
帮忙改一下这个程序
程序代码:
#include <string>
#include <iostream>
#include<stdio.h>
using namespace std;

int main() {
    string str,inputFile;
    FILE *fp1,*fp2;
    char ch;
   
    if((fp2=fopen("mingwen.txt","w+"))==NULL)
    cout<<"can not open this file.\n";
    cout<<"Enter the file path: ";
    cin>>inputFile;    //输入txt文件路径
    cout<<endl;
    infile(inputFile.c_str());    //infile按照指定路径读取txt文件
    fp1=inputFile.c_str();
    fscanf(fp1,"%c",str.c_str );
    string::iterator iter = str.begin();
    while (iter != str.end()) {
        ch = *iter++;
        for (int i = 0; i < 8; ++i)
          fprintf(fp2, "%d", ((ch >> i) & 1));
    }
    fclose(fp1);
    fclose(fp2);
    cout<<endl;
    return 1;
}
这个程序是用来读入一个文件inputFile(.txt),把文件inputFile里面的字符(包括文字)转换成2进制(如把A转换成1000100)保存到文件"mingwen.txt"。代码里面有些错误不知道怎么改,希望高手能帮帮忙。先谢啦!
3 回复
#2
习惯被动2012-03-19 13:02
求回复啊,急!
#3
nicum2012-03-20 19:16
用fstream吧
#4
习惯被动2012-03-20 20:03
呃,那个不熟,能不能帮忙改一下。
1