文件输入[C++]出现问题
Trouble with file input [C++]
我第一次尝试从一个.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:...mingwbin..libgccmingw324.4.1includec++bitsios_base.h|790|error: 'std::ios_base::ios_base(const std::ios_base&)' is private|
c:...mingwbin..libgccmingw324.4.1includec++iosfwd|47|error: within this context|
c:...mingwbin..libgccmingw324.4.1includec++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:...mingwbin..libgccmingw324.4.1includec++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:...mingwbin..libgccmingw324.4.1includec++iosfwd|78|error: within this context|
c:...mingwbin..libgccmingw324.4.1includec++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,也许可以从这个问题开始:
如何在C++中逐字符读取文件?
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题