Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i also get some warning :
Warning 2:warning C4101: 'it2' : unreferenced local variable

Warning 1:warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

when build this project and click F5
see a blank windows and there is no file
what 's wrong here


XML
#include "stdafx.h"
#include<fstream>
#include <iostream>
#include <cctype>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#include<set>
#include <cstddef>
#include <sstream>


using namespace std;

std::string int_to_string(int);
// tabdil int  i into s C++ string
string int_to_string(int i) {
    stringstream out;
    out<< i ;

    return out.str();
}


//an index to a file.

class FileIndex {
 public:
  FileIndex(int);
  FileIndex* getNext();
  int getIndex();
  void setNext(FileIndex*);

 private:
  int index;       // The index of the file a word was found in.
  FileIndex* next; // The next file index.
};

// The class  for the inverted index generator.
class InvertedIndexGen {
 public:
    InvertedIndexGen();
    ~InvertedIndexGen();
    int build(const std::string&);
    FileIndex* lookup(const std::string&);
    void to_set(std:: set<int>&s, FileIndex* ) ;
    std::string toString();
    int numberOfWords();
 private:
    std::map<std::string, FileIndex*> idx;

    //
    int loadIndexFile(std::vector<std::string>&, const std::string&);
    int indexFiles(const std::vector<std::string>&);
    int readWords(const std::string&, std::vector<std::string>&);
    void insert(const std::string&, int);
};


InvertedIndexGen::InvertedIndexGen() {
    // Default constructor.
}


InvertedIndexGen::~InvertedIndexGen() {
    map<string, FileIndex*>::iterator it = idx.begin();
    // iterate over each map element pair.
    while (it != idx.end()) {
        FileIndex* fi = it->second;
        // Now, delete each file index.
        while (fi != NULL) {
            FileIndex* p = fi;
            fi = fi->getNext();
            delete p;
        }
        it++;
    }
    // Next, delete all map entries.
    idx.clear();
}


// Takes a file name as an argument and builds the inverted index.
int InvertedIndexGen::build(const string& file) {
    vector<string> files;
    if (loadIndexFile(files, file) == -1)
        return -1;
    if (indexFiles(files) == -1)
        return -1;
    return 0;
}



// Looks up a word in the inverted index.
FileIndex* InvertedIndexGen::lookup(const string& word) {
    return idx[word];
}











// Returns true if c is an alpha character.
bool alpha(char c) {
    return isalpha(c);
}

// Returns true if c is not an alpha character.
bool not_alpha(char c) {
    return !isalpha(c);
}





// This method splits the string str into a vector of strings.  That is, we
// split a sentence into words.
//
vector<string> split(const string& str) {
    vector<string> ret;
    string::const_iterator i = str.begin();
    while (i != str.end()) {
        // Ignore unrecognized characters (non-alpha):
        i = find_if(i, str.end(), alpha);

        // Find the next alpha word:
        string::const_iterator j = find_if(i, str.end(), not_alpha);

        // Copy the characters in [i, j)
        if (i != str.end())
            ret.push_back(string(i, j));

        i = j;
    }
    return ret;
}






// This method reads the words in the provided file into the vector v.
//
int InvertedIndexGen::readWords(const string& file, vector<string>& v) {
    std::ifstream infile("file.txt");
    if (infile) {
        std::string line;
        while (getline(infile, line)) {
            vector<string> words = split(line);
            v.insert(v.end(), words.begin(), words.end());
        }
        return 0;
    }
    else {
        cerr << "can't open file " << file << endl;
        return -1;
    }
}



//
// This method converts the FileIndex list fi into a set of
// integers representing the file indexes.
//
void InvertedIndexGen :: to_set(set<int>&s, FileIndex* fi) {
    FileIndex* p = fi;
    while (p != NULL) {
        s.insert(p->getIndex());
        p = p->getNext();
    }
}





// Indexes each file in the files vector.
int InvertedIndexGen::indexFiles(const vector<string>& files) {
    // Uncomment the following lines:
    vector<string> words; // Words in a file.
    set<string>    seen;  // Files we have "seen" (already indexed).
    int            fcnt;  // The file we are indexing.
    string         curr_file;
    string         curr_word;
    int status = 0;

    fcnt = 0;


    for (vector<string>::const_iterator file_it = files.begin(); file_it != files.end(); ++file_it){
        curr_file = *file_it;
        if(seen.find(curr_file) != seen.end()){
            seen.insert(curr_file);
            if(readWords(curr_file, words) != -1){
                for (vector<string>::iterator words_it = words.begin(); words_it != words.end(); ++words_it){
                    curr_word = *words_it;
                    insert(curr_word, fcnt);
                    words.clear();
                }
                fcnt++;
            }
            else {
                status = -1;
                fcnt++;
            }
        }
        else {
            status = -1;
            cout << "duplicate input file: " << curr_file << ". Skipping." << endl;
            fcnt++;
        }
    }
    return status;
}



// Inserts a word into the inverted index.
void InvertedIndexGen::insert(const string& word, int fcnt) {
    if(idx.find(word) == idx.end()) {                // word not seen
        FileIndex* newEntry = new FileIndex(fcnt);  // add new pair to idx map
        idx.insert(pair<string, FileIndex*>(word, newEntry) );
        return;
    }
    else {                                           // word has been seen
        FileIndex* curr = lookup(word);
        while(curr->getIndex() != fcnt && curr->getNext() != NULL){  // iterate through word's FileIndex objects
            curr = curr->getNext();
        }
        if((curr->getIndex() == fcnt)) {  // if there's an index match, do nothing
            return;
        }
        else {                        // if there's no match, add new FileIndex pointer to value list
            FileIndex* addIndex = new FileIndex(fcnt);
            curr->setNext(addIndex);
        }
    }
}



// Loads the index file into the vector files.
int InvertedIndexGen::loadIndexFile(vector<string>& files, const string& idxfile) {
    std::ifstream infile("file.txt");
    if (infile) {
        std:: string line;
        int lineno = 1;
        while (getline(infile, line)) {
            if (line == "")
                cerr << "[" << int_to_string(lineno)
                     << "] found blank line in input file. skipping." << endl;
            else
                files.push_back(line);
            lineno++;
        }
        return 0;
    }
    else {
        cerr << "can't open file " << idxfile << endl;
        return -1;
    }
}




// Creates a new file index given the index i.
//
FileIndex::FileIndex(int i) {
    index = i;
  next  = NULL;
}

//
// Returns the next file index in this list.
//
FileIndex* FileIndex::getNext() {
  return next;
}

//
// Sets the next file index in this list.
//
void FileIndex::setNext(FileIndex* n) {
  next = n;
}

//
// Returns the index.
//
int FileIndex::getIndex() {
  return index;
}


//Returns the string representation of the inverted index.
string InvertedIndexGen::toString() {
    set<int>indexes;
    string   res = "";
    map<string, FileIndex*>::iterator it = idx.begin();
    int *it2 ;
    while (it != idx.end()) {
        res += it->first + ": ";
        to_set(indexes, it->second);
        for (set<int>::iterator it2 = indexes.begin();
             it2 != indexes.end(); ++it2) {
            res += int_to_string(*it2) + " ";
        }
        res += "\n";
        indexes.clear();
        it++;
    }

    return res;
}

int InvertedIndexGen::numberOfWords() {
    return idx.size();
}




// Main program entry point.
int main(int argc, char* argv[]) {
    // Check the program arguments.
    if (argc != 2) {
        cerr << "usage: InvIndexer file" << endl;
        return 1;
    }
    cout << "Test" << endl;

    InvertedIndexGen ivgen;     // Create the inverted index generator.
    ivgen.build(argv[1]);       // Build the index.
    cout << ivgen.toString();   // Return the a string representation of the index.
    cout << ivgen.numberOfWords() << " words" << endl;
    return 0;
}
Posted
Updated 17-Dec-13 22:14pm
v3
Comments
[no name] 18-Dec-13 2:05am    
why any body don't answer me?
Richard MacCutchan 18-Dec-13 4:49am    
You are not paying for this, so just learn to be patient; people will answer in their own time. I already suggested to you in this question that you need to get hold of a book on C++, and study hard.

1 solution

There is no error during program execution. The messages from the debug output window inform you which modules have been loaded. With some modules, there is no debug information available. This is not an error. It is just an informative message.
 
Share this answer
 
Comments
CPallini 18-Dec-13 3:41am    
5.
Jochen Arndt 18-Dec-13 4:20am    
You must differentiate between compiler messages (like those from the above comment) and debug output when running your program (like those from your question).

With compiler messages, there is a line number. Go to to that line and see what might be wrong. When double clicking on the message, the source window will be opened and the cursor is positioned on that line. To get information about the error, click once on the message so that the cursor is on it and press F1.

Warning C4101: You declared a variable 'it2' but never used it.

Warning C4800: You are assigning an int to a bool. Depending on the logic of your code you should use a condition. Examples:
bool b = intVar; // Warning C4800
bool b = intVar != 0; // No warning

I did not understeand what you mean with your last statement. However, this and the questions about the warnings are not related to the original question. So it is better to write a new question.
nv3 18-Dec-13 4:18am    
What is so difficult to understand about "unreferenced local variable"?
[no name] 18-Dec-13 4:20am    
i don't know how fix it?
nv3 18-Dec-13 4:29am    
If a local variable is not referenced either forgot some part in your code and should add or fix that or the variable is totally unnecessary and you should remove it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900