在C ++中使用Direct Access文件 [英] Working with Direct Access Files in C++

查看:133
本文介绍了在C ++中使用Direct Access文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++(和一般的程序设计)是非常新的,我正在开发一个项目,让我厌倦了(不难做)。该项目涉及直接访问文件。我们要创建一个由一系列零件记录组成的文件。以下是一些规范:

I am extremely new to C++ (and programming in general really) and am working on a project that has me stumped (not hard to do ha). The project involves Direct Access Files. We are to create a file consisting of a series of parts records. Here are some of the specifications:


应包含标头记录(24个字节 - 填充),表示
有效项目数

Should contain a header record (24 bytes - filled) indicating the number of valid items.

每个(24字节长)数据记录将包含一个
库存编号(最多4位),一个描述(最多8个字符)
(4位数字)和一个测试部分指示器(4位数字最多=> -1 -
文件的结尾)。

Each (24 byte-long) data record will contain a stock number (4 digits max), a description (8 characters max), a count (4 digits), and a "test part" indicator( 4 digits max => -1 - end of file).

文件将最初保存20个空白(虚拟)记录,并将
按顺序创建。

This file will initially hold 20 blank (dummy) records and will be created sequentially.

文件创建后,更新
顺序文本

Once the file has been created, an update sequential text file will be accessed based on stock numbers and new records will be inserted into the file.

当更新完成后,
的有效部分将被写入文件。

When the update is complete, the valid parts records will be printed in order starting at stock record "1".

通过读取文本更新文件(prog4.dat)和
更新根据库存编号寻找文件位置(不要忘记
头记录)

Update by reading the text update file (prog4.dat) and seeking the file position based on the stock number (don't forget the header record)

例如:

Initial (empty)
Input (updates)
    1 widgits 25 3
    6 gidgits 12 8
    8 kidgits 6 -1
    3 didgits 11 6
Output
    1 widgits 25
    3 didgits 11
    6 gidgits 12
    8 kidgits 6

我完全不知道Direct Access文件,所以一直在看一些不同的链接, ( http://cee-ux49.cee。 illinois.edu/cee490/public_html/pdfs_vgs/aL23_Direct_Access_Files.pdf http://www.learncpp.com/cpp-tutorial/137-random-file-io/ ),但我无法弄清楚如何使这个工作为这个特定的程序。

I know absolutely nothing about Direct Access files, so have been looking at a couple different links that I've found on Google (http://cee-ux49.cee.illinois.edu/cee490/public_html/pdfs_vgs/aL23_Direct_Access_Files.pdf , and http://www.learncpp.com/cpp-tutorial/137-random-file-io/ specifically), but am having trouble figuring out how to make this work for this particular program.

我没有做过太多的代码,因为像我说的,这让我失望,我做的是主要是基于第一个链接我不知道它是正确的(不知道什么大小给矢量,因为我不完全确定什么将是在我的具体问题(部分数组可能?),但这里是我一点点能够想出。

I haven't done much in the way of code since like I said, this has me stumped, and what I do have is based primarily off of that first link so I don't know that it's right (not sure what size to give vector since I'm not entirely sure what that would be in my specific problem (the array of parts perhaps?)), but here is what little I've been able to come up with.

#include <iostream>
#include <string>
#include <fstream>
#include <stream>
using namespace std;

class records {
public:
        int getStckNumber() {
                return stockNumber;
        }
        void setStockNumber(int stockNum) {
                stockNumber = stockNum;
        }

        string getItemDespcription() {
                return itemDescription;
        }
        void setItemDespcription(string itemDescrip) {
                itemDescription = itemDescrip;
        }

        int getItemAmount() {
                return itemAmount;
        }
        void setItemAmount(int itemAmt) {
                 itemAmount = itemAmt;
        }

        int getNext() {
                return next;
        }
        void setNext(int nxt) {
                next = nxt;
        }
private:
        int stockNumber;
        string itemDescription;
        int itemAmount;
        int next;
        int recNum;
}


int main() {
        int stockNumber;
        string itemDescription;
        int itemAmount;
        int next;
        int recNum;
        int recSize = sizeof(int) + sizeof(string) + sizeof(int) + sizeof(int) + sizeof(int);

        istream updateFile;
        updateFile.open("prog4.dat");
        if(!updateFile) {
                cerr << "Open Failure" << endl;
                exit(1);
        }
}

这是我将用于更新的文件:

Here is the file I will be using for the updates:

10 zidgits 17 -1
14 lidgits 2 7
6 gidgits 12 8
1 bidgits 25 3
16 widgits 9 10
7 midgits 0 2
3 didgits 11 6
5 tidgits 5 16
2 pidgits 7 5
8 kidgits 6 14

这里有一些具体问题:


  1. 我如何将来自updateFile的信息存储到要写入输出文件的变量中(尚未创建)?

  1. How would I go about storing the information from the updateFile to the variables to be written to the output file (hasn't been created yet)?

如何以正确的顺序写入数据,因为它基于updateFile中每行的最后一个数字。

How would I get it to write the data in the correct order since it's based on the last number on each line in the updateFile.

例如,输出文件应以最低stockNumber为1开始,因此基于文件,stockNumber为1的项为bidgits。然后该文件应该查看该行上的最后一个数字(3),并用该stockNumber(didgits)等编写项目的信息。

For example, the output file is supposed to start with the lowest stockNumber which is 1, so based on the file, the item with stockNumber 1 is bidgits. The file is then supposed to look at the last number on that line (3) and write the information for the item with that stockNumber (didgits) and so on.

是我现在跳出来的主要问题,但我肯定更多的将弹出,因为这进展。任何帮助将是非常感激。另外,这是约5小时,所以我试图尽可能贴近我已经有的代码(我知道更多的将被添加),如果可能的话。

Those are the main questions that jump out at me right now, but I'm sure more will pop up as this progresses. Any help would be GREATLY appreciated. Also, this is due in about 5 hours, so I'm trying to stick as closely as I can to the code I already have (I know much more will be added of course) if at all possible.

推荐答案

对于像这样的项目,我要写一些原语整个记录和更新文件头。

What I would to for a project like this is write some primitive functions for reading and writing whole records and updating the file header.

我将创建POD类型结构来存储要从数据文件读取或写入数据文件的单个记录。

I would create POD type structs to store individual records that are to be read from or written to the data files.

例如:

struct header
{
    uint32_t valid; // number of valid records
    char pad[20]; // padding to make this object 24 bytes
};

struct record
{
    char no[4]; // stock number
    char desc[8]; // description
    uint32_t count;
    uint32_t test_part;
    char pad[4];  // padding to make this object 24 bytes
};

写头的函数code> 0 ):

A function to write the header (which is always at file position 0):

std::iostream& write(std::iostream& ios, const header& h)
{
    ios.clear(); // clear any errors
    ios.seekg(0); // move to beginning of file
    ios.write(reinterpret_cast<const char*>(&h), sizeof(h)); // write the header to file
    return ios; // return the stream (for easy error detection/chaining)
}

在特定位置写入记录是一样的:

Same thing to write a record at a specific position:

std::iostream& write(std::iostream& ios, const record& r, size_t pos)
{
    ios.clear(); // clear any errors
    ios.seekg(sizeof(header) + (sizeof(record) * pos)); // move to record's position
    ios.write(reinterpret_cast<const char*>(&r), sizeof(r)); // write the record to file
    return ios; // return the stream (for easy error detection/chaining)
}

然后可以用这些原语写入初始化函数:

You can then write an initialization function in terms of those primitives:

std::iostream& init(std::iostream& ios, size_t num)
{
    // Update the header to zero records
    header h;
    h.valid = 0;
    write(ios, h);

    // create each record with a -1 (EOF) marker
    record r;
    r.test_part = uint32_t(-1);

    // output 20 copies of that record.
    for(size_t pos = 0; pos < num; ++pos)
        write(ios, r, pos);

    return ios;
}

然后调用它在一个真实的文件有点像这样

int main()
{
    assert(sizeof(header) == 24);
    assert(sizeof(record) == 24);

    // binary mode io!
    std::fstream fs("records.dat", std::ios::in|std::ios::out|std::ios::binary);

    if(!init(fs, 20))
    {
        std::cerr << "ERROR: initializing data file:" << std::endl;
        return 1;
    }

    // ...
}

注意:此代码匆忙写入并完全未经测试,仅作为示例说明如何处理此问题。希望它会给你一些想法。

NOTE: This code is hastily written and completely untested and merely presented as an example of how this problem can be approached. Hopefully it will give you some ideas.

ALSO:这样的二进制文件在系统之间甚至不同版本在同一平台上使用相同的编译器。

ALSO: Writing binary files like this is not very portable between systems or even between different versions of the same compiler on the same platform.

这篇关于在C ++中使用Direct Access文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆