简体   繁体   English

如何在C ++中使用写入系统调用?

[英]How do you use the Write System Call in C++?

I'm trying to write to a file and want to use Write System Call to make it faster. 我正在尝试写入文件,并希望使用“写入系统调用”来使其更快。 Basically I have a QVector and I want to store the results in a file. 基本上我有一个QVector,我想将结果存储在一个文件中。 Originally I was just iterating through the array but it's too slow. 本来我只是在遍历数组,但是它太慢了。 So I did some research and found something called Write System Call , but I can't figure out how to set up the code. 所以我做了一些研究,发现了一个叫做Write System Call的东西,但是我不知道如何设置代码。

This is what I have tried so far: 到目前为止,这是我尝试过的:

/*Header File*/

QVector<unsigned short> rawData;

/*Implementation File*/

int fd = open("output.txt", O_WRONLY)L
write(fd, &rawData, rawData.size());
close(fd);

While the above code does not crash on me it doesn't actually write anything to the output file. 虽然上面的代码对我没有崩溃,但实际上并没有向输出文件写入任何内容。 Any ideas what I'm doing wrong? 有什么想法我做错了吗?

EDIT: 编辑:

Using fwrite I am able to write to the file but the data in the file is some strange Unicode. 使用fwrite我可以写入文件,但是文件中的数据是一些奇怪的Unicode。 Basically it's not number which is what I'm trying to get. 基本上,这不是我要获取的数字。 Here is what I'm doing: 这是我在做什么:

FILE * pFile;

pfile = fopen("pixelValues.txt", "wb");

fwrite(&rawData, sizeof(unsigned short), sizeof(rawData), pFile);

fclose(pFile);

There is a difference between QVector the object and the elements inside it. QVector对象和其中的元素之间有区别。 You probably want to write the QVector elements, so in order to do that you need something like this: 您可能想编写QVector元素,因此要执行以下操作:

template<typename T>
int writeVector(const QVector<T>& vec, int fd)
{
    // note: the size argument to write() is about the number of bytes
    // which is: (size of each element) * (number of elements to write) 
    return write(fd, vec.constData(), (sizeof(T)) * vec.size());
}

For a better solution, consider using a QDataStream instead: 为了获得更好的解决方案,请考虑使用QDataStream代替:

template<typename T>
void writeVectorBetter(QDataStream& stream, const QVector<T>& vec)
{
    stream << vec;
}

This will also ensure you can read the information back from the file in a consistent manner. 这还将确保您可以以一致的方式从文件读回信息。

You're assuming that you need to write your data directly using system calls. 您假设您需要使用系统调用直接写入数据。 Without seeing your code I'd argue that it probably is not the case. 在没有看到您的代码的情况下,我认为情况可能并非如此。 Most likely you were doing something very obnoxiously inefficient. 您最有可能做的是非常低效的操作。

You can do better: In Qt, the simplest way would be to leverage QDataStream . 您可以做得更好:在Qt中,最简单的方法是利用QDataStream It will do everything necessary to give you the same vector back when you read the data later. 以后读取数据时,它将做所有必要的工作以使您返回相同的向量。 It perfoms quite adequately: 它表现得相当充分:

bool write(const QString &filename, const QVector<uint16_t> &data)
{
   QSaveFile f(filename);
   if (!f.open(QIODevice::WriteOnly))
      return false;
   QDataStream s(&f);
   s.setVersion(QDataStream::Qt_5_6);
   s << data;
   return f.commit();
}

QVector<uint16_t> read(const QString &filename)
{
   QVector<uint16_t> data;
   QFile f(filename);
   if (!f.open(QIODevice::ReadOnly))
      return data;
   QDataStream s(&f);
   s.setVersion(QDataStream::Qt_5_6);
   s >> data;
   if (s.status() != QDataStream::Ok)
      data.clear();
   return data;
}

Other issues: 其他事宜:

  1. The file is not a text file, so don't give it a .txt extension. 该文件不是文本文件,因此请勿给其扩展名.txt

  2. Use types with explicit sizes, such as uint16_t or quint16 instead of unsigned short . 使用显式大小的类型,例如uint16_tquint16而不是unsigned short

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM