C++ 什么会导致std::cout在字符串中丢失一些字符
我打电话给std::cout是因为:C++ 什么会导致std::cout在字符串中丢失一些字符,c++,std,delimiter,cout,C++,Std,Delimiter,Cout,我打电话给std::cout是因为: cout << "will " << tempLine << " be put in the table??" << endl; 我觉得templine中可能有一个“\0”字符,它会停止std::cout正常工作。这会导致上述输出吗 我将把我的代码放在下面以供参考: #include <stdio.h> #include <stdlib.h> #include <
cout << "will " << tempLine << " be put in the table??" << endl;
我觉得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();
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int tempVarInst;
int tempVarDest;
int tempVarJmp=0;
int lineCount=0;
地图符号化;
bool替换(std::string&str,const std::string&from,const std::string&to){
size\u t start\u pos=str.find(from);
if(start_pos==std::string::npos)
返回false;
str.replace(起始位置,从.length()到);
返回true;
}
布尔值(字符串行){
字符串注释=“/”;
if((line.find(comment))!=string::npos){
返回true;
}否则{
返回false;
}
}
字符串转换二进制(无符号int-val)
{
字符串存储;
无符号整数掩码=1>=1;
}
返回临时存储区;
}
布尔isMemLoc(字符串行){
字符串符号=“@”;
if(line.find(symbol)!=string::npos){
返回true;
}否则{
返回false;
}
}
int常量(字符串行){
如果(第行=“A”)
返回32;
如果(第行=“M”)
返回8;
如果(第行=“D”)
返回16;
如果(行==“AM”)
返回40;
如果(行==“AD”)
返回48;
如果(行==“MD”)
返回24;
如果(行==“AMD”)
返回56;
}
int isInst(字符串行){
如果(行==“0”)
返回60032;
如果(第1行=“1”)
返回61376;
如果(行==“-1”)
返回61056;
如果(第行=“D”)
返回58112;
如果(行==“!D”)
返回58176;
如果(第行=“M”)
返回64512;
如果(第行=“A”)
返回60416;
如果(行==“!M”)
返回64576;
如果(行==“!A”)
返回60480;
如果(行==“-D”)
返回58304;
如果(行==“-M”)
返回64704;
如果(行==“-A”)
返回60608;
如果(行==“D+1”)
返回59328;
如果(行==“M+1”)
返回64960;
如果(行==“A+1”)
返回60864;
如果(第行=“D-1”)
返回58240;
如果(直线=“M-1”)
返回64640;
如果(行==“A-1”)
返回60544;
如果(直线=“D+M”)
返回61568;
如果(第行==“D+A”)
返回57472;
如果(直线=“D-M”)
返回62656;
如果(第行=“D-A”)
返回58560;
如果(行==“M-D”)
返回61888;
如果(行==“A-D”)
返回57792;
如果(第行=“D&M”)
返回61440;
如果(第行=“D&A”)
返回57344;
如果(直线=“D | M”)
返回62784;
如果(第行=“D | A”)
返回58688;
}
int jmpConstants(字符串行){
如果(行==“JGT”)
返回1;
如果(行==“JEQ”)
返回2;
如果(行==“JGE”)
返回3;
如果(行==“JLT”)
返回4;
如果(行==“JNE”)
返回5;
如果(行==“JLE”)
返回6;
如果(行==“JMP”)
返回7;
}
bool isJumpInstruction(字符串行){
字符串符号=“;”;
if(line.find(symbol)!=string::npos){
返回true;
}否则{
返回false;
}
}
首次通行无效(ifstream&infile){
弦模板;
int迭代器=0;
while(getline(内嵌、模板行)){
if(templane.substr(0,1)=“(”){
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;
if(c=='''''|c=='\')
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;
if(c=='''''|c=='\')
操作系统文件可能有CRLF行尾,而您不在Windows上。当您硬编码字符串时会发生什么情况?我现在运行的是Red Hat Linux,在代码中是否有一个简单的解决方法,即忽略CRLF行尾?Hi Beta,我会帮您检查now@BarneyChambers,从中删除回车符(结尾)字符串?很可能文件有CRLF行结尾,而您不在Windows上。当您硬编码字符串时会出现什么情况?我现在运行的是Red Hat Linux,在代码中是否有一个简单的解决方法,即忽略CRLF行结尾?Hi Beta,我将为您检查now@BarneyChambers,从中删除回车符(结尾)字符串?+1,这是一种分析问题原因的有建设性的方法。(顺便说一句,断点和查看templine的内容也可以完成这项工作。)谢谢托尼,我会给你一个这样的选择,这是一个很好的选择。也许是我,但是我不熟悉控制字符的八进制表示。克里斯:好问题-我不知道为什么八卦变得流行,但是现在它是C的传统,因此C++有十几个字符。scape序列,如回车符的\r
和水平制表符的\t
,但八进制方便地覆盖了所有ASCII字符。标准ala\x12
现在要求支持十六进制-我不确定他们为什么不为十进制添加一些符号。也许更多的人知道他们的8倍表使其成为如果您确实需要转换回十进制,请输入sier than hex.+1,以获得分析问题原因的建设性方法(