如何使用C 在线路和列中写入文本文件

How to write in text file by line and column using C++

本文关键字:文本 文件 何使用 在线路      更新时间:2023-10-16

您可以检查我的代码出了什么问题吗?我只想制作可以将信息存储在文本文件中的程序,并将在文本文件中显示:

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 |
-----------+--------------------------------+---------------------------+---------------------------+-----------------+-