未格式化的 std::string 输入而不是二进制文件中的 c 字符串

发布于 2024-09-01 23:09:11 字数 1621 浏览 7 评论 0原文

好的,我有这个程序使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅过去的我 2024-09-08 23:09:11

考虑使用 std::vector,而不是使用 std::string;这可以让您解决对调用 std::string::c_str() 的结果执行 const_cast 的所有问题。在开始使用之前,只需将矢量调整为您需要的任何大小即可。

如果要打印内容,可以通过将空终止符推到后面来以空终止符来终止向量的内容:

std::vector<char> v;
v.push_back('\0');
std::cout << &v[0];

或者可以将其转换为 std::string

std::vector<char> v;
std::string s(v.begin(), v.end());

这一切都假设您想要从二进制文件中读取一些文本块。如果您尝试打印出二进制字符,显然这是行不通的。从你的问题来看,这并不完全清楚。

Rather than using a std::string, consider using a std::vector<char>; that gets you around all the problems with doing a const_cast on the result of calling std::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:

std::vector<char> v;
v.push_back('\0');
std::cout << &v[0];

or you can convert it into a std::string:

std::vector<char> v;
std::string s(v.begin(), v.end());

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文