fstream methods

K

kieran

Hi,
Does anyone know of any methods in std::fstream that would:

check if the file exists (maybe something like file1.exist()?)

count the lines in the file (maybe int lines = file1.num_of_lines()?)



I've tried looking for methods like these, but can't find anything.
Help much appreciated.
 
V

Victor Bazarov

Does anyone know of any methods in std::fstream that would:

check if the file exists (maybe something like file1.exist()?)

count the lines in the file (maybe int lines = file1.num_of_lines()?)

Don't you have your compiler's reference where _all_ member functions
are listed and explained? Why ask about them when you can simply see
for yourself in your manual?
I've tried looking for methods like these, but can't find anything.

Because there are none.

Existence of a file has nothing to do with the stream that opens that
file. Besides, opening a "file stream" can have nothing to do with
the existence of some "data storage area" somewhere on the "file system"
in your OS. That's what the platform-specific methods for working with
your file system are for.

Counting lines is undefined. What's a line? How do you define a line?
Can you count lines before you read them? How would you do that? When
you read the stream, you _always_ change its state. So, once you count
the "lines", the number of "lines" can easily be different simply because
you counted them. Have you heard of the Heisenberg principle?

V
 
A

Alex Vinokur

Hi,
Does anyone know of any methods in std::fstream that would:
[snip]


count the lines in the file (maybe int lines = file1.num_of_lines()?)
[snip]


--- prog.cpp : BEGIN ---
#include <cassert>
#include <iostream>
#include <fstream>
using namespace std;


pair<size_t, bool> get_no_of_lines (const char * const filename_i)
{
ifstream fs (filename_i);

assert (fs);
assert (fs.is_open());


pair<size_t, bool> result;

result.first = count (istreambuf_iterator<char>(fs), istreambuf_iterator<char>(), '\n');

result.second = false;

fs.seekg(-1, ios::end);
if (fs.tellg() >= 0)
{
result.second = !count (istreambuf_iterator<char>(fs), istreambuf_iterator<char>(), '\n');
}

return result;

}

// -----------------
void create_file (
const string& data_i,
const char * const filename_i
)
{
remove (filename_i);

ofstream fs (filename_i);

assert (fs);
assert (fs.is_open());

fs << data_i;

fs.close();
assert (!fs.is_open());

}


#define FILE1 "foo1"
#define FILE2 "foo2"
#define FILE3 "foo3"
#define FILE4 "foo4"
#define FILE5 "foo5"

int main ()
{
const char data1[] = "12345\nabc\ndef\n";
create_file (data1, FILE1);

const char data2[] = "9876\nxyz\nprs";
create_file (data2, FILE2);

const char data3[] = "\n";
create_file (data3, FILE3);

const char data4[] = " ";
create_file (data4, FILE4);

const char data5[] = "";
create_file (data5, FILE5);


pair<size_t, bool> result1 = get_no_of_lines (FILE1);
pair<size_t, bool> result2 = get_no_of_lines (FILE2);
pair<size_t, bool> result3 = get_no_of_lines (FILE3);
pair<size_t, bool> result4 = get_no_of_lines (FILE4);
pair<size_t, bool> result5 = get_no_of_lines (FILE5);


cout << FILE1 << " : " << "lines = " << result1.first << ", tail - " << result1.second << endl;
cout << FILE2 << " : " << "lines = " << result2.first << ", tail - " << result2.second << endl;
cout << FILE3 << " : " << "lines = " << result3.first << ", tail - " << result3.second << endl;
cout << FILE4 << " : " << "lines = " << result4.first << ", tail - " << result4.second << endl;
cout << FILE5 << " : " << "lines = " << result5.first << ", tail - " << result5.second << endl;

return 0;

}

--- prog.cpp : END -----




--- Run log : BEGIN ---

foo1 : lines = 3, tail - 0
foo2 : lines = 2, tail - 1
foo3 : lines = 1, tail - 0
foo4 : lines = 0, tail - 1
foo5 : lines = 0, tail - 0

--- Run log : END -----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top