使用C ++

时间:2015-04-29 05:19:37

标签: c++ file-io

首先,感谢您的帮助,我希望我正确遵循您的指导方针,如果不是,我会非常乐意纠正它。

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

错误应位于getAccountInfo()outputFile()

Source.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文件,其中包含帐户信息类。

Bank.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;
    }
}

再次,谢谢你们的帮助。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

你的路径不正确。如果filename是"abc.txt"且文件与二进制文件(exe文件)存在于同一文件夹中,则只需要提供名称。如果不是以双引号提供文件的完整路径(如果存在空格)