int main()
{
string path = "c:\\encryption\\";
string searchPattern = "*.txt";
string fullSearchPath = path + searchPattern;
WIN32_FIND_DATA FindData;
HANDLE hFind;
hFind = FindFirstFile( fullSearchPath.c_str(), &FindData );
if( hFind == INVALID_HANDLE_VALUE )
{
cout << " !!! Error searching directory !!!" << endl;;
return -1;
}
do
{
string filePath = path + FindData.cFileName;
ifstream in( filePath.c_str() );
if( in )
{
//////////////////////// HERE IS PROBLEM I KNOW THAT BUT WHAT I DON'T KNOW
char s;
while (!in.eof())
{
in >> s;
cout << s << endl;
}
cout << "************************ File Completely Read *** **************************** " << endl;
///////////////////////////////////////////////////////////////////////
}
else
{
cout << " !!! Problem opening file !!!" << FindData.cFileName << "\n";
}
}
while( FindNextFile(hFind, &FindData) > 0 );
if( GetLastError() != ERROR_NO_MORE_FILES )
{
cout << " !!! Something went wrong during searching !!! " << endl;
}
return 0;
}
我正在一个一个地读取特定文件夹中的所有文件然后 对于每个文件逐个字符读取它..以上是我的努力 到目前为止,我被困在一个我想将空格读作的部分 嗯..我该怎么办?我应该包括什么?请给我一些建议
最佳答案
>>
和 <<
运算符专为格式化 操作而设计,而您需要二进制 操作。尝试使用 ios::binary
打开流:
ifstream in( filePath.c_str(), ios::binary );
并使用 <a href="http://www.cplusplus.com/reference/istream/istream/read/" rel="noreferrer noopener nofollow">read</a>
(或 <a href="http://www.cplusplus.com/reference/istream/istream/readsome/" rel="noreferrer noopener nofollow">readsome</a>
)和 <a href="http://www.cplusplus.com/reference/ostream/ostream/write/" rel="noreferrer noopener nofollow">write</a>
处理 I/O。
如果你想坚持格式化操作(如果你喜欢加密,你不应该这样做),使用 <a href="http://www.cplusplus.com/reference/istream/istream/getline/" rel="noreferrer noopener nofollow">getline</a>
读取包含空白字符的行;
关于c++ - 如何在我的文件阅读中包含空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20334980/