Reputation: 16865
I am writing a C++ program that reads a file and then sends it to another class as a character array. Because character arrays only get passed by pointer, all size is lost.
The file it'll be reading will be a text file. Are text files null terminated?
Preferably I do not want to use a Vector as I really don't need any of it's features but the size of the array.
Upvotes: 5
Views: 4518
Reputation: 7
use eof rather than finding null character ... for example :
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream iFile("myfile.txt"); // myfile.txt has integers, one per line
while (!iFile.eof())
{
int x;
iFile >> x;
cerr << x << endl;
}
return 0;
}
Upvotes: -1
Reputation: 11181
No, they are not. But many C/C++ function to read strings from files appends zero termination to returned data.
Upvotes: 1
Reputation: 1223
no text files are not NULL terminated. You need to check for EOF (End Of File) I think
cin.eof()
is what you are searching. It returns true if the end of file is reached.
Upvotes: 4
Reputation: 887777
No.
Files have a known length, so they do not need any terminator byte.
Upvotes: 8