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

标签 c++ std delimiter cout

我这样调用 std::cout:

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

但是我的输出是这样的:

 be put in the table??

我觉得模板行中可能有一个 '\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();
}

最佳答案

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;

关于c++ - 什么会导致 std::cout 在我的字符串中丢失一些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30678639/

相关文章:

c++ - 为什么没有 std::stou?

postgresql - 带文字定界符的 Postgres COPY 命令

elasticsearch - 有什么方法可以告诉 elasticsearch 标准分词器不要将 $、@、# 视为分隔符?

c++ - 模板类中的 Typedef 不起作用

php - ESP8266 读取 JSON,但不读取 PHP 文件

c++ - 从 C++ 中的成员函数返回指针

c++ - 完美转发对象数组

c++ - 对 "class"的引用不明确

c++ - 如何使用标准 C++ 从 UTF-8 转换为 ANSI

Java - 使用多个分隔符时的字符串解析或 split() 错误