Jeroen
Jeroen

Reputation: 16865

Are standard text files NULL-terminated?

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

Answers (4)

suraj
suraj

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

LS_ᴅᴇᴠ
LS_ᴅᴇᴠ

Reputation: 11181

No, they are not. But many C/C++ function to read strings from files appends zero termination to returned data.

Upvotes: 1

robin.koch
robin.koch

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

SLaks
SLaks

Reputation: 887777

No.
Files have a known length, so they do not need any terminator byte.

Upvotes: 8

Related Questions