I was writing a program that opens a file assigns each element to an array. I was wondering if anyone had suggestions on how to get the number of items in a file, without knowing before hand.

I was usuing this:

int main()
{
    ifstream inputFile;
    inputFile.open("P6.txt");
    int index = 0;
    int x;

    while(inputFile >> x)
    {
        index++;
    }
    cout << index;

    return 0;
}

I made a dummy file to read crap into, then counted as they were cycled though, this way I can use the index to creat the array and assign elements to it. This seems wonky and I was hoping for some suggestions on how to tackle something like this.

Recommended Answers

All 16 Replies

first call seekg() to move the file pointer to the end of the file, then call ftell() to get the file size, allocate a buffer of that size or larger, call seekg() again to move the file pointer back to the beginning of the file, then call fread() to read the entire file into the buffer at one time.

It will depend in part on what type of data the file represents. If it is a plain text file, AD's method will work. If the file contains numeric values, the size of the file won't tell you how many numbers are stored. Your method would suffice to find the number of elements.

Better solution is to not worry about the size of the file (within reason), use a storage method that can grow as you read in more data. Vectors would be a good choice.

First of all try to use vector and to add the new members at the end of vector.

Can you be a little clearer about what you want to achieve.
Are you looking into reading an entire file and check how many times a character appears in the file and pupulate that into an array???

Can you please explain yourself better.?

If your goal is to find out how many numbers are in the file then you can't use the method I suggested. But if you want to file out the entire file size, such as what you see with Mcirosoft's File Explorer, when the way I suggested is how to go about doing that.

First of all try to use vector and to add the new members at the end of vector.

Vectors are useless for this purpose unless you need to use the data for something else.

From the first sentence> was writing a program that opens a file assigns each element to an array.

It should be obvious to see that the members of array will be used. You could use method push back and size to figure the numbers of array, that have been filed from the file.
And also I think there is a way to read the file size, this way is usual for programers that come from other languages

And also I think there is a way to read the file size,

That won't produce the file size, just the number of integers in the file. The integer "123" is at least three bytes in the file (could be more), not one.

From the first sentence> was writing a program that opens a file assigns each element to an array.

Yes, you are right, I was wrong about that, didn't read the first sentence good enough.

Well if I think for just a second the size of int is possible to obtain by sizeof(int).
But that is not the point I would like to make. If you would like to read the file, you need to open the file, and then if my memory serves me well, y ou automaticly lose the procesor. Now your program is in queue with others, and loses time. There are some files that go with any file, and that are kept on the system, it would be way faster to read those things. Sorry, I don't know how to do it yet.
And if you work with binary file mode, in the heder of a file there is a infomation how big or small is the file. Try some examples on bmp file, in the struct that is at the begining you could find that thing. But, if is possible to get it without opening it, like find the place where system keep that info, and to obtain it. .Net C++ has that method, that I remember for sure.

Well if I think for just a second the size of int is possible to obtain by sizeof(int).

True, but how do you know that the sizeof(int) bytes you read really represent an integer value? That works for binary files where the format is known, but for text files you're SOL on that assumption because values are represented by strings of characters rather than their in-memory bit pattern.

If you would like to read the file, you need to open the file, and then if my memory serves me well, y ou automaticly lose the procesor. Now your program is in queue with others, and loses time. There are some files that go with any file, and that are kept on the system, it would be way faster to read those things. Sorry, I don't know how to do it yet.

I'm still not sure what point you're trying to make. In a multitasking OS, this is a given regardless of what you're doing.

And if you work with binary file mode, in the heder of a file there is a infomation how big or small is the file. Try some examples on bmp file, in the struct that is at the begining you could find that thing.

Only if the file format has header information and the header information includes the size.

But, if is possible to get it without opening it, like find the place where system keep that info, and to obtain it. .Net C++ has that method, that I remember for sure.

Once again, the request is for the number of items in the file, not its byte size. The only way to query the number of items with 100% accuracy is to read the file. In some rare cases with strict formatting you can calculate the number of items from the byte size, but that approach raises a few red flags in terms of how accurate the result will be.

Well if I think for just a second the size of int is possible to obtain by sizeof(int).

Yes, but only in memory. That has no relationship to the number of bytes it occupies in a text file. sizeof(int) will produce the same in binary files assuming the programmer wrote out the integer as binary instead of text. But then you can use cin >> x; on bianry data.

And if you work with binary file mode, in the heder of a file there is a infomation how big or small is the file

No there isn't, unless the program that generated the file put it there. Normally binary files have no such indication of file size at the beginning.

But, if is possible to get it without opening it,

Yes, call stat() which, among other info, will return the file size.

Once again, the request is for the number of items in the file, not its byte size.

I'm not too sure about that -- the OP never said which one he/she wants. The subject of this thread indicates file size, not the number of integers in the file. But maybe the OP didn't know what he/she wanted.

I'm not too sure about that -- the OP never said which one he/she wants.

I can't divine what the OP wants, but the first post clearly said items: "I was wondering if anyone had suggestions on how to get the number of items in a file, without knowing before hand."

Further, the sample code strongly suggests arbitrary integers from a text file rather than bytes.

But maybe the OP didn't know what he/she wanted.

That's a possibility. We can only go on what was asked, and I'm having difficulty interpreting the OP's question as the number of bytes in a file.

Well there is thing caled FCB(File Control Block) if you could read that thing, you will be able to find the size of a file.
But it is becoming very ...
And the mayor problem is, if you open the file, and your app lose the processor.
Somehow I don't see the point of hving this discusion any more.

Somehow I don't see the point of hving this discusion any more.

They why are you continuing it???

That function is ok for MS-Windows -- but not if the program is running on *nix or other op.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.