创建顺序文件

时间:2011-03-07 01:52:36

标签: c++ file sequential

我正在尝试创建一个顺序文件,但它似乎不起作用。任何人都可以解释如何在MS Visual Studio 2010中使用它吗?

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

int main()
{
    ofstream outClientFile( "clients.dat", ios::out);

    if (!outClientFile)
    {
        cerr << "File could not be opened" << endl;
        exit(1);
    }

    cout << "Enter the Appointment Date, Time, Duration, Name," << endl
        << "Description, Contact Name, and Contact Number.\n? "; 

    int appDate, appTime, appContactNum;
    string appName, appDescription, appContactName;
    double appDuration;

    while ( cin >> appDate >> appTime >> appDuration >>
        appName >> appDescription >> appContactName >> appContactNum )
    {
        outClientFile << appDate << ' ' << appTime << ' ' << appDuration << ' ' << appName << ' ' << appDescription << ' ' << appContactName << ' ' << appContactNum << endl;
        cout << "? ";
    }
}

这是输入一行后的输出。

Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
The program '[452] CSC275 Assignment 3.exe: Native' has exited with code 0 (0x0).

1 个答案:

答案 0 :(得分:1)

您无法使用int存储10位数的电话号码,因为您可以存储的“最大”电话号码(signed int2147483647或({ {1}})unsigned int。这些都不足以存储我所在州的电话号码,区号为4294967295503541Strings are probably best for storing phone numbers,他们扩展到处理来自捷克共和国或美属萨摩亚的电话号码等等。

我也很谨慎使用971来存储除数学,科学数据或物理模拟之外的任何东西。我可能对此有点偏执,因为大多数在这方面使用double的应用程序大多都会使用它,但double会精确存储数据,直到你略微超出它们的界限,此时它们确实非常接近地存储数据。