从文件到字符串C ++读取任意长度的字节 [英] Read arbitrary-length bytes from file to string C++
问题描述
现在我对Python很熟悉,决定学习C ++,所以我非常n00b但肯定愿意学习。
我已经做了一个脚本从一个非常严格指定的文件格式(.EDF,医疗信号)读取,一个ascii头由字段大小定义。因此,我读取第一个字段的8个字节,第二个字段的80个字节,等等。
Now that I am quite familiar with Python, decided to learn C++, so I am very n00b but sure willing to learn. I had made a script to read from a very tightly-specified file format (.EDF, for medical signals), with an ascii header defined by field sizes in bytes. So, I read 8 bytes for the first field, 80 bytes for the second field, and so on.
我的工作python脚本如下:
My working python script is as follows:
## HEADER FIELD NAMES AND SIZES FROM EDF SPEC:
header_fields = (
('version', 8), ('patinfo', 80), ('recinfo', 80),
('start date', 8), ('start time', 8), ('header bytes', 8),
('reserved', 44), ('nrecs', 8), ('recduration', 8),
('nchannels', 4))
## TELL WHICH FILE TO OPEN
folder = os.path.expanduser('~/Dropbox/01MIOTEC/06APNÉIA/Samples')
f = open(folder + '/Osas2002plusQRS.rec', 'rb')
# READ FILE CONTENT TO DICTIONARY OF LABELLED FIELD CONTENTS,
# ALREADY STRIPPED FROM BLANK SPACES
header = {}
for key, value in header_fields:
header[key] = f.read(value).strip()
最后的结果是'header'
The end result is 'header', a dictionary where each pair is a "labeled" string.
我目前的尴尬c ++代码,几乎可以工作打印到屏幕未刷新的字符串,这是:
My current awkward c++ code, which almost work printing to screen the unstripped strings, is this:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
static int header_bytes[] = {8,80,80,80,80,8,8,8,44,8,8,4};
static int header_bytes_len = sizeof(header_bytes)/sizeof(int);
static string header_fields[] =
{
"version",
"patinfo",
"recinfo",
"patinfo",
"recifo",
"start date",
"start time",
"header bytes",
"reserved",
"nrecs",
"rec duration",
"nchannels"
};
int main()
{
ifstream edfreader;
edfreader.open("/home/helton/Dropbox/01MIOTEC/06APNÉIA/Samples/Osas2002plusQRS.rec", ios::binary);
char * buffer = new char [80];
for (int n = 0; n<header_bytes_len; n++)
{
edfreader.read(buffer, header_bytes[n]);
buffer[header_bytes[n]] = '\0';
cout<<"'"<<buffer<<"'"<<endl;
}
return 0;
}
实际上,我从cplusplus复制粘贴main()的最后一部分.com论坛条目,只是为了获得某种输出,但实际上我想要的是将字段保存为字符串对象数组,或更好的数组指向字符串对象的指针。
我正在阅读C ++ Primer,但仍然在200多页,但我想严重地干扰一些c ++代码fiddling,所以如果任何人可以指向我一些方法或概念或读数,我会很高兴。
Actually, I copy-pasted the last part of main() from a cplusplus.com forum entry, just to get some kind of output, but actually what I wanted was to save the fields as an array of string objects, or better yet an array of pointers to string objects. I am reading "C++ Primer", but still in 200+ pages, but I want badly to fiddle with some c++ code fiddling, so if anyone could point me to some methods or concepts or readings, I would be very happy.
感谢阅读
推荐答案
假设没有空格除了填充字段,您可以使用以下方式将它们读入C ++字符串:
Assuming there are no spaces in the fields except for the padding, you can read them into C++ strings using:
/* Read field of n bytes */
std::string read_field(std::istream &edfreader, size_t n)
{
// there's no need for new;
// in fact, new may lead to a memory leak if you forget to delete
std::vector<char> buf(n);
// read as a sequence of bytes
edfreader.read(&buf.front(), n);
// find the first space or end of buffer
size_t end = 0;
while (end < n && buf[end] != ' ')
end++;
// make a string object from the buffer
return std::string(buf, end);
}
std :: string
为你分配内存;你可以使用它很像一个Python字符串,除了它是可修改的。
std::string
does memory allocation for you; you can use it pretty much like a Python string, except that it is modifiable.
这里唯一的假设是你的操作系统的字符集是一个超集且 edfreader 的异常处理已启用 code>。
The only assumptions made here are that your OS's character set is (a superset of) ASCII and that exception handling is on for edfreader
.
这篇关于从文件到字符串C ++读取任意长度的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!