0

First of all, Thank you for the help and I hope I'm following your guidelines correctly, if I'm not, I'd be more than happy to correct it.

I need some help with an assignment I have. After finally getting it to run, my program won't open the files I need. The program should open a file named prog10in.txt and output it into a file named prog10out.txt However, I put a line in it that would tell me if it failed and that keeps coming up. I am doing this in Visual Studio. I need to understand how to properly open the file whether my error is in the code or in Visual Studio. The program compiles without error.

The error should be in getAccountInfo() or 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);


}

There is an Account.h and Account.cpp file that holds the account information class.

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

Again, thank you guys for the help.

2
  • Please post only the code relevant to your question, this will help the ones that read your question. Commented Apr 29, 2015 at 5:21
  • I'm not exactly sure where my error is but I trimmed off a few files that I knew the error wasn't in. Commented Apr 29, 2015 at 5:30

2 Answers 2

1

I think your program don't find the input file. right? because the output file will be created if not exists.

So about the input, if you use an absolute path it should work unless the path is wrong. but if you are using a relative path. you should know that running the program from visual studio the current directory (.) will be the projects directory.

Sign up to request clarification or add additional context in comments.

4 Comments

I think that is the issue. I tried putting the file in multiple directories within the project file. It still won't work.
@KristianGalvan If you are not using the fullpath put it inside the Release or Debug folder where your exe file is created ( whichever mode you are using for compilation). It's not just any folder
As I stated when running from visual studio, the current directory will be the project directory. not debug or release. but if running the application directly, well the current directory is the current directory itself!!
Thank you guys. I figured out I was naming the file "prog10in.txt" and it automatically added .txt to the end of that. So the file was prog10in.txt.txt and the parameter was set to find prog10in.txt.
0

Your path is not correct. if filename is "abc.txt" and the file is present in same folder as that of binary (exe file) than you only need to provide the name. If not than provide the full path of the file in double quotes (if space is present)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.