从文本文件读取不起作用
reading form a textfile not working
>我有一个问题。当我使用以下文本文件运行此代码时:
car
3
4
-8 7
-+--
-7 1
我尝试打印它,它最多打印数字 7,最后 2 个字符没有打印
谁能帮我?谢谢
string srow;
string scolumn;
int row = 0;
int column = 0;
string gametype;
ifstream myReadFile;
myReadFile.open("text.txt");
if (myReadFile.is_open())
{
getline(myReadFile, gametype);
getline(myReadFile, srow);
getline(myReadFile, scolumn);
row = stoi(srow);
column = stoi(scolumn);
for (i = 0; i <row; i++)
{
for (j = 0; j <column; j++)
{
myReadFile.get(ch);
printf("%c",ch);
}
}
}
myReadFile.close();
return 0;
}
如果 ch 是字符...您正在跳过行尾。您的文件实际上是:
car<n>
3<n>
4<n>
-8 7<n>
-+--<n>
-7 1<n>
<> 是行尾字符,因此.. 如果您读取 4 个字符 x 2 行(8 个字符(.. .您正在阅读:
12345678
-+--E-7
^
|
----- End of line (</n)
您可以使用 getline 来避免行尾或在 for 句子中读取它,但要小心,Windows/DOS (\r( 和 Unix (( 在行尾没有相同的字符。
它给出了错误的输出,因为每行都有5
字符而不是4
。您没有考虑n
在行尾。最后两个字符不会打印,因为两个n
是从上面两行打印的。要完成的更改包括:
Replace
for (j = 0; j <column; j++)
with
for (j = 0; j <= column; j++)
相关文章:
- 为什么读取函数在 sha1 c++ 实现中不起作用?
- 使用sscaf_s从obj读取顶点线不起作用
- c++ 读取进程内存基址 + 偏移量不起作用
- 尝试读取数据文件,存储在数组中并打印所有元素,但它不起作用
- cin.clear() 不起作用 - 不读取输入
- 从文本文件中读取不起作用C
- 从文件中读取行,while(getline()) 不起作用
- 在 Mac 中使用C++读取 UTF-8 数据不起作用
- 串行通信读取始终不起作用
- 用c++编写的程序,只读取字符串变量的数值.但不起作用
- Telling和seekg以及文件读取不起作用
- C++ SQLite3 读取 blob 不起作用
- c++ 将文本文件读取到结构数组中不起作用
- 从文件中读取 C++ 不起作用
- 尝试读取文件直到最后(不起作用)
- 从文本文件读取不起作用
- 使用 std::ifstream 读取文件以编译 OpenGL 着色器不起作用
- 在 Ctrl+Z (EOF) 之后从 iostream::cin 恢复读取?("ignore"不起作用)
- 从文本文件中简单读取算法不起作用
- 使用VideoCapture读取目录中的图像不起作用