在MinGW中字符串长度超过15个字符的问题

时间:2018-12-27 10:24:59

标签: c++ string mingw getline

问题描述

我对<input class='move' /><input class='move' /><input class='move' />函数有疑问。我这样使用它:

std::getline()

一切都适合于文件短的文件,例如:

std::fstream f; // opened in read in text mode
std::string name;
std::getline(f, name);

但是当我尝试阅读更长的时间时,例如:

example text

我得到了(编译并运行后):

example text in file longer than 15

文件

main.cpp:

delete: invalid pointer 00FD7A48 (0040CD22)
*** Checking for memory corruption: START
*** Checking for memory corruption: 0 FOUND

prowadzacy.h:

#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:

#include "przedmiot.h"

struct Prowadzacy {
    std::string imie;
    std::string nazwisko;

    Przedmiot* przedmiot = nullptr;

    Prowadzacy* next = nullptr;
};

plik.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.cpp:

#include <fstream>
#include <iostream>
#include "prowadzacy.h"
#include "przedmiot.h"

bool zapiszDoPliku (const std::string, Prowadzacy*&);
bool czytajZPliku (const std::string, Prowadzacy*&);

包含我需要获取的数据的文件:

using namespace std;

bool czytajZPliku (const string fn, Prowadzacy*& baza) {
    fstream f;
    f.open(fn, ios::in);
    if (f) {
        string typ;
        Prowadzacy* p = baza;

        while (getline(f, typ, ' ')) {
            cout << typ << endl;
            if (typ.find("[prowadzacy]") != string::npos) {
                string imie, nazwisko;
                while (p != nullptr)
                    p = p->next;
                getline(f, imie, ',');
                getline(f, nazwisko);
                dodajProwadzacego(imie, nazwisko, baza);
            } else if (typ.find("[przedmiot]") != string::npos) {
                string nazwa;
                getline(f, nazwa);
            }
        }
        f.close();
        return true;
    }
    return false;
}

我使用Makefile和MinGW。使用标志[prowadzacy] Jan,Nowak [przedmiot] Informatyka w Technologii

进行编译

问题更新

我写了一个简单的新程序:

-std=c++14 -O3 -g -pedantic-errors

我毫不费力地编译和运行这些程序。但是当我将其代码更改为:

#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;
}

在Visual Studio中,一切正常。为什么在MinGW中不起作用?

编译为: 1234567890123456 delete: invalid pointer 00F27F48 (004157B0) *** Checking for memory corruption: START *** Checking for memory corruption: 0 FOUND

更新:我也在Linux上进行了测试。在Linux上,一切都很好。问题仅出现在Windows上的MinGW中。

解决方案

在编译命令中添加g++ -std=c++14 -pedantic-errors -Wall -Wextra -O3 -g -pedantic-errors -o main main.cpp可解决此问题。

1 个答案:

答案 0 :(得分:1)

例如使用std::string相关的std::getline函数(来自<string>标准标题)

std::string linstr;
std::getline (std::cin, linstr);

避免使用原始的char*缓冲区。

还请注意,UTF-8 is used everywhere将于2018年推出(您可以期望用户将其系统配置为使用UTF-8;或者应该处理其他character encodings)。

最后,您的程序可能在其他地方(可能有)一些bug(例如,某些undefined behavior)。阅读How to debug small programs。编译所有警告和调试信息(因此将g++ -Wall -Wextra -gGCC一起编译)。使用gdb debuggervalgrind

更新

  

当我将其代码更改为:

#include <string>
#include <iostream>
int main () {
   std::string str = "1234567890123456";
   std::cout << str << std::endl;
   return 0;
}
     

我得到无效的指针

可能您的计算机上有问题(上面的代码是完全合法的C ++ 11)。我猜您安装的MinGW错误,或设置错误(例如PATH或其他错误)或配置。获得更多帮助;您需要更多解释。

仅供参考,(当然)您的最后一个代码在我的Linux发行版(Debian / Sid,GCC 8.2)上运行正常。我建议安装和使用一些Linux发行版,特别是因为它对开发人员更友好(并提供valgrind非常对发现错误很有用)