如何将文件读入一个一维数组??紧急求助
怎么将存放在记事本文件当中的所有字符读入到一个字符型数组当中呢?
[解决办法]
#include <fstream>
using std::ifstream;
int main()
{
ifstream f( "f.txt " );
char a[ 100000 ];
int i = 0;
int c = f.get();
while ( !f.eof() )
{
a[ i++ ] = c;
c = f.get();
}
a[ i ] = '\0 ';
}
[解决办法]
定义 char a[ 100000 ]; 太不负责任了!!!
文件内容超过100000字符,那问题就大了!!!!
建议使用这样定义vector(向量)
vector <char> a;
然后采用
a.push_back(c)
插入每个字符
[解决办法]
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int length;
ifstream is;
is.open ( "g:\\c\\22.txt ",ios::binary );
is.seekg (0,ios::end);
length = is.tellg();
char * data = new char[length];
is.seekg(0,ios::beg);
int i=0;
while (!is.eof())
{
//cout < <(char)is.get();
data[i++] = is.get();
}
cout < <length;
return 0;
}
[解决办法]
针对believefym(feng) 内存泄漏呀!!!
使用了关键字new 一定要记得delete