C++ 使用fstream读取每个字符,包括空格和换行符

C++ 使用fstream读取每个字符,包括空格和换行符,c++,newline,spaces,fstream,C++,Newline,Spaces,Fstream,我想使用fstream读取txt文件 我正在使用infle>>characterToConvert,但问题是这会忽略任何空格和换行符 我正在写一个加密程序,所以我需要包括空格和换行符 什么是实现这一点的正确方法? < P>下面的C++代码将读取整个文件… #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string

我想使用
fstream
读取txt文件

我正在使用
infle>>characterToConvert
,但问题是这会忽略任何空格和换行符

我正在写一个加密程序,所以我需要包括空格和换行符


什么是实现这一点的正确方法?

< P>下面的C++代码将读取整个文件…


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
  string line;
  ifstream myfile ("foo.txt");

  if (myfile.is_open()){

    while (!myfile.eof()){
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }
  return 0;
}

#包括
#包括
#包括
使用名称空间std;
int main()
{
弦线;
ifstream myfile(“foo.txt”);
如果(myfile.is_open()){
而(!myfile.eof()){
getline(myfile,line);

cout对于加密,最好以二进制模式打开文件。使用类似的方法将文件的字节放入向量:

std::ifstream ifs("foobar.txt", std::ios::binary);

ifs.seekg(0, std::ios::end);
std::ifstream::pos_type filesize = ifs.tellg();
ifs.seekg(0, std::ios::beg);

std::vector<char> bytes(filesize);

ifs.read(&bytes[0], filesize);
std::ifstream-ifs(“foobar.txt”,std::ios::binary);
ifs.seekg(0,标准::ios::结束);
std::ifstream::pos_type filesize=ifs.tellg();
如果seekg(0,标准::ios::beg);
std::向量字节(文件大小);
ifs.read(&字节[0],文件大小);

编辑:根据评论修复了一个细微的错误。

我还没有测试过这个,但我相信您需要清除“跳过空白”标志:

我使用C++流的以下引用:


对于加密,您可能应该使用。加密算法通常处理固定大小的块。哦,要以二进制模式打开(无翻译frmo\n\r到\n),请将ios_base::binary作为第二个参数传递给构造函数或open()调用。

您可以调用,它将从流中读取单个字符。您还可以使用,它在多个字符上执行与
get()
相同的操作。给定的链接包括使用每个方法的示例


我还建议以二进制模式进行读写。这样可以正确地从文件中读取ASCII控制字符并将其写入文件。否则,加密/解密操作对可能会导致文件不完全相同。为此,请使用标志打开文件流。对于二进制文件,您要使用
read()
方法。

最好的方法可能是将整个文件的内容读入字符串,使用ifstream的方法可以很容易地完成:

std::ifstream-in(“myfile”);
std::stringstream缓冲区;

bufferistream层的许多好处是为简单类型和流提供基本的格式化和解析。就您所描述的目的而言,这些都不是很重要,您只对作为字节流的文件感兴趣

出于这些目的,您最好只使用filebuf提供的基本_streambuf接口。“跳过空白”行为是istream接口功能的一部分,您不需要它

filebuf是ifstream的基础,但直接使用它是完全有效的

std::filebuf myfile;
myfile.open( "myfile.dat", std::ios_base::in | std::ios_base::binary );

// gets next char, then moves 'get' pointer to next char in the file
int ch = myfile.sbumpc();

// get (up to) the next n chars from the stream
std::streamsize getcount = myfile.sgetn( char_array, n );
还可以查看函数snextc(向前移动“get”指针,然后返回当前字符)、sgetc(获取当前字符,但不移动“get”指针)和sungetc(如果可能,将“get”指针备份一个位置)


当您不需要istream类提供的任何插入和提取运算符,而只需要基本的字节接口时,streambuf接口(filebuf,stringbuf)通常比istream接口(ifstream,istringstream)更合适

< P>正如Charles Bailey正确指出的,你不需要FSCAN的服务只是为了读取字节。所以忘记这个流的愚蠢,使用fOpen/Fad并完成它。C STDIO是C++的一部分,你知道;

< P>简单< /P>
#include <fstream>
#include <iomanip>


ifstream ifs ("file");
ifs >> noskipws
#包括
#包括
ifs流ifs(“文件”);
ifs>>noskipws
仅此而已。

std::ifstream ifs(“filename.txt”);
std::ifstream ifs( "filename.txt" );

std::string str( ( std::istreambuf_iterator<char>( ifs ) ), 
                 std::istreambuf_iterator<char>()
               );
std::string str((std::istreambuf_迭代器(ifs)), std::istreambuf_迭代器() );
ifstream ifile(路径);
字符串内容((std::istreambuf_迭代器(ifile)),std::istreambuf_迭代器();
ifile.close();

另一个更好的方法是使用istreambuf_迭代器,示例代码如下:

ifstream inputFile("test.data");

string fileData(istreambuf_iterator<char>(inputFile), istreambuf_iterator<char>());
ifstream输入文件(“test.data”);
字符串文件数据(istreambuf_迭代器(inputFile),istreambuf_迭代器());
< /代码> 我也发现IFFAST对象的方法也可以读取文件的所有字符,不需要unSETP>Cudio:Std::IsOxBase::SkIPWS/C++代码。
一些未格式化的操作一次处理一个字节的流。表17.19中描述的这些操作读取而忽略空白

这些操作如下所示: 和

以下是最低工作代码

#include <iostream>
#include <fstream>
#include <string>

int main(){
    std::ifstream in_file("input.txt");

    char s;

    if (in_file.is_open()){
        int count = 0;
        while (in_file.get(s)){

            std::cout << count << ": "<< (int)s <<'\n';
            count++;
        }

    }
    else{
        std::cout << "Unable to open input.txt.\n";
    }
    in_file.close();

    return 0;
 }
程序的输出为:

0: 97
1: 98
2: 32
3: 99
4: 100
5: 10
6: 101
7: 102
8: 32
9: 103
10: 104
11: 32
12: 10

10和32是换行符和空格字符的十进制表示形式。显然,所有字符都已被读取。

但使用流时,流不必只是一个文件。它可以是字符串、套接字或任何您喜欢的东西。streambuf_iteratorI同意,但OP明确表示目标是“读取txt文件”。抱歉,这有一个微妙的错误。将调用只占用一个偏移量的“seekg”重载,而您最终只能读取两个字节(std::ios:end的值)。您应该调用seekg(0,std::ios::end)和seekg(0,std::ios::beg)。是的,如果您想读取二进制数据,确实需要添加此项,因为它在默认情况下处于打开状态。除了ios::binary标志之外,我也需要此项。您的意思是“ifs>>noskipws>>some_string”吗?使用good()比使用eof()更好。如果(myfile>>ch)需要逐字符读取,则是正确的方法,而检查eof则不是。请参考:我如何才能更进一步。这将为我节省数小时的编码时间…你会被这个网站上存在的大量错误建议(即使是有经验的堆栈用户)所震惊。
ifstream inputFile("test.data");

string fileData(istreambuf_iterator<char>(inputFile), istreambuf_iterator<char>());
#include <iostream>
#include <fstream>
#include <string>

int main(){
    std::ifstream in_file("input.txt");

    char s;

    if (in_file.is_open()){
        int count = 0;
        while (in_file.get(s)){

            std::cout << count << ": "<< (int)s <<'\n';
            count++;
        }

    }
    else{
        std::cout << "Unable to open input.txt.\n";
    }
    in_file.close();

    return 0;
 }
ab cd
ef gh
0: 97
1: 98
2: 32
3: 99
4: 100
5: 10
6: 101
7: 102
8: 32
9: 103
10: 104
11: 32
12: 10