![]() |
#2
习惯被动2012-03-19 13:02
|

#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"。代码里面有些错误不知道怎么改,希望高手能帮帮忙。先谢啦! #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;
}