Problems with streaming file from folder & more

Hello, first time poster long time reader.

The idea of the program is to accept a file_name from the user and find it within the "text" folder. To count the words and characters then display the info to the user organized in some way.

all the comments are for testing or unimplemented.

Here's the code:

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
#include <iostream>
#include <fstream>


using namespace std;

int main()
{
    //long long int letter_count = 0;
    long long int word_count = 10000;
    long long int final_word_count = 0;
    string word_array[word_count];
    //char letter_array[letter_count];
    string file_name = "";

    getline (cin, file_name);
    cout << "\n";

    ifstream subject_text(file_name);
    //ifstream test("hum");

    //cout << file_name << "\n";

    if (!subject_text.is_open()){
        cout << "couldnt open subject text file stream.\n";
       return 1;
    }

    for(word_count = 0; word_count < 10000; word_count++)
    {
        subject_text >> word_array[word_count];

        if (word_array[word_count] != "")
        {
            cout << "\t" << word_count << ": " << word_array[word_count] << "\n";
        }

        else if (word_array[word_count] == "")
            break;
            final_word_count = word_count;
    }


    cout << "\n\t" << final_word_count << " strings in " << file_name << "\n";

    subject_text.clear();
    //test.clear();

    subject_text.close();
    //test.close();

    return 0;
}


Let me mention the "int"s are "long long" because eventually I would like to count a book. And I would like to feel confident I will have enough room in the word_array to not worry about any problems that may arise from not having enough room for each word.

This works but there's some problems I would like to address.

First of all, I can't figure out how to set a path to the folder for file_name without declaring the file name.

these don't work:
1
2
ifstream subject_text("../text", file_name)
ifstream subject_text("../text" file_name)


Also, what should I do to allow the program to count the words of a text file without knowing how many words are in it. Without changing word_count < 10000 every time I count a bigger text file.

I don't remember what exactly I did to try that out but it was something like this
1
2
3
4
5
6
word_count = 1;
while (word_count > 0)
{
    subject_text >> word_array[word_count];
    word_count++;
}

but breaks down after I cin file_name


And how would I go about sorting my strings according to rate of occurrence or alphabetically. maybe qsort? I plan to implement an option for either based on user input.


Lastly Should I use gcount() for counting my characters?


I'm not fluent in C++ so spoon feed it to me if you will.

Thanks for the help.
Last edited on
First of all, I can't figure out how to set a path to the folder for file_name without declaring the file name.

The solution to this one is simple, don't. Every major operating system today has it's own method of resolving the path to a file. In Windows for example, first the local directory is checked, if the file is not found then the directories listed under the 'Path' environment variable are searched.

what should I do to allow the program to count the words of a text file without knowing how many words are in it.

Use the variable sized containers like std::deque. Also consider only adding unique instances of words to your container and increment a counter in a custom class when you find repeats.

Another thing is I can't stream a "txt" file. I can only stream a "file" file. How would I go about searching for a text file for the ease of some copy -> paste action instead of saving a text file as a ".file".

I have no idea what you are trying to say here. Please rephrase.

Lastly Should I use gcount() for counting my characters?

This one is dealers choice. I can think of a half dozen different ways to do this and none of them are noticeably better then the rest.
Another thing is I can't stream a "txt" file. I can only stream a "file" file. How would I go about searching for a text file for the ease of some copy -> paste action instead of saving a text file as a ".file".


Disregard that I figured it out.
Last edited on
The solution to this one is simple, don't. Every major operating system today has it's own method of resolving the path to a file. In Windows for example, first the local directory is checked, if the file is not found then the directories listed under the 'Path' environment variable are searched.
It's quite the opposite, actually. Ignoring path separator differences, relative paths work exactly the same everywhere. A file name without path refers to a file in the working directory.
Absolute paths are platform-dependent.

PATH is only used to find executables (programs, dynamic libraries), never data files.
PATH is only used to find executables (programs, dynamic libraries), never data files.

You got me there, I should have thought of that and known to point it out. I still say don't do anything to hard code the path though.
Okay so I've been reading a lot and I still can't seem to stream a file from a folder. Any tips?

But I have progressed a little bit further and would like to share my progress and ask for a little bit of help with a new problem that has arisen.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

    string clean_string;
    long long int final_count = 0;


//erases non-letters and takes out spaces.
string String_cleanup(string dirty_string)
{
    string dirt_chars = "?!.,-=""""/\\";

    while(dirty_string.find_first_of(dirt_chars) != string::npos) {

        int found_chars = dirty_string.find_first_of(dirt_chars);
        dirty_string[found_chars] = ' ';
        int shrink_string = dirty_string.find_first_of(' ');
        dirty_string.erase(dirty_string.begin() + shrink_string);
    }

    dirty_string.shrink_to_fit();

    if (dirty_string.find_first_of(dirt_chars) == string::npos)
        return dirty_string;

    clean_string = dirty_string;
    return clean_string;

}

//counts letters
string Letter_count(string string_to_count)
{
    long long int letter_count = 0;
    int check_pos = 0;

    for(check_pos == 0; check_pos < string_to_count.size(); check_pos++)
        letter_count = check_pos;

    final_count += ++letter_count;
    cout << final_count << "\n";

}


int main()
{
    long long int word_count = 0;
    string file_name = "";

    getline (cin, file_name);
    cout << "\n";


    ifstream subject_text(file_name);


    if (!subject_text.is_open()){
        cout << "couldnt open subject text file stream.\n";
        return 2;
    }

    vector<string> string_vector;
    string word;

//passes word to string cleanup then displays it
    while(subject_text >> word)
    {
        word = String_cleanup(word);

        if (!word.empty()){
            string_vector.push_back(word);
            Letter_count(word);
        }
    }

//displays progressive word count
    for(word_count == 0; word_count < string_vector.size(); word_count++)
    {
        cout << "\n\t" << word_count << ": ";
        cout << string_vector.at(word_count);
    }

//final count
    cout << "\n";
    word_count--;
    cout << "\n\t\t" << word_count << " strings in " << file_name << "\n";
    cout << "\n\t\t" << final_count << " characters in " << file_name << "\n";

    subject_text.clear();

    subject_text.close();


    return 0;
}


The long long int final_count = 0 in int main() seems to allow the program to function correctly. But since I declared one globally I thought I should delete the redundant declaration in int main() but when I do delete that line it compiles and runs but breaks after I put data into file_name. And gives me a segmentation fault.

If someone could point me in the right direction it would be much appreciated. Maybe I've been looking at it for too long.


And Thanks to Computergeek01, helios for the advice and replies.

Last edited on
Previous problem solved.

I changed

1
2
3
4
5
6
7
8
9
10
11
12
string Letter_count(string string_to_count)
{
    long long int letter_count = 0;
    int check_pos = 0;

    for(check_pos == 0; check_pos < string_to_count.size(); check_pos++)
        letter_count = check_pos;

    final_count += ++letter_count;
    cout << final_count << "\n";

}


to

1
2
3
4
5
6
7
8
9
10
11
void Letter_count(string string_to_count)
{
    long long int letter_count = 0;

    for(int check_pos = 0; check_pos < string_to_count.size(); check_pos++)
        letter_count = check_pos;

    final_count += ++letter_count;
    cout << final_count << "\n";

}


And I was also able to remove the redundant long long int final_count
Topic archived. No new replies allowed.