如何在文件读取中包含空格

How to include white spaces in my file reading

本文关键字:包含 空格 读取 文件      更新时间:2023-10-16
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 );

并使用read(或readsome(和write来处理 I/O。

如果您想坚持格式化操作(如果您喜欢加密,则不应该这样做(,请使用 getline 读取包含空格字符的行;

使用 getline(( methood 来获取字符和空间这就是有效的方式。