65,183
社区成员




#include <stdio.h>
#include <fstream>
using namespace std;
int main()
{
char *read_buf = NULL;
ifstream fins("1.txt", ifstream::in);//“1.txt”的内容:“Hello world!<!----//----->Hello china!”
if (!fins.good())
{
fins.close();
return 0;
}
fins.seekg(0, ios::end);
int length = fins.tellg();
fins.seekg(0, ios::beg);
read_buf = new char[length + 1];
memset(read_buf, 0, length + 1);
if (read_buf)
fins.read (read_buf, length);
else
return 0;
char *end = strstr(read_buf, "<!");
*(end - 1) = '\0';
//*(end + 1) = '\0';//这样做是正确的,这很显然
fins.close();
printf("%s\n",read_buf);
system("pause");
return 0 ;
}
#include <stdio.h>
#include <fstream>
using namespace std;
int main()
{
char *read_buf = "Hello world!<!----//----->Hello china!";
char *end = strstr(read_buf, "<!");
*(end - 1) = '\0';
//*(end + 1)= '\0';//这样做,也是错误的,显然此处不是因为越界
printf("%s\n",read_buf);
system("pause");
return 0 ;
}
#include <stdio.h>
#include <fstream>
using namespace std;
int main()
{
char read_buf[] = "Hello world!<!----//----->Hello china!";//这样应该就没问题了
char *end = strstr(read_buf, "<!");
*(end - 1) = '\0';
//*(end + 1)= '\0';//这样做,也是错误的,显然此处不是因为越界
printf("%s\n",read_buf);
system("pause");
return 0 ;
}