File io 为什么我的变量被读取,然后消失?

File io 为什么我的变量被读取,然后消失?,file-io,File Io,所有这些都发生在我的单个循环中。 我为X、Y和3个字符串创建临时变量 文本文件有320行,如下所示: 1 2 "string" "stringy" "stringed" int1 int2 string1 string2 string3 int1 int2 string1 string2 string3 int1 int2 string1 string2 string3 int1 int2 string1 string2 string3 循环代码如下: for(int

所有这些都发生在我的单个循环中。 我为X、Y和3个字符串创建临时变量

文本文件有320行,如下所示:

1   2   "string"   "stringy"   "stringed"

int1 int2 string1 string2 string3

int1 int2 string1 string2 string3

int1 int2 string1 string2 string3

int1 int2 string1 string2 string3
循环代码如下:

for(int Y = 0;Y < 320 ;Y++)
{

    int tempX;
    int tempY;
    char tempRegionName;
    char tempTXTfile;
    char tempIMGfile;

    fscanf(FileHandle, "%d %d %s %s %s ", &tempX, &tempY, &tempRegionName, &tempTXTfile, &tempIMGfile);

    cout<<"X: "<<tempX<<" Y: "<<tempY<<" Name: "<<tempRegionName<<" TXT: "<< tempTXTfile << " IMG: " << tempIMGfile << endl;

}
然后它就这样做了

tempX=1

tempY=2(tempX现在为0)

tempRegionName=“string”(tempY现在为0)

试探xtfile=“stringy”(tempReginoName现在为空)

tempIMGfile=“stringed”(文件现在为空)

然后输出以下内容:

 X: 1   Y: 0    NAME:    TXT:    IMG: stringed
我不明白这一点。我试图按照我在使用fscanf时发现的示例进行操作,另一个代码示例使用%d:%d工作。我尝试将空格替换为:,但显然不是空白


在cplusplus上查找,我有点难以理解。也许我只是累了,但我做错了什么?

缓冲区不够大,无法容纳字符串,即
char
只有一个字节。您应该将变量声明为字符数组。例如,请尝试以下方法:

for(int Y = 0;Y < 320 ;Y++)
{
    int tempX;
    int tempY;
    char tempRegionName[64];
    char tempTXTfile[64];
    char tempIMGfile[64];
for(int Y=0;Y<320;Y++)
{
int-tempX;
int tempY;
char tempRegionName[64];
字符文件[64];
char tempIMGfile[64];

<>但是要注意%s和目标缓冲区的大小。写“界外”很容易。< /p>这是什么语言?文件IO与循环问题有关吗?如果我的眼睛不欺骗我,这看起来像C++。对不起,我应该标记它。但是我确实说过“在CPLUS PLUS上查找”。你可以考虑用真正的C++方式来做,消除缓冲区溢出的风险。看看谢谢你。我从来没想到它是缓冲区大小。我很感激,这两个都修复了它,并给了我一个答案,如果没有你的帮助,我需要花上几天时间才能解决。这让我头疼。我甚至不接近Figuri。搞定这个问题,哈哈。
for(int Y = 0;Y < 320 ;Y++)
{
    int tempX;
    int tempY;
    char tempRegionName[64];
    char tempTXTfile[64];
    char tempIMGfile[64];