C++ ifstream读取整个流的随机字符
我试图实现清单5.1中的函数 但是当从文件中读取数据复制到缓冲区时,我只得到整个数组的相同字符(Í),其中string.txt是前一个链接内容的复制和粘贴。 这是我的密码:C++ ifstream读取整个流的随机字符,c++,C++,我试图实现清单5.1中的函数 但是当从文件中读取数据复制到缓冲区时,我只得到整个数组的相同字符(Í),其中string.txt是前一个链接内容的复制和粘贴。 这是我的密码: #include <iostream> #include <fstream> #include <string> #include <cinttypes> #include <cstdio> #include <cstring> const int b
#include <iostream>
#include <fstream>
#include <string>
#include <cinttypes>
#include <cstdio>
#include <cstring>
const int block_size = 0x4000; //16KB
int search(char* buffer, int searchLength, char* stringToSearch, int stringToSearchLength) {
char * potentialMatch;
while (searchLength) {
potentialMatch = reinterpret_cast<char *>(memchr(buffer, *stringToSearch, searchLength));
if (potentialMatch == NULL)
break;
if (stringToSearchLength == 1) {
return 1;
} else {
if (!memcmp(potentialMatch + 1, stringToSearch + 1, stringToSearchLength - 1))
return 1;
}
searchLength -= potentialMatch - buffer + 1;
buffer = potentialMatch + 1;
}
return 0;
}
int main(int argc, char* argv[]) {
char *toSearch = "Interpreting Where";
int done = 0;
int found = 0;
char *buffer;
int64_t fileSizeLeft = 0;
std::ifstream myFile("string.txt");
if (!myFile.fail()) {
buffer = new char[block_size];
myFile.seekg(0, std::ios::end); //Get file's size
fileSizeLeft = myFile.tellg();
} else {
std::cout << "Cannot open file" << std::endl;
return 1;
}
int toSearchLength = strlen(toSearch);
int stringLeft = toSearchLength - 1;
int first_time = 1;
while (!done && fileSizeLeft > toSearchLength) {
if (first_time) {
myFile.read(buffer, block_size);
found = search(buffer, block_size, toSearch, toSearchLength);
} else {
memcpy(buffer, buffer + stringLeft, stringLeft);
myFile.read(buffer+stringLeft, fileSizeLeft-stringLeft);
found = search(buffer, block_size, toSearch, toSearchLength);
}
fileSizeLeft = fileSizeLeft - block_size;
first_time = 0;
}
if (found) {
std::cout << "String found" << std::endl;
} else {
std::cout << "String not found" << std::endl;
}
myFile.close();
delete[] buffer;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
const int block_size=0x4000//16KB
int-search(char*buffer、int-searchLength、char*stringToSearch、int-stringToSearchLength){
字符*电位匹配;
while(搜索长度){
potentialMatch=reinterpret_cast(memchr(缓冲区,*stringToSearch,搜索长度));
if(potentialMatch==NULL)
打破
如果(stringToSearchLength==1){
返回1;
}否则{
如果(!memcmp(潜在匹配+1,字符串搜索+1,字符串搜索长度-1))
返回1;
}
searchLength-=potentialMatch-buffer+1;
缓冲区=电位匹配+1;
}
返回0;
}
int main(int argc,char*argv[]){
char*toSearch=“解释何处”;
int done=0;
int=0;
字符*缓冲区;
int64_t fileSizeLeft=0;
std::ifstream myFile(“string.txt”);
如果(!myFile.fail()){
缓冲区=新字符[块大小];
seekg(0,std::ios::end);//获取文件大小
fileSizeLeft=myFile.tellg();
}否则{
std::cout您正在将myFile
的位置设置为ios\u base::end
:
然后试着从中读出:
myFile.read(buffer, block_size);
显然不会读取任何数据,因为myFile
已经位于ios\u base::end
。您将读取缓冲区中已经存在的任何未初始化数据
您可能想做的是在阅读以下内容之前,将您的myFile
位置设置回起始位置:
myFile.seekg(0, ios::beg);
您正在使用以下命令将myFile
的位置设置为ios\u base::end
:
然后试着从中读出:
myFile.read(buffer, block_size);
显然不会读取任何数据,因为myFile
已经位于ios\u base::end
。您将读取缓冲区中已经存在的任何未初始化数据
您可能想做的是在阅读以下内容之前,将您的myFile
位置设置回起始位置:
myFile.seekg(0, ios::beg);
我看不到将文件内部位置返回到开头的部分,您可以看到文件的大小,并将位置保留在末尾。您似乎在读垃圾。Í是二进制值0xcd的文本解释,该值是Visual Studio的调试模式放入未初始化内存中的值。只需删除link,试试@EdMaster谢谢就是这样!把它作为一个答案发布,这样我就可以接受了:)!Hello@Aram:通常,对于非常小的问题,只写一条评论是不礼貌的。我看不到将文件内部位置返回到开头的部分,你可以看到文件的大小,并在结尾保留该位置。你似乎是e reading garbage.Í是二进制值0xcd的文本解释,它是Visual Studio的调试模式放入未初始化内存中的值。只需删除链接中的此处,尝试@EdMaster Thank Thank Thank Thank Thank it it!将其作为答案发布,以便我可以接受:)!Hello@Aram:通常,对于非常小的问题,放置只说一句话。