c++ - 如何在我的文件阅读中包含空格

标签 c++ file-handling

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/

相关文章:

c++ - 使错误 : Missing Includes in C++ Script?

c++ - 将元素添加到未存储的 c++ 类中的 vector

c++ - 在 seekg() 上读取文件时崩溃

c++ - 从文件C++读取时发生无限循环

java - file.getPath() 的相对路径

c - 停在文件末尾

c++ - 检查下一个元素是否是 STL 列表中的最后一个元素

c++ - 许多具有相同构造函数的类

C++ ofstream 不在程序的后续运行中创建和写入文件

c++ - 链接器错误 undefined reference