什么会导致std :: cout丢失我的字符串中的一些字符

时间:2015-06-06 03:30:55

标签: c++ std delimiter cout

我正在调用std :: cout:

    cout << "will " << tempLine << " be put in the table??" << endl;    

但我的输出是这样的:

 be put in the table??

我觉得templine可能有一个'\ 0'字符,它会阻止std :: cout正常工作。这会导致输出吗​​?

我会将我的代码放在下面以供参考:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <map>

using namespace std;

int tempVarInst;
int tempVarDest;
int tempVarJmp = 0;
int lineCount=0;

map<string, int> symbolTable;




bool replace(std::string& str, const std::string& from, const std::string& to) {
    size_t start_pos = str.find(from);
    if(start_pos == std::string::npos)
        return false;
    str.replace(start_pos, from.length(), to);
    return true;
}

bool isComment(string line){
    string comment = "/";
    if((line.find(comment)) != string::npos){
        return true;
    }else{
        return false;
    }
}

string ConvertToBinary(unsigned int val)
{

    string tempStore;

   unsigned int mask = 1 << (sizeof(int) * 4 - 1);

   for(int i = 0; i < sizeof(int) * 4; i++){
      if( (val & mask) == 0 )

         tempStore+='0';

      else

         tempStore+='1';

      mask  >>= 1;
   }
   return tempStore;
}

bool isMemLoc(string line){
    string symbol = "@";
    if(line.find(symbol) != string::npos ){
        return true;
    }else{
        return false;
    }

}


int destConstants(string line){

    if(line== "A")
        return 32;
    if(line== "M")
        return 8;
    if(line== "D")
        return 16;
    if(line== "AM")
        return 40;
    if(line== "AD")
        return 48;
    if(line== "MD")
        return 24;
    if(line== "AMD")
        return 56;  
}

int isInst(string line){

    if(line=="0")
        return 60032;
    if(line=="1")
        return 61376;
    if(line=="-1")
        return 61056;
    if(line=="D")
        return 58112;
    if(line=="!D")
        return 58176;
    if(line=="M")
        return 64512;
    if(line=="A")
        return 60416;
    if(line=="!M")
        return 64576;
    if(line=="!A")
        return 60480;
    if(line=="-D")
        return 58304;
    if(line=="-M")
        return 64704;
    if(line=="-A")
        return 60608;
    if(line=="D+1")
        return 59328;
    if(line== "M+1")
        return 64960;
    if(line== "A+1")
        return 60864;
    if(line== "D-1")
        return 58240;
    if(line== "M-1")
        return 64640;
    if(line== "A-1")
        return 60544;
    if(line== "D+M")
        return 61568;
    if(line== "D+A")
        return 57472;
    if(line== "D-M")
        return 62656;
    if(line== "D-A")
        return 58560;
    if(line== "M-D")
        return 61888;
    if(line== "A-D")
        return 57792;
    if(line== "D&M")
        return 61440;
    if(line== "D&A")
        return 57344;
    if(line== "D|M")
        return 62784;
    if(line== "D|A")
        return 58688;   
}


int jmpConstants(string line){

    if(line== "JGT")
        return 1;
    if(line== "JEQ")
        return 2;
    if(line== "JGE")
        return 3;
    if(line== "JLT")
        return 4;
    if(line== "JNE")
        return 5;
    if(line== "JLE")
        return 6;
    if(line== "JMP")
        return 7;
}

bool isJumpInstruction(string line){

    string symbol = ";";
    if(line.find(symbol) != string::npos ){
        return true;
    }else{
        return false;
    }

}

void firstPass(ifstream &infile){
    string tempLine;
    int iterator=0;
    while (getline(infile, tempLine)){
        if(tempLine.substr(0,1) == "("){
        cout << "will " << tempLine << " be put in the table??" << endl;    
////////////////////////////////////
            for (map<string, int>::iterator it = symbolTable.begin();
    it != symbolTable.end(); ++it)
{
    cout << "ASDJASJASDJJSJAS:     " << it->first << endl;
    if (it->first == tempLine)
        continue;
    else {

        symbolTable.insert(pair<string, int>(tempLine, lineCount));
            lineCount++;
    }
}
/////////////////////////////

        for (map<string, int>::iterator it = symbolTable.begin();
    it != symbolTable.end(); ++it)
{
    cout << "HERE The Symbol Table is:    "<< it->first << " " << it->second << endl;
}
        }
        else
            lineCount++;
    }
    cout << "end file" << endl;
}


int main( int argc, const char* argv[] )
{
    string outLine;
    string file1 = argv[1];
    replace(file1, "asm", "hack");

    //input
    //WHILE READ LINE()
    ifstream infile(argv[1]);
    string tempLine;

    ofstream outfile(file1.c_str());
    tempVarJmp=0;
    tempVarDest=0;
    tempVarInst=0;
    firstPass(infile);

    ifstream secondPass(argv[1]);
    ofstream secondOut(file1.c_str());

    cout << "hi" << endl;
    while (getline(secondPass, tempLine)){
            //cout << "current line: " << tempLine << " length: " << tempLine.length() << endl;
            tempLine = tempLine.substr(0, tempLine.length()-1); 
        //Blank check
        //cout << "The tempLine is: " << tempLine << endl;
        if(tempLine.length()<=1){
            //cout << "Checking blackspaces, tempLine is: " << tempLine << endl;
            continue;

        }
        //Comment Check
        else if(isComment(tempLine)){
            //cout << "Checking comments, tempLine is: " << tempLine << endl;
            continue;
        }
        //@ Check
        else if(isMemLoc(tempLine)){
            tempLine.erase(0,1);
            int number = (atoi(tempLine.c_str()));
            //cout << "number: " << number << "binary: "<< ConvertToBinary(number) << endl;
            outfile << ConvertToBinary(number) << std::endl;
            continue;
            }

            //tempLine=tempLine(isInst).c_str+tempLine(destConstants)+tempLine(jmpConstants);
            //outfile << ConvertToBinary(tempLine) << endl;

        else if(isJumpInstruction(tempLine)){

            //  cout << "Jump Instruction" << endl;
                tempVarJmp = jmpConstants(tempLine.substr(tempLine.find(';')+1, tempLine.length()));
                cout << tempLine << " " << tempLine.length();
            //  cout << "TempVarInst: " << tempLine.substr(tempLine.find(';')+1) << endl;
            //  cout << "tempVarJmp: " << tempVarJmp << endl;
                tempVarDest = isInst(tempLine.substr(0,tempLine.find(';')));

            //  cout << "tempvarDest = " << tempVarDest << endl;

                int finalVal=tempVarDest+tempVarJmp;
                outfile << ConvertToBinary(finalVal) << std::endl;

        ///     cout << "TempVarJmp is: " << tempVarJmp << endl;
        //      cout << "output should be: " << ConvertToBinary(finalVal) << endl;
                //cout << ConvertToBinary(finalVal) << endl;
            }else{

            //  cout << "Right TempLineInst is: " << tempLine.substr(tempLine.find('=')+1,tempLine.length()) << endl;
            //  cout << "Right TempLineDest is: " << tempLine.substr(0,tempLine.find('=')) << endl;

                tempVarInst = isInst(tempLine.substr(tempLine.find('=')+1,tempLine.length()));
                tempVarDest = destConstants(tempLine.substr(0,tempLine.find('=')));
                tempVarJmp = 0;
                if(tempLine.substr(1,2) == ";J"){
                    tempVarJmp = jmpConstants(tempLine.substr(2,4));
                //  cout << "TempLineDest is: " << tempLine.substr(2,4) << endl;

                }
                //cout << "tempvarInst = " << tempVarInst << endl;
                //cout << "tempvarDest = " << tempVarDest << endl;
                //cout << "tempvarJmp = " << tempVarJmp << endl;
                int totalSum = tempVarInst+tempVarDest+tempVarJmp;
                //cout << "totalSum: " << ConvertToBinary(totalSum) << endl;

                outfile << ConvertToBinary(totalSum) << std::endl;
            }
    }

    outfile.close();
}

1 个答案:

答案 0 :(得分:4)

NUL能否以某种方式阻止或清除输入取决于您的终端程序。回车符(ASCII码13)是原因的一个更强的候选者,因为它几乎普遍由返回当前行最左列的终端处理。

为了找到答案,我建议你编写一个函数,一次输出一个字符串,使用转义(八进制控制字符),就像在程序中指定字符串文字时一样:

if (c == '"' || c == '\')
    os << '\\';
if (std::isprint(c))
    os << c;
else
    os << '\\' << std::setw(3) << std::setfill('0')
       << std::oct << (int)(unsigned char)c;