Creating a file using fstream in a function

Hi,

I seem to be running into far heavier resistance with an assignment than I initially intended, and I'm at my wits end, I have a basic start, but I can't seem to get things working.Here is the problem.

Array/File Functions
Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents to the file, and then close the file.

Write another function named fileToArray. This function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, read its contents into the array, and then close the file


code:
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
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
using namespace std;

// constant variables
const int ARRAY_SIZE = 10;
const int NAME_SIZE = 20;

// function prototypes
void arrayToFile(char[], int[], int);
void fileToArray(char[], int[], int);



int main()
{

	int arrayContents[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int* intPointer;
	intPointer = arrayContents;
	char fileName[NAME_SIZE];

	cout << "Chapter 12 Program 8" << endl << endl;
	cout << "Enter name of file." << endl << endl;
	cin.getline(fileName, NAME_SIZE);
	arrayToFile(fileName, intPointer, ARRAY_SIZE);

	






	return 0;
}

void arrayToFile( char file[], int pointer, int arr_size)
{
	fstream dataFile;
	dataFile.open(file, ios::in | ios::binary);
	for (int count = 0; count < arr_size; count++)
	{
		dataFile.write(reinterpret_cast<char *>(pointer), sizeof(pointer));
	}

}


here are the errors I'm getting right now.

1
2
Error	1	error LNK2019: unresolved external symbol "void __cdecl arrayToFile(char * const,int * const,int)" (?arrayToFile@@YAXQADQAHH@Z) referenced in function _main	12-8.obj
Error	2	fatal error LNK1120: 1 unresolved externals	C:\Users\Nick Gingerella\Desktop\Program Projects\CIS-7A\12-8\Debug\12-8.exe


Help would be very much appreciated.

Last edited on
The second parameter in line 38 is a simple int, not a pointer nor an array
Thank you very much, I rewrote "int pointer" to "int pointer[]"

Would this be considered a pointer to an integer array?
it compiles now(thanks again) but I want to know if my understanding is correct.
since I passed "intPointer" (a pointer to an int) and set that to point to "arrayContents"
(an array), it now points to an array, but in order for it to be recognized a pointer to an array
I have to add the [] to it?

forgive my crude interpretation, I'm just trying to get some extra help on this.

here is the complete code, some input on whether or not it actually accomplishes what It's supposed to is of some concern.

Complete code:
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
using namespace std;

// constant variables
const int ARRAY_SIZE = 10;
const int NAME_SIZE = 20;

// function prototypes
void arrayToFile(char[], int[], int);
void fileToArray(char[], int[], int);



int main()
{

	int arrayContents[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int arrayContents2[ARRAY_SIZE];
	int* intPointer2;
	int* intPointer;
	intPointer = arrayContents;
	intPointer2 = arrayContents2;
	char fileName[NAME_SIZE];

	cout << "Chapter 12 Program 8" << endl << endl;
	cout << "Enter name of file." << endl << endl;
	cin.getline(fileName, NAME_SIZE);
	arrayToFile(fileName, intPointer, ARRAY_SIZE);
	fileToArray(fileName, intPointer2, ARRAY_SIZE);

	//dislplay contents of arrayContents2
	for (int count = 0; count < ARRAY_SIZE; count++)
	{
		cout << intPointer[count] << " ";
	}


	return 0;
}

void arrayToFile( char file[], int pointer[], int arr_size)
{
	fstream dataFile;
	dataFile.open(file, ios::in | ios::binary);
	for (int count = 0; count < arr_size; count++)
	{
		dataFile.write(reinterpret_cast<char *>(pointer), sizeof(pointer));
	}

	dataFile.close();

}

void fileToArray( char file[], int pointer[], int arr_size)
{
	fstream dataFile;
	dataFile.open(file, ios::out | ios::binary);
	for (int count = 0; count < arr_size; count++)
	{
		dataFile.read(reinterpret_cast<char *>(pointer), sizeof(pointer));
	}

	dataFile.close();
}
Last edited on
int pointer[] is an array, int *pointer a pointer ( to an array or to a single int )
There's not much difference though, an array is a pointer to the first element of the array
Last edited on
thanks, that helped
Topic archived. No new replies allowed.