gpt4 book ai didi

c++ - 协助 C++ 中的文件输入和输出

转载 作者:行者123 更新时间:2023-11-28 06:17:37 28 4
gpt4 key购买 nike

首先,感谢您的帮助,我希望我正确地遵循了您的指导方针,如果没有,我将非常乐意纠正它。

我需要一些帮助来完成我的任务。最终让它运行后,我的程序无法打开我需要的文件。该程序应该打开一个名为 prog10in.txt 的文件并将其输出到一个名为 prog10out.txt 的文件中但是,我在其中放了一行,如果它失败了会告诉我,不断出现。我在 Visual Studio 中这样做。我需要了解如何正确打开文件,无论我的错误是在代码中还是在 Visual Studio 中。程序编译无误。

错误应该在getAccountInfo()outputFile()中。

源代码.cpp:

#include <iostream>
#include <string>

#include "Account.h"
#include "Bank.h"

#include <cstdio>

#include <fstream>
#include <iomanip>



using namespace std;


ifstream inStream;

int main(int argc, char* argv[])
{
Account accounts[MAXIMUM_ACCOUNTS];
Bank Customer;
int AccountNumber = 0;

int ID;
string lastName;
string firstName;
double balance;

Customer.getAccountInfo(argv, AccountNumber, ID, lastName, firstName,balance, accounts);
Customer.sort(AccountNumber, accounts);
Customer.outputFile(argv, AccountNumber, accounts);


}

Account.hAccount.cpp 文件保存了账户信息类。

银行.cpp:

#include <iostream>
#include <string>

#include "Account.h"
#include "Bank.h"

#include <cstdio>

#include <fstream>
#include <iomanip>





void Bank::getAccountInfo(char* argv[], int& accountNumber, int& accountID, std::string& accountLastName, std::string& accountFirstName, double& accountBalance, Account Customers[MAXIMUM_ACCOUNTS])
{
std::ifstream fin;
int accountIndex = 0;
fin.open(argv[1]);
if (fin.fail())
{
std::cout << "Input file did not open" << std::endl;
system("pause");
exit(1);
}

while (!fin.eof() && accountIndex < MAXIMUM_ACCOUNTS)
{
std::cout.setf(std::ios::fixed | std::ios::showpoint | std::ios::right);
std::cout.precision(2);

fin >> accountID >> accountLastName >> accountFirstName >> accountBalance;

fin.ignore(1, ' ');

Customers[accountIndex]._accountID = accountID;
Customers[accountIndex]._accountLastName = accountLastName;
Customers[accountIndex]._accountFirstName = accountFirstName;
Customers[accountIndex]._accountBalance = accountBalance;

accountIndex++;
accountNumber++;
}

std::cout << "Clients: " << accountIndex << std::endl;
}

void Bank::sort(int accountNumber, Account Customers[MAXIMUM_ACCOUNTS])
{
int nextSmallest;

int IDtemp;
std::string lastNameTemp;
std::string firstNameTemp;
double balanceTemp;

for (int accountIndex = 0; accountIndex < accountNumber; accountIndex++)
{
nextSmallest = accountIndex;
for (int accountIndex2 = accountIndex + 1; accountIndex2 < accountNumber; accountIndex2++)
{
if (Customers[accountIndex2]._accountID < Customers[nextSmallest]._accountID)
{
nextSmallest = accountIndex2;
}

IDtemp = Customers[accountIndex]._accountID;
Customers[accountIndex]._accountID = Customers[nextSmallest]._accountID;
Customers[nextSmallest]._accountID = IDtemp;

lastNameTemp = Customers[accountIndex]._accountLastName;
Customers[accountIndex]._accountLastName = Customers[nextSmallest]._accountLastName;
Customers[nextSmallest]._accountLastName = lastNameTemp;

firstNameTemp = Customers[accountIndex]._accountFirstName;
Customers[accountIndex]._accountFirstName = Customers[nextSmallest]._accountFirstName;
Customers[nextSmallest]._accountFirstName = firstNameTemp;

balanceTemp = Customers[accountIndex]._accountBalance;
Customers[accountIndex]._accountBalance = Customers[nextSmallest]._accountBalance;
Customers[nextSmallest]._accountBalance = balanceTemp;
}
}
}

void Bank::outputFile(char* argv[], int accountNumber, Account Customers[MAXIMUM_ACCOUNTS])
{
int outFileIndex;

std::ofstream fout;

fout.open(argv[2]);
if (fout.fail())
{
std::cout << "Output file did not open" << std::endl;
system("pause");
exit(1);
}
if (fout.fail())
{
std::cout << "Output failed" << std::endl;
exit(0);
}

fout.setf(std::ios::fixed | std::ios::showpoint | std::ios::right);
fout.precision(2);

for (outFileIndex = 0; outFileIndex < accountNumber; outFileIndex++)
{
fout << std::setw(6) << Customers[outFileIndex]._accountID;
fout << std::setw(10) << Customers[outFileIndex]._accountLastName;
fout << std::setw(10) << Customers[outFileIndex]._accountFirstName;
fout << std::setw(10) << Customers[outFileIndex]._accountBalance;
fout << std::setw(10) << std::endl;
}
}

再次感谢大家的帮助。

最佳答案

我认为您的程序找不到输入文件。正确的?因为如果不存在,将创建输出文件。

所以关于输入,如果你使用绝对路径,它应该可以工作,除非路径是错误的。但是如果你使用的是相对路径。您应该知道,从 visual studio 运行程序时,当前目录 (.) 将是项目目录。

关于c++ - 协助 C++ 中的文件输入和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29934948/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com