我正在尝试创建一个顺序文件,但它似乎不起作用。谁能解释如何让它在 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).
最佳答案
您不能使用 int
来存储 10 位数的电话号码,因为您可以存储的“最大”电话号码是(在 signed int
中) 2147483647
或(在 unsigned int
中)4294967295
。这些都不够大,无法存储区号为 503
、541
或 971
的电话号码。 Strings are probably best for storing phone numbers ,因为它们扩展到处理来自捷克共和国或美属萨摩亚等地的电话号码。
我也对使用 double
来存储数学、科学数据或物理模拟以外的任何东西持怀疑态度。我可能对此有点偏执,因为大多数在这方面使用 double
的应用程序大多都没有使用它,但是 double
会准确地存储数据,直到你稍微推到它们的外面边界,在这一点上它们确实非常近似地存储数据。
关于c++ - 创建顺序文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5215009/