如何使用C 在线路和列中写入文本文件
How to write in text file by line and column using C++
您可以检查我的代码出了什么问题吗?我只想制作可以将信息存储在文本文件中的程序,并将在文本文件中显示:
bookid booktitle authorname bookdepartment bookissuer
,但仅对1本书成功,如果我输入2本书,它将获得无限
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void IssueBooks(){
int SIZE;
cout << "How many books you want to borrow?"<< endl;
cin >> SIZE;
string booksTitle[SIZE], department[SIZE], authorName[SIZE],
BooksIssuer[SIZE];
double booksID[SIZE];
for (int i = 0; i < SIZE; i++ ){
for (int j = 0; j < SIZE; j++){
cout << "Enter Book ID:" << endl;
cin >> booksID[j];
cout << "nEnter Book Title:" << endl;
cin >> booksTitle[j];
cout << "nEnter Book Author:" << endl;
cin >> authorName[j];
cout << "nEnter Book Department:" << endl;
cin >> department[j];
cout << "nEnter Book Issuer: " << endl;
cin >> BooksIssuer[j];
ofstream out("IssueRecord.txt");
out << setw(10) << booksID[j];
out << setw(10) << booksTitle[j];
out << setw(10) << authorName[j];
out << setw(10) << department[j];
out << setw(10) << BooksIssuer[j];
}
}
}
int main(){
IssueBooks();
return 0;
}
谢谢
这是良好的样式和C 代码:
- 将structs用于相关数据
- 使用向量
- 使用getline允许输入字段中的空格
-
单独的关注点(在阅读输入时不要打印输出,从功能的角度看不当)
int main() { Books issued = borrowBooks(); std::cout << "Successfully borrowing " << issued.size() << " books, saving receiptn"; printReceipt(issued, "IssueRecord.txt"); }
-
处理错误
作为奖金,我显示了以静态配置的列引导的漂亮打印表:
struct {
int width;
std::string header;
} columns[] = { { 10, "ID" }, { 30, "Title" }, { 25, "Author" },
{ 25, "Department" }, { 15, "Issuer" } };
活在coliru
#include <vector>
#include <fstream>
#include <iomanip>
#include <iostream>
struct Book {
int id = 0;
std::string title, department, author, issuer;
};
using Books = std::vector<Book>;
bool promptDetails(Book& book) try {
std::cin.exceptions(std::ios::failbit);
std::cout << "Enter Book ID (enter 0 to complete): ";
if (std::cin >> book.id) {
std::cin.ignore(1024, 'n');
}
if (book.id == 0)
return false;
std::cout << "Enter Book Title: ";
std::getline(std::cin, book.title);
std::cout << "Enter Book Author: ";
std::getline(std::cin, book.author);
std::cout << "Enter Book Department: ";
std::getline(std::cin, book.department);
std::cout << "Enter Book Issuer: " << std::endl;
std::getline(std::cin, book.issuer);
return true;
} catch(std::exception const& e) {
std::cout << "Input error (" << e.what() << ")n";
return false;
}
Books borrowBooks() {
Books books;
while (true) {
Book book;
if (!promptDetails(book))
break;
books.push_back(std::move(book));
}
return books;
}
void printReceipt(Books const& receipt, std::string const& fname) {
std::ofstream out(fname);
struct {
int width;
std::string header;
} columns[] = { { 10, "ID" }, { 30, "Title" }, { 25, "Author" },
{ 25, "Department" }, { 15, "Issuer" } };
for (auto& col : columns) out << std::setw(col.width) << col.header << " | ";
out << "n" << std::setfill('-');
for (auto& col : columns) out << std::setw(col.width) << "" << "-+-";
out << "n" << std::setfill(' ');
for (auto& book : receipt) {
out << std::setw(columns[0].width) << book.id << " | "
<< std::setw(columns[1].width) << book.title << " | "
<< std::setw(columns[2].width) << book.author << " | "
<< std::setw(columns[3].width) << book.department << " | "
<< std::setw(columns[4].width) << book.issuer << " |" << "n";
}
for (auto& col : columns) out << std::setw(col.width) << std::setfill('-') << "" << "-+-";
out << "n";
}
int main() {
Books issued = borrowBooks();
std::cout << "Successfully borrowing " << issued.size() << " books, saving receiptn";
printReceipt(issued, "IssueRecord.txt");
}
如果输入为
29134
The Hen And Her Trust Issues
Mojit Bunghead
Pop Culture
Central Library
4893
Most Unnecessary
P.H.W. Graftink
Science Fiction
Central Library
6083
Code Of Concoct
Public Domain
Folklore
Central Library
0
输出文件看起来像
ID | Title | Author | Department | Issuer |
-----------+--------------------------------+---------------------------+---------------------------+-----------------+-
29134 | The Hen And Her Trust Issues | Mojit Bunghead | Pop Culture | Central Library |
4893 | Most Unnecessary | P.H.W. Graftink | Science Fiction | Central Library |
6083 | Code Of Concoct | Public Domain | Folklore | Central Library |
-----------+--------------------------------+---------------------------+---------------------------+-----------------+-
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入