读出磁盘的中txt文件内容中的所有单词并打印出来
#include<iostream>
#include<fstream>
using namespace std;
void read(char mode[]);
void main()
{ char mode[300];
read(mode);
}
void read(char mode[]){
long sum=0;
FILE *fp=fopen("f://data.txt","r");
while(fgetc(fp)!=EOF)
{
sum++;
}
fclose(fp);
cout<<sum<<endl;
ifstream infile("f://data.txt",ios::in);
if(!infile){cout<<"open error!/n";}
for(int i=0;i<sum;i++)
{ infile.read(&mode[i],sizeof(char));
if(mode[i]==' ')
{
cout<<endl;
}
else
{
cout<<mode[i];
}
}
cout<<endl;
infile.close();
}
读取二进制文件
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
ifstream in;
in.open("E://学习下载的资料//二进制数据//22.dat",ios_base::in|ios_base::binary);
char c;
while(1)
{
in >> c; // 读取
if( in.eof() )
break;//说明上次读取无效
cout << c;//
}
}
读取二进制文件
#include<iostream>
#include<fstream>
using namespace std;
void read(char mode[]);
void main()
{ char mode[300];
read(mode);
}
void read(char mode[]){
long sum=0;
FILE *fp=fopen("E://学习下载的资料//二进制数据//22.dat","r");
while(fgetc(fp)!=EOF)
{
sum++;
}
fclose(fp);
cout<<sum<<endl;
ifstream infile("E://学习下载的资料//二进制数据//22.dat",ios_base::in|ios_base::binary);
if(!infile){cout<<"open error!/n";}
for(int i=0;i<sum;i++)
{ infile.read(&mode[i],sizeof(char));
cout<<mode[i];
}
cout<<endl;
infile.close();
}