未格式化的 std::string 输入而不是二进制文件中的 c 字符串
好的,我有这个程序使用 C 字符串工作。我想知道是否可以将未格式化的文本块读入 std::string ?我玩弄了 if >>
但这是逐行读取的。我一直在尝试使用 std::string 来破坏我的代码并用头撞墙,所以我认为是时候聘请专家了。这是一个工作程序,您需要提供一个包含一些内容的文件“a.txt”才能运行。
我试图愚弄:
in.read (const_cast<char *>(memblock.c_str()), read_size);
但表现得很奇怪。我必须做 std::cout << memblock.c_str()
来打印它。并且 memblock.clear()
没有清除字符串。
不管怎样,如果你能想出一种使用STL的方法,我将不胜感激。
这是我使用 C 字符串的程序,
// What this program does now: copies a file to a new location byte by byte
// What this program is going to do: get small blocks of a file and encrypt them
#include <fstream>
#include <iostream>
#include <string>
int main (int argc, char * argv[])
{
int read_size = 16;
int infile_size;
std::ifstream in;
std::ofstream out;
char * memblock;
int completed = 0;
memblock = new char [read_size];
in.open ("a.txt", std::ios::in | std::ios::binary | std::ios::ate);
if (in.is_open())
infile_size = in.tellg();
out.open("b.txt", std::ios::out | std::ios::trunc | std::ios::binary);
in.seekg (0, std::ios::beg);// get to beginning of file
while(!in.eof())
{
completed = completed + read_size;
if(completed < infile_size)
{
in.read (memblock, read_size);
out.write (memblock, read_size);
} // end if
else // last run
{
delete[] memblock;
memblock = new char [infile_size % read_size];
in.read (memblock, infile_size % read_size + 1);
out.write (memblock, infile_size % read_size );
} // end else
} // end while
} // main
如果您发现任何可以使此代码变得更好的内容,请随时告诉我。
ok i have this program working using c-strings. I am wondering if it is possible to read in blocks of unformatted text to a std::string? I toyed arround with if >>
but this reads in line by line. I've been breaking my code and banging my head against the wall trying to use std::string, so I thought it was time to enlist the experts. Here's a working program you need to supply a file "a.txt" with some content to make it run.
i tried to fool around with:
in.read (const_cast<char *>(memblock.c_str()), read_size);
but it was acting odd. I had to do std::cout << memblock.c_str()
to get it to print. and memblock.clear()
did not clear out the string.
anyway, if you can think of a way to use STL I would greatly appreciate it.
Here's my program using c-strings
// What this program does now: copies a file to a new location byte by byte
// What this program is going to do: get small blocks of a file and encrypt them
#include <fstream>
#include <iostream>
#include <string>
int main (int argc, char * argv[])
{
int read_size = 16;
int infile_size;
std::ifstream in;
std::ofstream out;
char * memblock;
int completed = 0;
memblock = new char [read_size];
in.open ("a.txt", std::ios::in | std::ios::binary | std::ios::ate);
if (in.is_open())
infile_size = in.tellg();
out.open("b.txt", std::ios::out | std::ios::trunc | std::ios::binary);
in.seekg (0, std::ios::beg);// get to beginning of file
while(!in.eof())
{
completed = completed + read_size;
if(completed < infile_size)
{
in.read (memblock, read_size);
out.write (memblock, read_size);
} // end if
else // last run
{
delete[] memblock;
memblock = new char [infile_size % read_size];
in.read (memblock, infile_size % read_size + 1);
out.write (memblock, infile_size % read_size );
} // end else
} // end while
} // main
if you see anything that would make this code better please feel free to let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑使用
std::vector
,而不是使用std::string
;这可以让您解决对调用std::string::c_str()
的结果执行const_cast
的所有问题。在开始使用之前,只需将矢量调整为您需要的任何大小即可。如果要打印内容,可以通过将空终止符推到后面来以空终止符来终止向量的内容:
或者可以将其转换为
std::string
:这一切都假设您想要从二进制文件中读取一些文本块。如果您尝试打印出二进制字符,显然这是行不通的。从你的问题来看,这并不完全清楚。
Rather than using a
std::string
, consider using astd::vector<char>
; that gets you around all the problems with doing aconst_cast
on the result of callingstd::string::c_str()
. Just resize the vector to whatever size you need before you start using it.If you want to print the contents, you can null-terminate the contents of the vector by pushing a null-terminator onto the back:
or you can convert it into a
std::string
:This all assumes that you have some block of text that you want to read from a binary file. If you are trying to print out binary characters, this won't work, obviously. It wasn't entirely clear from your question.