从文件中读取某些整数,就像从VB6中一样
C++ Read certain integers from file as from VB6
我想使用c++从文件中提取一些整数,但我不确定我是否做得正确。
我的VB6代码如下:
Redim iInts(240) As Integer
Open "m:devvoice.raw" For Binary As #iFileNr
Get #iReadFile, 600, iInts() 'Read from position 600 and read 240 bytes
我向c++的转换如下:
vector<int>iInts
iInts.resize(240)
FILE* m_infile;
string filename="m://dev//voice.raw";
if (GetFileAttributes(filename.c_str())==INVALID_FILE_ATTRIBUTES)
{
printf("wav file not found");
DebugBreak();
}
else
{
m_infile = fopen(filename.c_str(),"rb");
}
但现在我不知道如何从那里继续,我也不知道"rb"是否正确。
我不知道VB如何读取文件,但如果您需要从文件中读取整数,请尝试:
m_infile = fopen(myFile, "rb")
fseek(m_infile, 600 * sizeof(int), SEEK_SET);
// Read the ints, perhaps using fread(...)
fclose(myFile);
或者您可以使用c++的方式使用ifstream。
带有流的完整示例(注意,您应该添加错误检查):
#include <ifstream>
void appendInts(const std::string& filename,
unsigned int byteOffset,
unsigned int intCount,
const vector<int>& output)
{
std::ifstream ifs(filename, std::ios::base::in | std::ios::base::binary);
ifs.seekg(byteOffset);
for (unsigned int i = 0; i < intCount; ++i)
{
int i;
ifs >> i;
output.push_back(i);
}
}
...
std::vector<int> loadedInts;
appendInts("myfile", 600, 60, loadedInts);
使用整型数组代替矢量,并传递poth文件描述符和数组指针给函数read()
如下
...
int my_integers[240];
read(m_infile, my_integers, 240, 600);
..
有关read()
的更多信息,请参阅http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html
相关文章:
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 为什么在C++中对链表这样做?(像堆叠一样处理它们)
- 堆栈和队列是否像C++中的数组一样传递?
- 是否有技术原因阻止 Java 中的 final C++ 像 const 一样严格?
- 访问数据成员(本身是对象)的数据成员,就好像它们是类成员一样
- 我们可以将集合的值存储在变量中吗?就像我们可以将数组的值存储在变量中一样
- 我如何在C++像在 Python 中一样实现 f 字符串?
- 自制的上衣:看起来一样,但不完全相同
- 如何使用 redis-plus-plus 存储二进制数据,就像我想存储结构一样?@for_stack?
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 像 CPP 中的控制台一样的应用程序
- 为什么C++中没有常量引用,就像常量指针一样?
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 有没有像给定的一样的 std::optional_function
- Java 是否像C++模板一样具有泛型推论?
- 记忆栅栏和记忆屏障是一样的吗
- 对象作为参数传递,就好像我们正在传递构造函数值一样
- 像自定义类一样构造的指针(内置类型)如何工作?
- 从文件中读取某些整数,就像从VB6中一样