Counting char array w/out using the class

Have a project I am working on. Basically, The getFileLength function should accept a filename (an array of characters) as an
argument and return an integer that states how large the file is, in number of characters. I am having difficulty being able to count the number char, any ideas? I am also getting error in MAIN, stating inFile and arr are undefined, not sure why. Thank you. (Also I am not allowed to use any String FCNS)

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

#include<iostream>
#include<fstream>

using namespace std;

int getFileLength(char arr[]);


int main(){

	getFileLength(inFile, arr); // getting undefined errors here

	 system("pause");
	return 0;
}


int getFileLength(ifstream inFile, char arr){

	int length = 0;


	inFile.open("test.html");
	while (!inFile.eof()) {
		
		inFile >> arr;
		
		for (int i = 0; i < arr; i++){
			length += i;
                      
		}

	}

	return length;
	inFile.close();
}
Last edited on
arr is a single char. So this:
inFile >> arr;
reads in a single char. One byte.

Then, you do this:
for (int i = 0; i < arr; i++){

What are you trying to do with i < arr ?

i is a number, arr is a character. So you're saying something like:
for (int i = 0; i < 'j'; i++){
for example, if the character in question happened to be 'j'.

And then you're counting up from 0 to 'j'. What? What?
As far as the inFile >> arr ~ I am suppose to read a file in and count how many chars it is. I thought I had that set up correctly.

The for - loop was my attempt to count how many chars were in the file I am to read
arr is a single char, so you're going to read in one char each time inFile >> arr; is executed.
I see what your saying, Arr is basically just one element. So, how am I to count how chars. of the file I am trying to read without knowing how large to make Arr, or should I even be using an array to do this. Hmmm...
A very simple way would be to read in a character, and add one to the count, and read in a character, and add one to the count, and read in a character, and add one to the count, and so on, until you reach the end of the file.
Topic archived. No new replies allowed.