c++ - 文件输入问题 [C++]

标签 c++ pointers file-io fstream

我第一次尝试从文件中读取数据,一个 .txt 文件。下面是我的代码:

Level::ReadStream(std::fstream data)
{
bool write = false;
char* p = data.tellg();

while(!data.eof())
{
    int i = 0, j = 0;

    if(&p == '[')
    {
        write == true;
    }

    if(write == true)
    {
        mapX[i] = p;
        mapY[j] = p;
    }

    if(&p == '/n')
    {
        i++;
        j++;
    }

    else
    {
        i++;
    }

    *p++
}
};

void Level::SetMapSize(std::fstream data)
{
int* p = data.tellg();
int sizeX = 0;
int sizeY = 0;

while(!data.eof())
{
    if(&p != '[' && &p != ']' && &p != '\n')
    {
        sizeX++;
    }

    else if(&p != '\n')
    {
        sizeY++;
    }
}

mapX.resize(sizeX);
mapY.resize(sizeY);

std::cout << sizeX << '\n' << sizeY;
};

这两个函数的目标是: 1-读取文件中的所有字符,如果当前字符不是括号,则将其添加到索引映射(X,Y)中,然后增加计数器以便将下一个非括号值放入正确的索引中.

2- 读取所有文件并像以前一样计算非括号值,因此它可以使 mapX 和 mapY 的大小正确。

但是,它不起作用,我收到以下错误:

 C:\...\Level.cpp|58|warning: multi-character character constant|

 c:\...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\ios_base.h|790|error: 'std::ios_base::ios_base(const std::ios_base&)' is private|

 c:\...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|47|error: within this context|

 c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|87|note: synthesized method 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)' first required here |

 c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\streambuf|770|error: 'std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private|

 c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|78|error: within this context|

 c:...\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iosfwd|87|note: synthesized method 'std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)' first required here |

C:...\Level.cpp||In constructor 'Level::Level()':|

C:...\Level.cpp|24|note: synthesized method 'std::basic_fstream<char, std::char_traits<char> >::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&)' first required here |

C:...\Level.cpp|24|error:   initializing argument 1 of 'void Level::SetMapSize(std::fstream)'|

C:...\Level.cpp|29|error: 'cout' was not declared in this scope|

C:...\Level.cpp|38|error: ISO C++ forbids declaration of 'ReadStream' with no type|

C:...\Level.cpp|38|error: prototype for 'int Level::ReadStream(std::fstream)' does not match any in class 'Level'|

C :...\Level.cpp|17|error: candidate is: void Level::ReadStream(std::fstream)|
C:...\Level.cpp||In member function 'void Level::SetMapSize(std::fstream)':|
C:...\Level.cpp|75|error: invalid conversion from 'std::streamoff' to 'int*'|
C:...\Level.cpp|81|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|81|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|81|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|86|error: ISO C++ forbids comparison between pointer and integer|
C:...\Level.cpp|95|error: 'cout' is not a member of 'std'|
||=== Build finished: 15 errors, 1 warnings ===|

有人能帮忙吗?

编辑:好的,我改变了一切。我的新代码是:

void Level::ReadStream()
{
long p = textData.tellg();
int counter = 0;
int i = 0;
int j = 0;
char ch;

while(p != textData.eof())
{
    textData.seekg(counter);
    textData >> ch;

    if(ch != '[' && ch != ']')
    {
        mapX[i] = ch;
        mapY[j] = ch;

        i++;

        if(ch == '\n')
        {
            i = 0;
            j++;
        }
    }

    counter++;
    p = textData.tellg();
}
};

mapX 和 mapY 的长度为 5 int。现在它可以编译,但会挂起并崩溃。我不明白为什么...有人可以帮助我吗?

最佳答案

\n不是/n!这应该可以清除一些错误。

此外,您不能只从 tellg 获取指针——它返回位置,而不是指向该位置的指针!

基本上尝试阅读 C++ 中的 IO,也许从这个问题开始:

How to read from a file char by char in C++?

关于c++ - 文件输入问题 [C++],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10032224/

相关文章:

复制稀疏文件

perl - 如何读写文件,语法错误

c# - 将 C 代码移植到 C#。指针的问题

c - memcpy vs 用于读取 BLE 传感器 float 的指针转换

c - 固定大小数组的动态数组

Python如何在不删除已有内容的情况下继续写入文件

c++ - 使用模运算符 C++

c++ - 使用 QComboBox 后 QGraphicsItem 失去焦点

c++ - 虚拟构造函数习语和工厂设计

c++ - if-else 循环不循环 C++ opencv visualstudio