Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;“调试断言失败”;在读取txt文件时 我试图创建一个C++程序,它读取一个文本文件,内容如下: ITEMS 8 ABERDEEN 430082 3.2 5.0 GLASGOW 629501 2.0 1.5 PAISLEY 74170 1.0 1.0 MOTHERWELL 30311 3.0 1.0 EDINBURGH 430082 5.0 1.3 FALKIRK 32379 3.1 1.2 LINLTHGOW 13370 3.0 1.5 DUNDEE 154674 3.2 3.1_C++_File - Fatal编程技术网

&引用;“调试断言失败”;在读取txt文件时 我试图创建一个C++程序,它读取一个文本文件,内容如下: ITEMS 8 ABERDEEN 430082 3.2 5.0 GLASGOW 629501 2.0 1.5 PAISLEY 74170 1.0 1.0 MOTHERWELL 30311 3.0 1.0 EDINBURGH 430082 5.0 1.3 FALKIRK 32379 3.1 1.2 LINLTHGOW 13370 3.0 1.5 DUNDEE 154674 3.2 3.1

&引用;“调试断言失败”;在读取txt文件时 我试图创建一个C++程序,它读取一个文本文件,内容如下: ITEMS 8 ABERDEEN 430082 3.2 5.0 GLASGOW 629501 2.0 1.5 PAISLEY 74170 1.0 1.0 MOTHERWELL 30311 3.0 1.0 EDINBURGH 430082 5.0 1.3 FALKIRK 32379 3.1 1.2 LINLTHGOW 13370 3.0 1.5 DUNDEE 154674 3.2 3.1,c++,file,C++,File,我的程序崩溃,出现以下错误: 程序读取文件的某些内容,直到某一点: 我注意到的一件有趣的事情是,每个城镇的X和Y坐标都应该是双倍的,当我阅读文件时,一些X/Y是整数或双倍的(不知道这是怎么发生的)。我将每个城镇作为城镇对象存储在城镇区域中,如下所示: 这是我的从文件读取方法: bool TownReader::readDatafile(char *datafile) { ostringstream errorString; ifstream inDatastream(dat

我的程序崩溃,出现以下错误:

程序读取文件的某些内容,直到某一点:

我注意到的一件有趣的事情是,每个城镇的X和Y坐标都应该是双倍的,当我阅读文件时,一些X/Y是整数或双倍的(不知道这是怎么发生的)。我将每个城镇作为城镇对象存储在城镇区域中,如下所示: 这是我的从文件读取方法:

bool TownReader::readDatafile(char *datafile)
{
    ostringstream errorString;

    ifstream inDatastream(datafile, ios::in);

    if (!inDatastream)
    {       
        errorString << "Can't open file " << datafile << " for reading.";
        MessageBoxA(NULL, errorString.str().c_str(), "Error", MB_OK | MB_ICONEXCLAMATION);
        return false;
    }

    cout << "Reading from file: " << datafile << endl; 

    readUntil(&inDatastream, "ITEMS");
    //Read the number of towns...
    inDatastream >> numTowns;
    //reserve the nesessery memory...
    TownPtr = new Town[ numTowns ];

    cout << "Number of towns: " << numTowns << endl;

    for (int i = 0; i < numTowns; ++i)
    {
        Town newTown;
        char townName[] = "";
        double townX = 0.0;
        double townY = 0.0;
        int townPopulation = 0;
        inDatastream >> townName >> townPopulation >> townX >> townY;
        cout << "Town name: " << townName << endl;
        cout << "Town pop: " << townPopulation << endl;
        cout << "Town X: " << townX << endl;
        cout << "Town Y: " << townY << endl;

        newTown.setName(townName);
        newTown.setPopulation(townPopulation);
        newTown.setX(townX);
        newTown.setY(townX);

        TownPtr[i] = newTown;
    }

    return true;
}
这是一个大小为1的数组。你不能读入任何东西。使用C++时,请使用char数组,使用<代码> STD::String < /C> > < /P>
这是一个大小为1的数组。你不能读入任何东西。在使用C++时,请使用char数组,使用<代码> STD::String 。< /P>尽管这个问题已经得到回答,但它仍然是一个本地化问题,是堆栈溢出的Q&A库的有用部分。您可能已经表明,您已将问题缩小到尝试读入
char townName[]=”
,问题标题为“为什么我不能读入
char townName[]=”“
?虽然这个问题已经得到了回答,但这个问题仍然过于局限,无法成为堆栈溢出问答库的有用部分。你的回答会显示你已经将问题缩小到试图读入
char-townName[]=”
,题目是“为什么我现在不能读入
char-townName[]=”
?VTC。非常感谢!从char[]切换到string解决了问题。:)非常感谢!从char[]切换到string已修复问题。:)
class TownReader
{
private:
    Town *TownPtr;
    int numTowns;

public:
    TownReader(void);
    bool readDatafile(char *filename);
    bool writeDatafile(char *filename);

    bool readUntil(std::ifstream *inStream, char *target);


    Town *getTowns(void);
    int getNumTowns(void);

    bool writeBlankRecords(char *datafile, int num);
    bool writeTownsBinary(char *datafile);
    bool readSpecifiedRecord(char *datafile);
    bool writeSpecifiedRecord(char *datafile);
};
char townName[] = "";