How to Read and Store Literally Everything From and In/Out of a .txt File.

My objective is to have an array of structures that read literally everything from a .txt file..

That is;
> The inner most structure variable is a character array that stores every character of the file.
> Above that, a string array stores every word of each sentence.
> And then another string of the structure at the next level stores every sentence.
> The last and most upper most string stores the entire content of the file... Maybe, it is also an array and can store multiple files similarily as above?

If someone can come up with such a code, it will be alot helpful for beginners and students like me to develop databases and file management systems for our projects.
Please Contribute as much as possible. :D
Last edited on
Don't duplicate post.
@TheIdeasMan, I don't think this is a duplicate post.
Well, not an exact duplicate, but the same question essentially.

@OP

Please don't do this: it is ultimately a time waster for those who reply.
> > The inner most structure variable is a character array that stores every character of the file.
> > Above that, a string array stores every word of each sentence.
> > And then another string of the structure at the next level stores every sentence.
> > The last and most upper most string stores the entire content of the file...

Once you have a container that holds every character in the file, it is easy to derive the rest (lines etc.) from that information. Most likely, all these structures would not be required simultaneously.

Something like this, perhaps:

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
// this is some (sample) text used for testing the code - for correctness.
// this is line #2

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <iterator>
#include <regex>

// read all characters in an input stream (file) into a string
std::string get_text( std::istream& stm )
{
    std::string result ;
    char c ;
    while( stm.get(c) ) result.push_back(c) ;
    return result ;
}

// get lines from the the text in the file in a vector
std::vector< std::string > split_into_lines( const std::string& text )
{
    std::vector< std::string > result ;

    std::istringstream stm(text) ;
    std::string line ;
    while( std::getline(stm,line) ) result.push_back(line) ;

    return result ;
}

// get words in a line of text in the file
std::vector< std::string > split_into_words( const std::string& line )
{
    std::vector< std::string >  words ;

    static std::regex words_re( "\\w+" ) ; // word: a sequence of one or more 'word' characters
    const std::sregex_iterator end ;
    for( std::sregex_iterator iter( line.begin(), line.end(), words_re ) ; iter != end ; ++iter )
        words.push_back( iter->str() ) ;

    return words ;
}

int main()
{
    if( std::ifstream file{__FILE__} )
    {
        // get the contents of the file
        const std::string text = get_text(file) ;
        std::cout << "the file contains " << text.size() << " characters:\n" << text << "---------------\n" ;

        // split it into lines
        const auto lines = split_into_lines(text) ;
        std::cout << "there are " << lines.size() << " lines\n" ;
        if( !lines.empty() )
        {
            std::cout << "the first line is: " << lines.front() << '\n' ;

            // split that line into words
            const auto words = split_into_words( lines.front() ) ;
            std::cout << "the " << words.size() << " words in this line are:\n" ;
            for( const auto& w : words ) std::cout << '\t' << w << '\n' ;
        }
    }
}

http://coliru.stacked-crooked.com/a/4b7a840e3460434e
Sorry, i did not know such a question already existed here as i tried searching but the exact objective/function of the program as i stated did not come up.. There were similar ones but they missed a few things. So i asked my own.
I don't understand what you're trying to achieve.
Programs rarely need to store the entire contents of a file. Can you tell us what problem you're trying to solve? there's a good chance that it can be done by reading the file sequentially.

Or to put it another way, is there something in the problem that requires you to know the first character and the last character of the file at the same time?
If the files are small enough, and it would make the program simpler, go ahead and store its entire contents in memory.

As they say, premature optimization is the root of all evil (attributed to Donald Knuth),
when in doubt, use brute force (Ken Thompson), clarity is better than cleverness (ESR) etc.
Sorry, i did not know such a question already existed here as i tried searching but the exact objective/function of the program as i stated did not come up.. There were similar ones but they missed a few things. So i asked my own.


I meant the other was question was your own & very similar:
http://www.cplusplus.com/forum/beginner/275904/
Topic archived. No new replies allowed.