用于存储在文件中的单词的固定大小
fixed size for words for storing in a file
我有一个程序,它将一个人的名字和姓氏写入文件
当我打开文件查看其内容时,这些单词就在一起,没有任何空格。例如,如果输入是Bill和Gates,则这些单词存储在文件中,如下所示:
BillGates
但我想把它们存储为两个单独的单词,每个单词都有固定的字段长度。就像这样。
Bill Gates
--------------------
每个单词占用10个单位的空间。怎么做?这是节目。
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
class Person
{
public:
char fname[11];
char lname[11];
};
ostream & operator << (ostream & obj,Person & p)
{
obj << p.fname << p.lname;
return obj;
}
int main()
{
ofstream ofile("person.txt");
Person P;
cout << "Enter details n";
cin >> P.fname >> P.lname;
ofile << P;
ofile.close();
return 0;
}
您可以设置输出值的宽度。为了保持左边的格式,您可以设置输出流的格式标志。示例如下:
int main()
{
ofstream ofile("person.txt");
Person P;
cout << "Enter details n";
cin >> P.fname >> P.lname;
ofile.setf(ofile.left);
ofile.width(10);
ofile << P.fname;
ofile << P.lname;
ofile.close();
return 0;
}
这将为您提供10个字符宽的字段,同时保持左对齐。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 文本文件中的单词链表
- 用于访问容器<T>数据成员的正确 API
- 重载操作程序时出错>>用于类中的字符串 memebr
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- C++中的cin.ignore()函数不适用于整个流
- 在指针的帮助下,文本文件中单词的频率
- 没有用于初始化C++中的变量模板的匹配构造函数
- 用于C++中带有数组和指针的循环
- 为什么它不适用于Visual 2019的原因
- 使用在用于SFINAE的void_t中具有参数的方法
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 重载==不适用于二进制树
- 用于获取以元音和辅音开头的单词数量的程序返回错误的答案
- Qt C++macOS问题。我正在使用函数 .find( "a word") 在多集中搜索单词,它适用于 Windows 但不适用于 mac
- DAWG可以用于存储与单词相关的信息吗?
- Regexp,用于带有数字的特定单词
- 用于存储在文件中的单词的固定大小
- C++,用于在大文件中的行上搜索单词的算法