File I/O Issues Containing Arrays

Given a History of transactions in a bank account how can we print an array of an account to a file? For example you call the function by its filename. Then list the information in each account (name,account number,balance) such as Account a1("bob","1234",199) account a2("Jim, "4567",345) .... account a20 ("Pete", "7890",400). Then in this example we would need an array length of 20 elements. I believe this has to do with file i/o and its an output file we must create.
Must also write a function that reads in a file showing a list of accounts and creates account objects out of the accounts and store valid accounts as objects in the accounts array, skip invalid accounts and, return the number of invalid objects. So far I have :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
void accounts2file(std::string ofname, Account accounts[], int numaccounts){
	std::string filename = "outfile.txt";
	std::ofstream outFile;
  
	outFile.open(filename.c_str());
	if (outFile.fail()){
		std::cout<< "The File was not opened" << std::endl;
		exit(1);
	}
	
	outFile << numaccounts << std::endl;
			
  outFile.close();

  }


 
int file2accounts(std::string ifname, Account accounts[]){

	 std::string filename = "infile.txt";
	std::ifstream inFile;
	
	inFile.open(filename.c_str());
	if (inFile.fail()){
		std::cout<<"The File was not opened"<<std::endl;
	}
	
	inFile << numaccounts <<std::endl;
	
	inFile.close();
	}
	

}

Last edited on
Okay. What are you having trouble with? Try to be specific.
I need to write a function (void accounts2file(std::string ofname, Account accounts[], int numaccounts)) that prints an array of accounts to a file; The result should be something similar to this in the .txt file following this format.
N

accNum1 ss1 name1 bala1

accNum2 ss2 name2 bala2

...

...

accNumN ssN nameN balaN

The idea is to copy the string indexies from an array consisting of strings into a external .txt file to be "stored" as records.
Is this what you mean?
1
2
3
4
5
outFile << numaccounts << '\n';
for ( int i = 0; i < numaccounts * 4; ++i )
{
    outFile << accounts[i];
}


Also, you don't need to convert your strings on lines 6 and 25. open() can take a std::string.
I am not too good with file i/o and this is the question I was given. I know I have to write out to a file. I have a ".h" file and when calling it in my ".cpp" file it should write out to a file but I'm very confused on how to do this since we did not spend enough time on this topic.
@Yay295
Also, you don't need to convert your strings on lines 6 and 25. open() can take a std::string.

Really? I've always had to convert it to c-string and the article on this site says nothing about being able to use std::string. http://www.cplusplus.com/reference/fstream/ofstream/open/

@DTrey
A ".h" file is a header file, and I don't see how it could have anything to do with file input and output for your program.

You didn't declare numaccounts in the file2accounts function.
Line 30 should be inFile >> numaccounts;
You need a return numaccounts; at the end of the function.

Do you know how to create classes? Also, is the input file supposed to be the same format as the output file?

N

accNum1 ss1 name1 bala1

accNum2 ss2 name2 bala2

...

...

accNumN ssN nameN balaN
Last edited on
Using std::string to open a file is a new feature in C++11.
Really? I've always had to convert it to c-string and the article on this site says nothing about being able to use std::string. http://www.cplusplus.com/reference/fstream/ofstream/open/


Click the C++ 11 tab above the function prototypes.
Silly me, I haven't looked at any C++11 features because my school still uses MS Visual Studio 2010.
Yes this header file is where i created my class. I was given this function to implement and those were the directions. I know how to do file i/o for a basic code as opening a file that exists already or just writing out to a file. This has to do with file i/o and writing out to an array and its really confusing to me. The difficulty level is far greater than anything I have practiced so far and this is why its challenging.
This function should give the output you wanted:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void accounts2file(std::string ofname, Account accounts[], int numaccounts)
{
	std::ofstream outFile;

	outFile.open(ofname);
	if (outFile.fail())
	{
		std::cout<< "The File was not opened" << std::endl;
		exit(1);
	}

	outFile << numaccounts << std::endl << std::endl;
	
	for (int i = 0; i < numaccounts; ++i)
	{
		//don't know what you actually called your functions
		outFile << accounts[i].getAccNum() << ' ';
		outFile << accounts[i].getSS() << ' ';
		outFile << accounts[i].getName() << ' ';
		outFile << accounts[i].getBala() << std::endl << std::endl;
	}

	outFile.close();
}


Maybe you can use that to fix your other function.
Thank you I will attempt it and let you know if it works and in general should be the same idea for a file in as well?
Did not work. It actually made my program not compile. I'm not sure how to fix this issue because all the examples of codes written with file I/o are basically similar and when I try it doesn't work well with this program I've written.
It compiles fine for me after I change line 5 to outFile.open(ofname.c_str()); since I don't have a C++11 compliant compiler.
How would I get it to display the number of accounts that are written to the txt file. For example:
2
Account 1 info
Account 2 info

I was able to see my error when I wrote these in my file and it compiles now. Would I have to outFile<<accounts[i] in the beginning to display the number of accounts that are written.
Topic archived. No new replies allowed.