从文件读取 (C++)

发布于 2024-11-04 20:21:40 字数 1505 浏览 0 评论 0原文

我不明白为什么这不会从我的文件中读取...

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

int main()
{
    int acctNum;
    int checks;
    double interest;
    double acctBal;
    double monthlyFee;
    const int COL_SZ = 3;
    ifstream fileIn;
    fileIn.open("BankAccounts.txt");
    if(fileIn.fail())
    {
        cout << "File couldn't open." << endl;
    }
    else
    {
        cout << left;
        cout << "Bank Account records:" << endl;
        cout << setw(COL_SZ) << "Account#" << setw(COL_SZ) <<
            "Balance" << setw(COL_SZ) << "Interest" << setw(COL_SZ) << "Monthly Fee" << setw(COL_SZ) <<
            "Allowed Checks" << setw(COL_SZ) << endl;
        while(fileIn >> acctNum >> acctBal >> interest >> monthlyFee >> checks)
        {
            cout << setw(COL_SZ) << acctNum << setw(COL_SZ) << acctBal << setw(COL_SZ) << interest << setw(COL_SZ) <<
                monthlyFee << setw(COL_SZ) << checks << endl;
        }
    }
    fileIn.close();
    system("pause");
    return 0;
}

我拿出 ios::out 并放入 ios::in 同样的事情发生了没有数据和同样的事情把 ios 全部拿出来在一起。我确实从以前的程序中创建了该文件...我是否必须将文件代码的读取放入该程序中?

BankAccount.txt 文件作为图片。

I can't figure out why this won't read from my file...

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

int main()
{
    int acctNum;
    int checks;
    double interest;
    double acctBal;
    double monthlyFee;
    const int COL_SZ = 3;
    ifstream fileIn;
    fileIn.open("BankAccounts.txt");
    if(fileIn.fail())
    {
        cout << "File couldn't open." << endl;
    }
    else
    {
        cout << left;
        cout << "Bank Account records:" << endl;
        cout << setw(COL_SZ) << "Account#" << setw(COL_SZ) <<
            "Balance" << setw(COL_SZ) << "Interest" << setw(COL_SZ) << "Monthly Fee" << setw(COL_SZ) <<
            "Allowed Checks" << setw(COL_SZ) << endl;
        while(fileIn >> acctNum >> acctBal >> interest >> monthlyFee >> checks)
        {
            cout << setw(COL_SZ) << acctNum << setw(COL_SZ) << acctBal << setw(COL_SZ) << interest << setw(COL_SZ) <<
                monthlyFee << setw(COL_SZ) << checks << endl;
        }
    }
    fileIn.close();
    system("pause");
    return 0;
}

I took out the ios::out and put in ios::in same thing happened no data and the same thing with taking ios out all together. I did make the file from a previous program...would i have to put the reading of the files code into that program?

The BankAccount.txt file as a picture.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无畏 2024-11-11 20:21:40

编辑

查看您的输入,您无法仅使用此代码读取如此复杂的输入,

while(fileIn >> acctNum >> acctBal >> monthlyFee >> checks)

该代码设置为读取以下形式格式化的数据:

11 12.12 11.11 13.13 14.12
11 12.12 11.11 13.13 14.12
11 12.12 11.11 13.13 14.12

相反,您必须在刮出之前读取各种字符串等您需要的数据。例如,要跳过下面的“Account”一词,您可以将其读入虚拟字符串

Account Number#1234
 std::string dummy; 
 fileIn >> dummy;   // read up to the whitespace, 
                    // in this case reads in the word "Account"

然后要获取数字,您必须读取下一个字符串并提取 #1234

 std::string temp; 
 fileIn >> temp;   // read up to the whitespace, 
                    // in this case reads in the word "Number#1234"

但您也可以使用 getline 读取并包括 #

 std::getline(fileIn, dummy, '#');

然后读取 # 之后的数字

 int acctNum = 0;
 fileIn >> acctNum;

所以如果您输入的是真正按照您所描述的方式格式化,您将不得不花费比您预期更多的时间来弄清楚如何解析数据。我不太了解您的输入如何为您提供完整的答案,但以上内容应该可以帮助您开始。

(您可以选择学习正则表达式,但此时您可能只想学习基础知识。)

原始

我刚刚尝试了您的代码,并且在输入中有足够的格式良好的值,它在 g++ 中工作。然而,我在查看您的代码时要小心的一件事是这一行:

   while(fileIn >> acctNum >> acctBal >> monthlyFee >> checks)

如果由于文件过早结束而导致上述任何内容无法读取,您的 cout 将不会被执行,从而导致屏幕上没有输出。您的输入是否具有上述所有值?它们的格式正确吗?为了调试,我可能会尝试分解读取:

   while (fileIn)
   {
       fileIn >> acctNum;
       std::cout << "Acct num is:" << acctNum << std::endl;
       ...
   }

或者只是使用调试器单步执行。

例如对于此输入:

11 12.12 11.11 13.13 14.12

你的代码打印出来

Bank Account records:   
Account#BalanceInterestMonthly FeeAllowed Checks   
11 12.126.93517e-31011.1113 `

但是用输入搞砸并在某处添加随机非数字字符,即:

11 * 12.12 11.11 13.13 14.12

让我得到只是

Bank Account records:     
Account#BalanceInterestMonthly FeeAllowed Checks

所以我肯定会逐件查看正在读取的内容以及从 fileIn 读取失败的地方,这肯定会导致您的问题。

您当然知道要按照指定的 删除 ios::out这里

Edit

Looking at your input you can't read such complex input with just

while(fileIn >> acctNum >> acctBal >> monthlyFee >> checks)

This code is setup to read data formatted in the following form:

11 12.12 11.11 13.13 14.12
11 12.12 11.11 13.13 14.12
11 12.12 11.11 13.13 14.12

Instead you'll have to read the various strings and such before scraping out the data you need. For example to skip over the word "Account" below, you can read it into a dummy string

Account Number#1234
 std::string dummy; 
 fileIn >> dummy;   // read up to the whitespace, 
                    // in this case reads in the word "Account"

Then To get the number you'll have to read the next string and extract the #1234

 std::string temp; 
 fileIn >> temp;   // read up to the whitespace, 
                    // in this case reads in the word "Number#1234"

But you could also use getline to read up to and including the #

 std::getline(fileIn, dummy, '#');

Then read in the number after the #

 int acctNum = 0;
 fileIn >> acctNum;

So if you're input is truly formatted as you describe, you'll have to spend a lot more time figuring out how to parse your data then you may have expected. I don't know enough about how your input is expected to give you a complete answer, but the above should hopefully get you started.

(Optionally, you could learn about regular expressions, but at this point you may just want to learn the basics.)

Original

I just tried your code out and with enough well-formatted values in the input, it works in g++. However, one thing that I am wary of looking at your code is this line:

   while(fileIn >> acctNum >> acctBal >> monthlyFee >> checks)

If any of the above fails to read due to the file ending prematurely, your cout isn't going to get executed, causing no output to the screen. Does your input have all the above values? Are they well formatted? To debug I might try breaking up the reads:

   while (fileIn)
   {
       fileIn >> acctNum;
       std::cout << "Acct num is:" << acctNum << std::endl;
       ...
   }

or just step through with a debugger.

For example for this input:

11 12.12 11.11 13.13 14.12

your code prints out

Bank Account records:   
Account#BalanceInterestMonthly FeeAllowed Checks   
11 12.126.93517e-31011.1113 `

But screwing with the input and adding a random non numeric character somewhere, ie:

11 * 12.12 11.11 13.13 14.12

causes me to get just

Bank Account records:     
Account#BalanceInterestMonthly FeeAllowed Checks

So I would definitely look piece-by-piece at what's getting read and where a read from fileIn is failing, this would definitely cause your problems.

You know of course to remove the ios::out as specified here

扎心 2024-11-11 20:21:40

fileIn.open("BankAccounts.txt", ios::out);
                                ^^^^^^^^

正在打开文件进行输出。尝试 ios::in。

You have

fileIn.open("BankAccounts.txt", ios::out);
                                ^^^^^^^^

You're opening the file for output. Try ios::in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文