Читаем C++ Primer Plus полностью

Finally, to display the revised file, the program uses seekg() to reset the file pointer to the beginning. Listing 17.20 shows the complete program. Don’t forget that it assumes that a planets.dat file created using the binary.cpp program is available.

Note

The older the implementation, the more likely it is to run afoul of the C++ Standard. Some systems don’t recognize the binary flag, the fixed and right manipulators, and ios_base.

Listing 17.20. random.cpp

// random.cpp -- random access to a binary file

#include      // not required by most systems

#include

#include

#include       // for exit()

const int LIM = 20;

struct planet

{

    char name[LIM];     // name of planet

    double population;  // its population

    double g;           // its acceleration of gravity

};

const char * file = "planets.dat";  // ASSUMED TO EXIST (binary.cpp example)

inline void eatline() { while (std::cin.get() != '\n') continue; }

int main()

{

    using namespace std;

    planet pl;

    cout << fixed;

// show initial contents

    fstream finout;     // read and write streams

    finout.open(file,

           ios_base::in | ios_base::out | ios_base::binary);

    //NOTE: Some Unix systems require omitting | ios::binary

    int ct = 0;

    if (finout.is_open())

    {

        finout.seekg(0);    // go to beginning

        cout << "Here are the current contents of the "

             << file << " file:\n";

        while (finout.read((char *) &pl, sizeof pl))

        {

            cout << ct++ << ": " << setw(LIM) << pl.name << ": "

                 << setprecision(0) << setw(12) << pl.population

                 << setprecision(2) << setw(6) << pl.g << endl;

        }

        if (finout.eof())

            finout.clear(); // clear eof flag

        else

        {

            cerr << "Error in reading " << file << ".\n";

            exit(EXIT_FAILURE);

        }

    }

    else

    {

        cerr << file << " could not be opened -- bye.\n";

        exit(EXIT_FAILURE);

    }

// change a record

    cout << "Enter the record number you wish to change: ";

    long rec;

    cin >> rec;

    eatline();              // get rid of newline

    if (rec < 0 || rec >= ct)

    {

        cerr << "Invalid record number -- bye\n";

        exit(EXIT_FAILURE);

    }

    streampos place = rec * sizeof pl;  // convert to streampos type

    finout.seekg(place);    // random access

    if (finout.fail())

    {

        cerr << "Error on attempted seek\n";

        exit(EXIT_FAILURE);

    }

    finout.read((char *) &pl, sizeof pl);

    cout << "Your selection:\n";

    cout << rec << ": " << setw(LIM) << pl.name << ": "

         << setprecision(0) << setw(12) << pl.population

         << setprecision(2) << setw(6) << pl.g << endl;

    if (finout.eof())

        finout.clear();     // clear eof flag

    cout << "Enter planet name: ";

    cin.get(pl.name, LIM);

    eatline();

    cout << "Enter planetary population: ";

    cin >> pl.population;

    cout << "Enter planet's acceleration of gravity: ";

    cin >> pl.g;

    finout.seekp(place);    // go back

    finout.write((char *) &pl, sizeof pl) << flush;

    if (finout.fail())

    {

        cerr << "Error on attempted write\n";

        exit(EXIT_FAILURE);

    }

// show revised file

    ct = 0;

    finout.seekg(0);            // go to beginning of file

    cout << "Here are the new contents of the " << file

         << " file:\n";

    while (finout.read((char *) &pl, sizeof pl))

    {

        cout << ct++ << ": " << setw(LIM) << pl.name << ": "

             << setprecision(0) << setw(12) << pl.population

             << setprecision(2) << setw(6) << pl.g << endl;

    }

    finout.close();

    cout << "Done.\n";

    return 0;

}

Here’s a sample run of the program in Listing 17.20, based on a planets.dat file that has had a few more entries added since you last saw it:

Here are the current contents of the planets.dat file:

0:                Earth:   6928198253  9.81

1:        Jenny's World:     32155648  8.93

2:              Tramtor:  89000000000 15.03

3:              Trellan:      5214000  9.62

4:            Freestone:   3945851000  8.68

5:            Taanagoot:    361000004 10.23

6:                Marin:       252409  9.79

Enter the record number you wish to change: 2

Your selection:

2:              Tramtor:  89000000000 15.03

Enter planet name: Trantor

Enter planetary population: 89521844777

Enter planet's acceleration of gravity: 10.53

Here are the new contents of the planets.dat file:

0:                Earth:   6928198253  9.81

1:        Jenny's World:     32155648  8.93

2:              Trantor:  89521844777 10.53

3:              Trellan:      5214000  9.62

4:            Freestone:   3945851000  8.68

5:            Taanagoot:    361000004 10.23

6:                Marin:       252409  9.79

Done.

By using the techniques in this program, you can extend it so that it allows you to add new material and delete records. If you were to expand the program, it would be a good idea to reorganize it by using classes and functions. For example, you could convert the planet structure to a class definition; then you could overload the << insertion operator so that cout << pl displays the class data members formatted as in the example. Also the example doesn’t bother to verify input, so you could add code to check for numeric input where appropriate.

Working with Temporary Files

Перейти на страницу:

Все книги серии Developer's Library

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных