直接上代码,中间有说明
// readchar.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "fstream"
#include "string"
using namespace std;
int main(int argc, char* argv[])
{
string filename="E:\\c++\\readchar\\readtwo.txt";
ifstream fin;
fin.open(filename.c_str());
//string str;
//string **str={0};
//int str[20][20]={0};
int i=0;
//str=new char*[50];
//----------------------------------
/*
while((!fin.eof())&&(i<50) )
{
//str[i]=new char[3];
//memset(str[i],0,sizeof(char)*3);
//fin.read(str[i],3);
fin>>str;
//fin.getline(str[i],3,'\r');
//cout<<str.c_str ()<<endl;
i++;
}
*/
//int sentance[50];
//-----------------------------------------
/*
//把读到的数据放到数组中
int *sentance;
sentance =new int[50];
//fin.getline (sentance,20);
int strread;
string str;
for(i=0;i<20;i++)
{
//fin>>strread ;
//str[i][j]=strread;
//fin.read(sentance,2);
fin>>str;
sentance[i]=atoi(str.c_str ());
cout<<sentance[i]<<endl;
}
*/
//------------------------------
/*
//pdata是个二维数组,在读文件时候,是一边读一边动态申请的,这个比较好
pData=new UCHAR *[height];
for(int i=0;i<height;i++){
pData[i]=new UCHAR[width];
memset(pData[i],0,width*sizeof(UCHAR));
fin.read((char *)pData[i],width);
//for(int j=0;j<width;j++) pData[i][j]=(unsigned int)buffer[j];
}
*/
//-----------------------------
/*
//fin.read把文件里所有的数都读出来,但是str[0]=f;str[1]=w;而文件里最后两个字符是wf
//不知道什么原因。这个过程应该是read读所有的字符,然后str里保存最后两位。但是最后两位的顺序,不知道为什么反了
char str[2];
while(!fin.eof ())
{
fin.read (str,2);
//cout<<str<<endl;
cout.write (str,fin.gcount ());
}
for(i=0;i<2;i++)
{
cout<<endl;
cout<<i<<":";
cout<<str[i]<<endl;
}
*/
//-----------------------------------
/*
//每一次循环,仅仅读一个字符,包括空格也在内。onechar里存放了所有的字符和空格,一个元素一个字符或者空格
char onechar[20];
while(!fin.eof())
{
onechar[i]=fin.get();
cout<<onechar[i]<<endl;
i++;
}
*/
//--------------------------
/*
//这个用法和get暂时不知道区别,只是getline可以把分隔符读进去,但是get不读
char twochar[20];
while(!fin.eof())
{
fin.getline (twochar,20);
cout<<twochar<<endl;
//i++;
}
for(i=0;i<20;i++)
{
cout<<twochar[i]<<endl;
}
*/
// delete sentance;
fin.close ();
//char *
//delete str;
return 0;
}