C++读取文件传输到2d数组
C++ read file transfer to 2d Array
因此,正如标题所说,我试图从文件中读取并将字符保存到一个带有空格的2d数组中,但它被困在文件的do-while循环和最后一个字符中。关于如何解决这个问题有什么建议吗?我想过使用eofbit,但我想不通,然后我试着用eof退出do-while循环,但没用。它似乎只是挂在文件的最后一个字母上。提前感谢你的建议
char ch[100][100];
int row_count =0;
int col_count = 0;
int col[100];
char temp;
bool exit = false;
ifstream infile("data.txt");
if( infile.is_open())
{
while(!infile.eof())
{
do
{
infile.get(temp);
char[row_count][col_count] = temp;
col_count++;
}while(temp != 'n' || !infile.eof());
col[row_count] = col_count;
row_count++;
col_count= 0;
}
}
for(int i = 0; i <= 2; i++)
{
for(int j=0; i <= col[i]; j++)
{
cout << ch[i][j];
}
cout << endl;
}
return 0;
}
循环是因为temp != 'n'
,在文件末尾,此条件始终为true,因此永远不会验证第二个条件,因为您使用或对其进行检查。如果您想保留您的代码,请将!infile.eof()
放在第一位。
但是如果你想要一个最好的方法和一个简单的方法来读取文件。您应该使用getline
。
std::string line;
while(std::getline (infile,line))
{
//do something for every char in the string...
}
我没有编译它,但它应该可以工作:阅读评论并从中学习。
#include <iostream> // for cout
#include <fstream> // for infile
int main()
{
const int RowSize = 100, ColSize = 100;
char ch[RowSize][ColSize];
int row_count = 0;
int col_count = 0;
int col[ColSize];
char temp;
bool exit = false;
std::ifstream infile;
infile.open("data.txt"); // open the file
if (!infile.is_open()) // check if opened succseffuly
{
std::cout << "File didn't load successfully!" << std::endl; // prompt the user
getchar(); // wait for any input
return 0; // terminate
}
while (infile.get(temp)) // while reading succesffuly
{
// if you are here, means you have valid data
if (temp == 'n') // check if end of the line
{
col_count = 0; // reset cols
row_count++; // increment rows
ch[RowSize][ColSize] = NULL; // lock char array
continue; // continue the loop iteration
}
else
{
ch[row_count][col_count] = temp; // store temp in your array
}
col_count++; // increment cols on every read
}
for (int i = 0; i <= 2; i++) // this will display junk if your line is less than 100 characters (check NULL if you don't want that)
{
for (int j = 0; i <= col[i]; j++)
{
std::cout << ch[i][j];
}
std::cout << std::endl;
}
return 0;
}
iostream::eof
只有在读取流的末尾后才会返回true。它并不表示下一次读取将是流的末尾。参考这个。因此,修改后的代码看起来像下面的代码片段。
char ch[100][100];
int row_count =0;
int col_count = 0;
int col[100];
char temp;
bool exit = false;
ifstream infile("data.txt");
if( infile.is_open())
{
while(!(infile>>temp).eof())
{
ch[row_count][col_count] = temp;
col_count++;
if(temp == 'n'){
col[row_count] = col_count;
row_count++;
col_count= 0;
}
}
}
for(int i = 0; i <= 2; i++)
{
for(int j=0; i <= col[i]; j++)
{
cout << ch[i][j];
}
cout << endl;
}
return 0;
}
相关文章:
- 2D数组来自文本输入,中间有空格
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 获取 2D 数组 c++ 中的所有数字对
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 如何在构造函数中使用初始值设定项设置具有相同值的 2d 数组?
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 如何创建一个所有行大小不同的 2D 数组,并且用户将指定每行将有多少个元素?
- 使用指针算法修改函数中的 2D 数组
- 在C++中初始化和打印 2D 数组
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- C++ 2D 数组写入多个位置
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 如何从initializer_list初始化 2D 数组
- 交换函数不是在 C++ 中交换 2D 数组的元素