小编Nor*_* Me的帖子

字符串的'\n'太多了

我创建了一个名为FileReader的类.这是我在这个班级的阅读功能.它打开一个文件并读取它.当然,它将文件的内容放在我的类的"内容"变量中.它在最后一行.

std::string file_content;
std::string temp;
std::ifstream file;

file.open(filepath,std::ios_base::in);

while(!file.eof()){

    temp.clear();
    getline(file, temp);

    file_content += temp;
    file_content += '\n';
}

file_content = file_content.substr(0, file_content.length()-1); //Removes the last new line

file.close();

content = file_content;
Run Code Online (Sandbox Code Playgroud)

我打开的文件包含以下内容:

"你好\n什么事了?nCool".

当然我没有在我的文本文件中完全写出\n.但正如你所看到的那样,最后没有新的界限.

我的问题是,每当我将它打印到屏幕上时,"内容"都会在最后添加一个新行.但我删除了最后一个新行...出了什么问题?

c++

1
推荐指数
1
解决办法
90
查看次数

Xml Parser - string :: find

我试图解析一个包含我的XML文件行的字符串.

std::string temp = "<Album>Underclass Hero</Album>";
int f = temp.find(">");
int l = temp.find("</");
std::string _line = temp.substr(f + 1, l-2);
Run Code Online (Sandbox Code Playgroud)

这是我的函数代码的一部分,它应该实际返回解析后的字符串.我所期待的是它返回Underclass Hero.相反,我得到了Underclass Hero </ Alb
(这里是'<'和'/'之间的空格,因为我无法将它们一起编写).

我看了std :: string :: find几次,它总是说它返回,如果存在,第一个匹配的第一个字符的位置.这里它给了我字符串的最后一个字符,但只在我的变量l中.
f确实很好.

链接到std :: string :: find

所以有人能告诉我我做错了什么吗?

c++ string

0
推荐指数
2
解决办法
685
查看次数

我的strcpy出了什么问题?

我试着strcpy自己做.它应该工作,我甚至复制和粘贴(几乎确切的代码)来自这里的某些人 strcpy.两者都给我一个"分段错误".

char* strcpy(char * destination, const char * source)
{
    while( (*destination++ = *source++) != '\0' ) 
        ;
    return destination;
}
Run Code Online (Sandbox Code Playgroud)

这段代码出了什么问题?

char* a = "hello";
cout << strcpy(a, "Haha") << endl;
Run Code Online (Sandbox Code Playgroud)

c++ strcpy

0
推荐指数
1
解决办法
568
查看次数

C++这段代码做了什么?

在我的书中有一段我不理解的代码.遗憾的是没有解释它的作用......

int big = 200;
int small = 100;
int max = (a <= b) ? big : small;
Run Code Online (Sandbox Code Playgroud)

所以我所知道的是有3个变量(lol ..).
整数'max'得到big的值,但为什么呢?(a <= b)显然是一个条件,但是什么'?' 和':'做什么?

我希望有人能解释一下......

c++

-5
推荐指数
1
解决办法
129
查看次数

标签 统计

c++ ×4

strcpy ×1

string ×1