Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将文本文件读入字符数组。C++;文件流_C++_Arrays_Char_Ifstream - Fatal编程技术网

C++ 将文本文件读入字符数组。C++;文件流

C++ 将文本文件读入字符数组。C++;文件流,c++,arrays,char,ifstream,C++,Arrays,Char,Ifstream,我试图将整个file.txt读入一个字符数组。但有一些问题,请提出建议=] ifstream infile; infile.open("file.txt"); char getdata[10000] while (!infile.eof()){ infile.getline(getdata,sizeof(infile)); // if i cout here it looks fine //cout << getdata << endl; } //but

我试图将整个file.txt读入一个字符数组。但有一些问题,请提出建议=]

ifstream infile;
infile.open("file.txt");

char getdata[10000]
while (!infile.eof()){
  infile.getline(getdata,sizeof(infile));
  // if i cout here it looks fine
  //cout << getdata << endl;
}

 //but this outputs the last half of the file + trash
 for (int i=0; i<10000; i++){
   cout << getdata[i]
 }
ifstream-infle;
infle.open(“file.txt”);
char getdata[10000]
而(!infle.eof()){
getline(getdata,sizeof(infle));
//如果我不能在这里,它看起来很好

//cout每次读取新行时,都会覆盖旧行。保留一个索引变量i并使用
infle.read(getdata+i,1)
然后递增i。

如果计划将整个文件吸入缓冲区,则不需要逐行读取

char getdata[10000];
infile.read(getdata, sizeof getdata);
if (infile.eof())
{
    // got the whole file...
    size_t bytes_really_read = infile.gcount();

}
else if (infile.fail())
{
    // some other error...
}
else
{
    // getdata must be full, but the file is larger...

}
std::ifstream-infle;
open(“Textfile.txt”,std::ios::binary);
填充seekg(0,标准::ios::结束);
size_t file_size_in_byte=infle.tellg();
std::vector data;//用于存储文本数据
调整大小(文件大小以字节为单位);
填充seekg(0,标准::ios::beg);
infle.read(和数据[0],文件大小(以字节为单位);

使用
std::string

std::string contents;

contents.assign(std::istreambuf_iterator<char>(infile),
                std::istreambuf_iterator<char>());
std::字符串内容;
contents.assign(std::istreambuf_迭代器(infle),
std::istreambuf_迭代器();

您可以使用Tony Delroy的答案并加入一个小函数来确定文件的大小,然后创建该大小的
字符数组,如下所示:

//Code from Andro in the following question: https://stackoverflow.com/questions/5840148/how-can-i-get-a-files-size-in-c

int getFileSize(std::string filename) { // path to file
    FILE *p_file = NULL;
    p_file = fopen(filename.c_str(),"rb");
    fseek(p_file,0,SEEK_END);
    int size = ftell(p_file);
    fclose(p_file);
    return size;
}
然后你可以这样做:

//Edited Code From Tony Delroy's Answer
char getdata[getFileSize("file.txt")];
infile.read(getdata, sizeof getdata);

if (infile.eof()) {
    // got the whole file...
    size_t bytes_really_read = infile.gcount();
}
else if (infile.fail()) {
    // some other error...
}

或者也许有人可以建议一种更好的方法将文本文件存储到字符数组中。如果你在玩具应用程序以外的任何应用程序中执行此操作,请确保对无限内存分配进行保护。你似乎缺少一些分号。
read(…,1)
一次读取一个字符…效率非常低。infle.seekg(0,ios::end);int len=infle.peek();infle.seekg(0,ios::beg);infle.read(getdata,len);如果文件大于
10000
chars?…该怎么办?声明另一个更大的char数组并再次读取?错了,不是吗?…总是很难记住这个拼写。不是很直观。PS.因为OP请求了char数组,
contents.c_str()
可能有用。
//Edited Code From Tony Delroy's Answer
char getdata[getFileSize("file.txt")];
infile.read(getdata, sizeof getdata);

if (infile.eof()) {
    // got the whole file...
    size_t bytes_really_read = infile.gcount();
}
else if (infile.fail()) {
    // some other error...
}