简体   繁体   English

strcat替代问题C ++

[英]strcat alternative issue C++

I am working on a project which was initially sampled in C but want to work on it in C++. 我正在开发一个最初在C中采样但希望在C ++中使用它的项目。

There is a section where a strcat() is used, I have been told to use an alternative. 有一个部分使用了strcat(),我被告知要使用替代方案。 I have found one here , but when I try those the compiler gives me the following error: 我在这里找到了一个,但是当我尝试这些时,编译器给了我以下错误:

error: invalid operands of types char*' and char*' to binary `operator+' 错误: char*' and char *'类型的无效操作数到二进制`operator +'

Is there something I am doing wrong? 有什么我做错了吗?

Edit: 编辑:

Here's the portion of the code that doesn't work 这是代码中不起作用的部分

FILE *FileOpen(string *fname, string* mode){
FILE *fp;
string *str = "";

str += "tmp/"; //all files to be created in temporary sub directory
str += fname;
if((fp=fopen(str,mode))==NULL){
fprintf(stderr,"Cannot open file: %s\n", &fname);
exit(1);
}
FileReader(fname);
return(fp);
}

Edit 2: For those wondering why I have FileReader: it's for part 2 of the project. 编辑2:对于那些想知道为什么我有FileReader的人:它是项目的第2部分。 Disassembling a code. 反汇编代码。

You haven't shown any code, but I suspect you had something like this 你没有显示任何代码,但我怀疑你有类似的东西

char *s1 = "Hello, ", *s2 = "world!";
char buf[50];
strcpy(buf, s1);
strcat(buf, s2);

and now you changed it to 现在你把它改成了

char *s1 = "Hello, ", *s2 = "world!";
char buf[50];
buf = s1 + s2;

This doesn't work, as you already noticed. 正如您已经注意到的那样,这不起作用。 You must change the char pointers and char array to std::string as well 您还必须将char指针和char数组更改为std::string

std::string s1 = "Hello, ", s2 = "world!";
std::string buf = s1 + s2;

Thank you for posting your code; 感谢您发布您的代码; now the problem is readily apparent. 现在问题很明显了。

You should use string objects, not pointers to them. 您应该使用字符串对象,而不是指向它们的指针。

FILE *FileOpen(string fname, string mode)
{
    string str = "";

    str += "tmp/"; //all files to be created in temporary sub directory
    str += fname;
    FILE *fp = fopen(str.c_str(), mode.c_str());
    if (!fp) {
        fprintf(stderr, "Cannot open file: %s\n", fname.c_str());
        exit(1);
    }
    FileReader(fname);
    return fp;
}

A good next step would be to move to I/O functions that accept std::string arguments, so you don't have to say .c_str() everywhere. 一个好的下一步是移动到接受std::string参数的I / O函数,所以你不必在任何地方说.c_str()

I'm also confused why you have FileReader(fname) inside your file-opening function. 我也很困惑为什么你的文件打开功能中有FileReader(fname) That violates the Single-Responsibility-Prinicple, twice. 这违反了Single-Responsibility-Prinicple,两次。 Opening a file should not cause it to be read, and the code reading the file should use a FILE* and not care what the filename is (except perhaps for generation of error messages). 打开文件不应该导致它被读取,并且读取文件的代码应该使用FILE*而不关心文件名是什么(除了可能产生错误消息)。

If your code is using char * as strings, then strcat is probably the right function for you. 如果您的代码使用char *作为字符串,那么strcat可能是适合您的函数。 Of course, the C++ solution is to use std::string , in which case you can just use + - since there is binary operator+ available for std::string . 当然,C ++解决方案是使用std::string ,在这种情况下你可以使用+ - 因为有二进制operator+可用于std::string

well, C++ has a string class std::string whose operator+ performs concatenation. 好吧,C ++有一个字符串类std::string其operator +执行连接。 But you have to create one first. 但你必须先创建一个。

so the expression "abc" + "def" doesn't compile, but std::string("abc")+"def" works fine. 所以表达式"abc" + "def"不能编译,但是std::string("abc")+"def"工作正常。

alternatively you can write something like 或者你可以写类似的东西

std::string s("abc");
s += "def";

similarly, 同样,

std::string s = "abc";
s += "def";

if you want to concatenate a large amount of text, and care about the performance, consider using std::ostringstream . 如果你想连接大量的文本,并关心性能,可以考虑使用std::ostringstream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM