C++ MinGW中长度超过15个字符的字符串出现问题 问题描述
我对C++ MinGW中长度超过15个字符的字符串出现问题 问题描述,c++,string,mingw,getline,C++,String,Mingw,Getline,我对std::getline()函数有问题。我是这样使用它的: std::fstream f; // opened in read in text mode std::string name; std::getline(f, name); 所有内容都适用于包含短文本的文件,例如: example text 但当我尝试阅读更长的内容时,f.e.: example text in file longer than 15 我得到(编译并运行后): 文件夹 main.cpp: #include &
std::getline()
函数有问题。我是这样使用它的:
std::fstream f; // opened in read in text mode
std::string name;
std::getline(f, name);
所有内容都适用于包含短文本的文件,例如:
example text
但当我尝试阅读更长的内容时,f.e.:
example text in file longer than 15
我得到(编译并运行后):
文件夹
main.cpp:
#include <string>
#include <cstring>
#include "plik.h"
#include "prowadzacy.h"
using namesapce std;
int main(int argc, char** argv) {
if (argc == 3 && !strcmp(argv[1], "-f")) {
Prowadzacy* baza = nullptr;
string fn(argv[2]);
czytajZPliku(fn, baza);
}
}
przedmiot.h:
struct Przedmiot {
std::string nazwa;
enum Rodzaj {
WYKLAD, CWICZENIA, LABORATORIUM, PROJEKT, SEMINARIUM
} rodzaj;
enum DzienTygodnia {
PONIEDZIALEK, WTOREK, SRODA, CZWARTEK, PIATEK, SOBOTA, NIEDZIELA
} dzien_tygodnia;
int godzina;
int czas_trwania;
std::string miejsce;
Przedmiot* next = nullptr;
};
plik.h:
#include <fstream>
#include <iostream>
#include "prowadzacy.h"
#include "przedmiot.h"
bool zapiszDoPliku (const std::string, Prowadzacy*&);
bool czytajZPliku (const std::string, Prowadzacy*&);
我使用Makefile和MinGW。使用标志编译-std=c++14-O3-g-pedantic errors
问题更新
我编写了一个简单的新程序:
#include <string>
#include <iostream>
int main () {
std::string str = "123456789012345";
std::cout << str << std::endl;
return 0;
}
在VisualStudio中,一切正常。为什么在MinGW它不起作用
编译时使用:
g++-std=c++14-学究式错误-Wall-Wextra-O3-g-学究式错误-o main.cpp
更新:我也在Linux上测试了这个。在Linux上,一切都很好。问题只存在于Windows上的MinGW中
解决方案
将-std=c++14
添加到编译命令中可解决此问题。使用相关功能(从标准标题),例如
避免使用原始char*
缓冲区
还要注意,在2018年(您可以期望您的用户将其系统配置为使用UTF-8;或者您应该处理其他问题)
最后,您的程序可能(也可能)在其他地方有一些bug(例如,一些bug)。阅读使用所有警告和调试信息编译(因此g++-Wall-Wextra-g
with)。使用和
使现代化
当我将其代码更改为:
#include <string>
#include <iostream>
int main () {
std::string str = "1234567890123456";
std::cout << str << std::endl;
return 0;
}
#include <string>
#include <iostream>
int main () {
std::string str = "1234567890123456";
std::cout << str << std::endl;
return 0;
}
#包括
#包括
int main(){
std::string str=“1234567890123456”;
ST::C++中的CUT字符串是 STD::String 。使用它代替C样式字符串。我想使用<代码> STD::String 。但是这会使我感到不安。请改进你的问题,以显示一些请重新编辑以给出一个(这意味着有一个完整的程序,包含一个main
,并显示编译它的命令和输入文件示例)。可能您还有一些错误,请改进您的代码以使其完整,并在代码中使用看起来像英文的名称(这有助于理解您想要做的事情)。另外,请告诉我们您通过使用调试器了解了什么。顺便说一句,在调试程序时,您最好使用-Og
或-O0
而不是-O3
进行编译。我希望您的最后一句话是正确的。事实并非如此。大多数Windows计算机将使用系统定义的代码页,这几乎肯定不是UTF-8。但就目前而言据我所知,您可以将Windows系统配置为使用UTF-8。但您的客户可能不会(这是一种系统配置,不是程序配置)。我有:fstream f;
string name;
getline(f,name)
但是我得到内存损坏和无效指针错误。@blahax2g:在你的问题中显示一个。
[prowadzacy] Jan,Nowak
[przedmiot] Informatyka w Technologii
#include <string>
#include <iostream>
int main () {
std::string str = "123456789012345";
std::cout << str << std::endl;
return 0;
}
#include <string>
#include <iostream>
int main () {
std::string str = "1234567890123456";
std::cout << str << std::endl;
return 0;
}
1234567890123456
delete: invalid pointer 00F27F48 (004157B0)
*** Checking for memory corruption: START
*** Checking for memory corruption: 0 FOUND
std::string linstr;
std::getline (std::cin, linstr);
#include <string>
#include <iostream>
int main () {
std::string str = "1234567890123456";
std::cout << str << std::endl;
return 0;
}