0
在此代码中,字符长度突然变化。在介绍char 文件之前,strlen(str)是正确的。当我引入新的char文件时,变量str的strlen值发生变化。字符串长度突然变化
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(){
char buf[BUFSIZ];
if(!getcwd(buf,BUFSIZ)){
perror("ERROR!");
}
cout << buf << endl;
char *str;
str = new char[strlen(buf)];
strcpy(str,buf);
strcat(str,"/");
strcat(str,"input/abcdefghijklmnop");
cout << str << endl;
cout << strlen(str) << endl;
char *file;
file = new char[strlen(str)];
cout << strlen(file) << endl;
strcpy(file,str);
cout << file << endl;
}
这是什么问题? – Wilson
你的缓冲区'str'太短。你为什么不使用'std :: string'? –