-
Arrays and Functions
I know that you can't directly return an array from a function, but I'm a little shaky on how to use pointers with arrays.
I've tried using a function like this to read data from a file and assign it to an array via a function:
Code:
ifstream fin;
char array[4];
void readText(char *array)
{
for(int i=0;i==sizeof array;i++)
*(array+i)=fin.get();
}
The .dat file is simply
When I use cout to display the contents of the array that should have received the data from the file, I only receive 4 blank spaces. Apparently nothing is being written to the array, but I don't really understand what exactly is going on.
-
Your problem is less about pointers and more about for loops, in that sizeof array is not zero and therefore the for loop never happens.
However: the dimensions of an array do not get passed in to a function -- hence sizeof array is the sizeof any pointer on your system, usually four. You will need to pass in the dimensions separately using another parameter to your function.
-
Apart from the issue of using "==" in the loop condition .....
This is a case where the rule of thumb "a pointer is an array" fails .... a pointer is different from an array in this context.
sizeof array in the function gives the size of a pointer (usually 4 on most 32 bit systems), not the number of elements available in the array.
The size of the array needs to be passed separately to the function (eg a second argument).
The approach "number = sizeof(array)/sizeof(array[0])" only works for arrays. It does not work for pointers. When arrays are passed to functions as an argument, the function only receives a pointer and does not know it is an array.
-
Alternatively, you could use a std::vector<char> to save the data into instead of an array.
Code:
#include <vector>
#include <iostream>
#include <fstream>
#include <iterator>
int main(void)
{
std::ifstream input("testfile.dat");
std::vector<char> v((std::istream_iterator<char>(input)),
std::istream_iterator<char>());
copy(v.begin(), v.end(), std::ostream_iterator<char>(std::cout));
std::cout << std::endl;
}
-
Thanks for the help. I managed to get everything sorted out.
One last question:
Is there a specific function that can return the size, in bytes, of a data file? I've searched around a bit, but I haven't been able to find anything.
Thanks
-
The only cross platform way is to open the file and read all the data to get the total size in bytes.
-
Code:
#include <fstream>
#include <iomanip>
#include <iostream>
int main()
{
std::ifstream f("c:\\1.bmp",std::ios::binary);
f.seekg(0,std::ios_base::end);
std::streampos y = f.tellg();
std::cout << y <<std::endl;
return 0;
}
-
>> The only cross platform way is to open the file and read all the data to get the total size in bytes.
That might be the only completely portable way, but I believe cross-platform libraries have this ability (such as boost's filesystem).
-
I'm sure vart's post will work on 99.99% of all systems that support the fstream class (to some reasonable completeness).
There may be certain devices that don't support seek (such as tape-devices, consoles and serial ports), but for real files on a disk-like device, it should not be a problem to seek the end of the file to find out the size of the file. Just bear in mind that seek operates on the binary form of the file, so it's the size of the file on disk, not the size you'd get if you read the file using characters in text mode [on some machines that would indeed be the same, but on a Windows system, every newline consists of a CR and an LF, which when you read the file gets translated to newline (LF), so for every line in the file, the size is one byte bigger on disk than it is when you read it via the text-mode file-reading method].
--
Mats