473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fstream methods

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.

Jul 23 '05 #1
2 2852
ki****@cyrocom.co.uk wrote:
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
Jul 23 '05 #2

<ki****@cyrocom.co.uk> wrote in message news:11*********************@g14g2000cwa.googlegro ups.com...
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 -----
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Maya | last post by:
Apart from being a pointer, what would be the benefit of using 'std::filebuf' than using the std::fstream? As far as I can see, I would use the same methods in 'filebuf' that I would when using...
2
by: Javi | last post by:
Hi!. I'm trying to do something like this but results in lot of errors. Can anybody tell me how to do it (first of all I hope you can grasp my idea)?: myfile.cpp #include <iostream> #include...
4
by: adamrobillard | last post by:
Hi, I have always used fopen and FILE* to load and save structures to file. I am trying to convert all the older code to use proper C++ calls... the following code works properly but I would...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
1
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
7
by: Soneji | last post by:
*sigh* Ok, I have two questions. First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2; I recently started trying to use 'fstream' to work my input/output, and I noticed that using the...
6
by: canilao | last post by:
Hi All, So I ran into an issue with using the ! operator with fstream: // Make sure the file is ready to be read before we move on. 1: fstream fileStream; 2: ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.