C++中的字符串串接问题

C++中的字符串串接问题,c++,string,concatenation,C++,String,Concatenation,我试图连接一些字符串,但它在一个字符串中有效,而在另一个字符串中无效 工作:我接受2个参数,然后这样做。a=你好,b=世界 string concat = a + b; 输出将是hello world,没有问题 不工作:我从文件中读取并与第二个参数连接。假设文件中的字符串是abcdefg string concat = (string from file) + b; 它给了我worldfg b中的字符串将覆盖初始字符串,而不是连接 我尝试过其他一些方法,比如使用stringstream,但效

我试图连接一些字符串,但它在一个字符串中有效,而在另一个字符串中无效

工作:我接受2个参数,然后这样做。a=你好,b=世界

string concat = a + b;
输出将是hello world,没有问题

不工作:我从文件中读取并与第二个参数连接。假设文件中的字符串是abcdefg

string concat = (string from file) + b;
它给了我worldfg

b中的字符串将覆盖初始字符串,而不是连接

我尝试过其他一些方法,比如使用stringstream,但效果不太好

这是我的密码

int main (int nArgs, char *zArgs[]) {
    string a = string (zArgs [1]);
string b = string (zArgs [2]);
    string code;

    cout << "Enter code: ";
cin >> code;

    string concat = code + b;
}
// The output above gives me the correct concatenation.
// If I run in command prompt, and do this. ./main hello world
// then enters **good** after the prompt for code.
// The output would be **goodworld**
但是,我从文件中读到了一些行

 string f3 = "temp.txt";
 string r;
 string temp;

 infile.open (f3.c_str ());

 while (getline (infile, r)) {
    // b is take from above
temp = r + b;
    cout << temp << endl;
 }
 // The above would give me the wrong concatenation.
 // Say the first line in temp.txt is **quickly**.
 // The output after reading the line and concatenating is **worldly**
希望它能给出更清楚的例子

更新:

我想我可能已经发现问题是由文本文件引起的。我试图创建一个新的文本文件,里面有一些随机的行,它似乎工作得很好。但是如果我试图读取原始文件,它会给我错误的输出。我还在想办法解决这个问题

然后我尝试将原始文件的内容复制到新文件中,它似乎工作正常。不过,我不太确定这里出了什么问题。将继续测试,希望它工作正常

谢谢你的帮助!谢谢你

如果我编译这个

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main (int nArgs, char *zArgs[]) {
   string a = string (zArgs [1]);
   string b = string (zArgs [2]);
   string code;

   cout << "Enter code: ";
   cin >> code;

   string concat = code + b;
   // The output above gives me the correct concatenation.

   //However, I read some lines from the file.
   ifstream infile;

   string f3 = "temp.txt";
   string r;
   string temp;

   infile.open (f3.c_str ());

   while (getline (infile, r)) {
      temp = r + code;
      cout << temp << endl;
   }
   // The above would give me the wrong concatenation.
   infile.close();
   return 0;
}
它编译和运行完美无瑕。这在你的计算机上做什么?如果失败,我们可能需要比较temp.txt的内容

这应该是一个评论,而不是一个答案,但它太长了。对不起。

如果我编译这个

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main (int nArgs, char *zArgs[]) {
   string a = string (zArgs [1]);
   string b = string (zArgs [2]);
   string code;

   cout << "Enter code: ";
   cin >> code;

   string concat = code + b;
   // The output above gives me the correct concatenation.

   //However, I read some lines from the file.
   ifstream infile;

   string f3 = "temp.txt";
   string r;
   string temp;

   infile.open (f3.c_str ());

   while (getline (infile, r)) {
      temp = r + code;
      cout << temp << endl;
   }
   // The above would give me the wrong concatenation.
   infile.close();
   return 0;
}
它编译和运行完美无瑕。这在你的计算机上做什么?如果失败,我们可能需要比较temp.txt的内容


这应该是一个评论,而不是一个答案,但它太长了。抱歉。

我得到的结果与提出原始问题的人相同:

$ ./a.out hello world
Enter code: good
goodworld
worldly
这里的问题是文本文件的内容。对于我的示例,文本文件中最初的7个字符是:quick。但是,紧接着是7个退格字节hex 08。以下是emacs中的内容:

quickly^H^H^H^H^H^H^H
那么这是如何造成混乱的呢

连接操作实际上是正确的。如果您这样做:

std::cout << "string length: " << temp.size() << "\n";
…您得到的答案是19,它由以下部分组成:快速7+7退格字符+世界5。当您将这个19个字符的字符串打印到console时,您观察到的覆盖效果是由以下原因引起的:console eg xterm将退格序列解释为意味着将光标移回左侧,从而删除前面的字符。相反,如果将输出通过管道传输到文件,您将看到实际生成的完整字符串(包括退格)

要解决这个问题,您可能需要验证/更正来自该文件的输入。在C/C++环境中,有一些常用的函数,如isprintint C、iscntrlint C,您可以使用这些函数


更新:正如另一个响应者所提到的,其他ASCII控制字符也将具有相同的效果,例如,回车十六进制0D也会将光标移回左侧。

我得到的输出与提出原始问题的chap相同:

$ ./a.out hello world
Enter code: good
goodworld
worldly
这里的问题是文本文件的内容。对于我的示例,文本文件中最初的7个字符是:quick。但是,紧接着是7个退格字节hex 08。以下是emacs中的内容:

quickly^H^H^H^H^H^H^H
那么这是如何造成混乱的呢

连接操作实际上是正确的。如果您这样做:

std::cout << "string length: " << temp.size() << "\n";
…您得到的答案是19,它由以下部分组成:快速7+7退格字符+世界5。当您将这个19个字符的字符串打印到console时,您观察到的覆盖效果是由以下原因引起的:console eg xterm将退格序列解释为意味着将光标移回左侧,从而删除前面的字符。相反,如果将输出通过管道传输到文件,您将看到实际生成的完整字符串(包括退格)

要解决这个问题,您可能需要验证/更正来自该文件的输入。在C/C++环境中,有一些常用的函数,如isprintint C、iscntrlint C,您可以使用这些函数


更新:正如另一个响应者所提到的,其他ASCII控制字符也将具有相同的效果,例如,回车十六进制0D也会将光标移回左侧。

请提供更完整的代码。您的示例代码在MSVC中运行良好。你在用什么编译器?@user1778855,很明显你做错了什么,但是没有人能从你发布的信息中分辨出它是什么。请发布一个完整的程序,说明输入是什么,说明输出是什么,并说明您期望输出是什么。不要只说“它给了我错误的答案”,这不会告诉任何人任何事情。执行此操作,您的问题会很快得到回答。@john实际上它是正确的文件,但可能是因为该文件是在Windows中创建的,然后从linux读取的?导致直接从linux创建新文件
. 我认为可能性很大。@user1778855好的,明白了!Windows和Linux之间的区别在于行尾的表示方式,在Windows中使用两个字符\r和\n,在Linux中仅使用\n。现在猜猜a\r对Linux有什么影响。它使光标移动到行的开头!因此,在您的输出中,一个字符串似乎覆盖了另一个字符串!请提供更完整的代码。您的示例代码在MSVC中运行良好。你在用什么编译器?@user1778855,很明显你做错了什么,但是没有人能从你发布的信息中分辨出它是什么。请发布一个完整的程序,说明输入是什么,说明输出是什么,并说明您期望输出是什么。不要只说“它给了我错误的答案”,这不会告诉任何人任何事情。执行此操作,您的问题会很快得到回答。@john实际上它是正确的文件,但可能是因为该文件是在Windows中创建的,然后从linux读取的?导致直接从linux创建新文件。我认为可能性很大。@user1778855好的,明白了!Windows和Linux之间的区别在于行尾的表示方式,在Windows中使用两个字符\r和\n,在Linux中仅使用\n。现在猜猜a\r对Linux有什么影响。它使光标移动到行的开头!因此,在您的输出中,一个字符串似乎覆盖了另一个字符串!