0

Whenever I put a string and a vector in the same line, the string becomes empty. In my code I have,

string line, s1, s2; 
vector<pair<string,int> > binaryvector;
ifstream filename(uncompr_filename.c_str());

If I do

while(getline(filename, line))
{
    s1 = line.c_str();
    s2 = binaryvector[0].first.c_str();
    cout << s1 << endl;
    cout << s2 << endl;
}

it prints both the values of s1 and s2. But if I do,

while(getline(filename, line))
{
    s1 = line.c_str();
    s2 = binaryvector[0].first.c_str();
    cout << s1 << s2 << endl;
}

it prints just the s2 string. Where am I going wrong? This is just to illustrate the problem. What I am actually trying to do is to compare s1 and s2 using if(s1 == s2). But if I do that it returns false because s1 does not seem to have anything and is not equal to the s2 string.

Complete Code.

#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#define DICTIONARYSIZE 4

using namespace std;
typedef map<string,int> Instruction_Binaries;
struct val_greaterthan : binary_function < pair<string,int>, pair<string,int>, bool >
{
        bool operator() (const pair<string,int>& x, const pair<string,int>& y) const
        {return x.second > y.second;}
}val_gt;

int main(int argc, char *argv[])
{
    int c, i;
    Instruction_Binaries binary_count;
    string uncompr_filename, compr_filename, outfilename;

        if(argc <= 3)
        {
                cout << "Format is \"./SIM -c original.txt cout.txt\" or \"./SIM -d compressed.txt dout.txt\"" << endl;
                return 1;
        }
    while ((c = getopt (argc, argv, "c:d:")) != -1)
        switch (c)
        {
                case 'c':
                        (uncompr_filename=optarg);
                        break;
                case 'd':
                        (compr_filename=optarg);
                        break;
                default:
                        cout << "Format is \"./SIM -c original.txt cout.txt\" or \"./SIM -d compressed.txt dout.txt\"" << endl;
                        abort ();
                        break;
        }

    ifstream ifile(uncompr_filename.c_str());
    string binary, Directory_Index;

    while (ifile >> binary){
        int index;
        ++binary_count[binary];
    }

    vector<pair<string,int> > binaryvector;
    copy(binary_count.begin(), binary_count.end(), back_inserter(binaryvector));
    sort(binaryvector.begin(), binaryvector.end(), val_gt);

    while(ifile >> binary){
        int flag = 0;
        for(i=0; i<4; ++i){
                if(i==0) Directory_Index = "00";
                else if(i==1) Directory_Index = "01";
                else if(i==2) Directory_Index = "10";
                else if(i==3) Directory_Index = "11";
                if(binary == binaryvector[i].first){
                        cout << "001" << Directory_Index << endl;
                        flag=1;
                        break;
                }
        }
    if(flag == 0)
                cout << binary << endl;
    }
    return 0;
}
mrbubz
  • 427
  • 2
  • 6
  • 20
  • 1
    I assume binaryvector is initialized somewhere else? Also, why are you setting s2 equal to `binaryvector[0].first.c_str()` and not just `binaryvector[0].first`. (And are you sure s2 doesn't contain something like a \r as the first character? That might return the carriage to the beginning of the line, clearing out s1, but after a newline, it's invisible.) – Charlie Oct 24 '13 at 02:22
  • Yes, Sorry I did not put the complete code. I ll update the question – mrbubz Oct 24 '13 at 02:23
  • What if you do `cout << s1; cout << s2 << endl;`? – nhgrif Oct 24 '13 at 02:25
  • Even then it prints just the value of s2 – mrbubz Oct 24 '13 at 02:29
  • @Charlie, I tried just binaryvector[0].first. I saw the problem, so I thought maybe their types differ somehow. Bad try at a hack without completely understanding :| Very new to C++ – mrbubz Oct 24 '13 at 02:33
  • Anyone? The question just died – mrbubz Oct 24 '13 at 04:14
  • What's `val_gt`? A bad comparison predicate may cause memory corruption, and other nasty problems. – Igor Tandetnik Oct 24 '13 at 04:26
  • @IgorTandetnik val_gt is used to sort the lines based on their frequency. I have updated the question with the complete code. I am trying to build a frequency based dictionary and use it for compressing an input file based on matching. I re used a code online which was for arranging english words in increasing order of frequency (I modified it to suit my application. I cannot find the link to that code. I will post it when I do). – mrbubz Oct 24 '13 at 17:57
  • I think I have gotten the string comparison wrong. Can somebody please explain me if doing this is wrong? 'if(s1.compare(s2) != 0)' I give up on comment editing :/ – mrbubz Oct 24 '13 at 18:54
  • @Charlie. You were right! I was so stupid to not try what you told before. '\r' was causing it to happen! Thank you! http://stackoverflow.com/questions/2310939/remove-last-character-from-c-string – mrbubz Oct 24 '13 at 19:14

1 Answers1

0

The problem is somewhere else in your code, as this works:

#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <fstream>

using namespace std;

int main()
{
    string line, s1, s2; 
    vector<pair<string,int> > binaryvector;
    binaryvector.push_back({"heya", 5});
    ifstream filename("input.txt");

    while(getline(filename, line))
    {
        s1 = line.c_str();
        s2 = binaryvector[0].first.c_str();
        cout << s1 << s2 << endl;
    }
    return 0;
}

I ran this over on http://www.compileonline.com/compile_cpp11_online.php with an input file that looked like this:

This is the file you can use to provide input to your program and later on open it inside your program to process the input.
second line
third line

After a blank line!  Booya!
and a line without "ending" it

The output looked like this:

This is the file you can use to provide input to your program and later on open it inside your program to process the input.
heya
second line
heya
third line
heya
heya
After a blank line! Booya!
heya
and a line without "ending" itheya

This means that there is no problem with cout, but you're doing something else horribly wrong somewhere, since the "minimal" program I just made works 100% with your "constrained" problem.

Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54
  • Oh ya that works.. but this however did not. if(s1 == s2) cout << s1 << s2 << endl; Only change is the Comparison I am doing 'if(s1 == s2)' Where s1, I am reading line by line from a file which has 0000 1111 0101 1010 and 'binaryvector.push_back({"1111", 5});' s2 = binaryvector[0].first.c_str(); – mrbubz Oct 24 '13 at 18:46
  • Sorry for the poor comment editing. Still getting used to the format – mrbubz Oct 24 '13 at 18:47
  • Kevin, thanks for the help. Found out where the problem was. http://stackoverflow.com/questions/2310939/remove-last-character-from-c-string – mrbubz Oct 24 '13 at 19:16