Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

File for loop doesn't open file c++

This file is not opening for a reason which I don't know really, any insight?

#include <iostream>
#include <string>
#include <algorithm> 
#include <fstream>
#include <cctype>
using namespace std; 
.
.
.
.
.
void MinHeap::TopKFrequentWord(string fileName, int k)
{
    MinHeap mh;
    Trie T;
    string word;
    
    string line;
    
    ifstream inFile(fileName);
    for (int i = 0; i < 22; i++)
    {
        if (i >= 10)
        {
            fileName = "C:\Users\Kareem's Laptop\Desktop\Reuters-21578\reut2-0" + to_string(i) + ".sgm";

        }

        else if (i <= 9)
        {
            fileName = "C:\Users\Kareem's Laptop\Desktop\Reuters-21578\reut2-00" + to_string(i) + ".sgm";


        }

        if (!inFile)
        {
            cout << fileName << " did not open." << endl;
            exit(1);
        }

        bool found = true;

        while (inFile >> line)
        {

            size_t pos = line.find("<BODY>");

            if (pos != string::npos)
            {
                if (found)
                {
                    word = line.substr(pos + 6);

                    found = true;

                    TrieNode* TN = T.search(word);

                    if (!TN)
                    {
                        TN = T.insert(word);
                    }
                    else
                    {
                        TN->frequency++;
                    }
                    mh.insert(TN, word);
                }
            }
        }
        mh.Display();
        cout << '
';
        inFile.close();
    }
}
    
int main()
{

    MinHeap foo; 
    string fileName; 
    foo.TopKFrequentWord(fileName, 10);
    return 0;
}

I have to open 21 files in a loop, read them all and print out the top 10 word count for all of those words. Unable to use vector due to instructions. I apologize if similarities are obvious. I tried putting all files in an array but it still didn't work. No errors just not opening (getting exit(1) command).

question from:https://stackoverflow.com/questions/65832271/file-for-loop-doesnt-open-file-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are opening the file before the loop. Since you are updating the filename variable inside the loop, I suppose you want to open it each time you pass through the loop.

Move the line :

ifstream inFile(fileName);

to one line before the test:

if (!inFile)


Also, the place where you code ifstream inFile(fileName); you have an empty string in fileName (You didn't initialize the variable passed as argument in main).

Also, you are passing a int k parameter to the function but never uses it there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...