Reading records from files

Is there a way to indicate how many records exist in a given file? For example, vectors have the vector.size() command to show the number of given elements. Is there a such command for files and records?
A record is something that's part of the file format. Nothing that C++ knows anything about.
Ok. I guess I'll have to keep a running total in a separate file. Thanks
> Is there a such command for files and records?

wc can give a count of lines, words and bytes.
http://www.freebsd.org/cgi/man.cgi?query=wc&manpath=FreeBSD+10.0-RELEASE&format=html

Simplified C++ version of wc (only as filter, no command line options):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>

int main()
{
    unsigned long long line_cnt = 0 ;
    unsigned long long word_cnt = 0 ;
    unsigned long long byte_cnt = 0 ;
    
    std::string line ;
    while( std::getline( std::cin, line ) )
    {
        ++line_cnt ;
        byte_cnt += line.size() + 1 ;
        std::istringstream stm(line) ;
        std::string word ;
        while( stm >> word ) ++word_cnt ;
    }
    
    int width = 8 + ( byte_cnt > 9999999 ? 8 : 0 ) ;
    std::cout << line_cnt << std::setw(width)  << word_cnt << std::setw(width) << byte_cnt << '\n' ;
}

http://coliru.stacked-crooked.com/a/9787506d7de896f1
My main objective is to create a database of records and store them in a file. In order to read, sort, search the database I need to know how many records exist in the file.

I can just keep track on a separate file but I was wondering if there was an easier way to do this.

This is for a black jack game I created. I want to put usernames, passwords, and dollar amounts as the records.
Last edited on
Read the records into a std::vector<>. The size of the vector will tell you how many records were read, you can sort the vector, search in the vector etc.

For instance, something like this (you may want to use a cryptographically strong hash for the password):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>
#include <vector>
#include <functional>
#include <algorithm>

struct record
{
    std::string user_name ;
    std::size_t password_hash ;
    int amount ;
};

record make_record( std::string name, std::string password, int amount )
{ return { name, std::hash<std::string>()(password), amount } ; }

int main()
{
    const char* const file_name =  "records.txt" ;

    {
        std::vector<record> data ;

        // add some records
        data.push_back( make_record( "alee 4408", "jjjgUGUGU", 2234 ) ) ;
        data.push_back( make_record( "Peter 87", "YUGFUGU", 2000 ) ) ;
        data.push_back( make_record( "JL Borges", "kbbhki", 920 ) ) ;
        data.push_back( make_record( "Daffy Duck", "yf76rt87r87", 1500 ) ) ;

        // write the records into a file
        std::ofstream file(file_name) ;
        for( const record& rec : data )
        {
            file << rec.user_name << "# " // user name terminated by #
                 << rec.password_hash << ' '
                 << rec.amount << '\n' ; // one line per record
        }
    }

    // display the contents of the file (for testing)
    std::cout << "the file contains:\n\n" << std::ifstream(file_name).rdbuf() ;

    std::vector<record> recs_from_file ;
    // read the records from the file
    {

        std::ifstream file(file_name) ;
        record rec ;
        while( std::getline( file, rec.user_name, '#' ) && // read use name, throw away the'#'
               file >> rec.password_hash >> rec.amount >> std::ws ) // throw away the last new line
        {
            recs_from_file.push_back(rec) ;
        }
    }
    std::cout << '\n' << recs_from_file.size() << " records were read\n\n" ;

    // sort the records on amount
    std::sort( recs_from_file.begin(), recs_from_file.end(),
                [] ( const record& a, const record& b ) { return a.amount < b.amount ; } ) ;

    // display the sorted records
    for( std::size_t i = 0 ; i < recs_from_file.size() ; ++i )
    {
        std::cout << i+1 << ". " << recs_from_file[i].user_name << ' '
                   << recs_from_file[i].password_hash << ' '
                   << recs_from_file[i].amount << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/90f587f9b81550b9
Ohhh yeah. True true. Read the contents of the entire file back into the vector every time the program runs. Thank you.
Topic archived. No new replies allowed.