C++ C++;一旦文本到达行尾,停止将其输入2D数组
我找了又读,但我想不出这个。我只是尝试将文本文件输入到[I][j]字符串数组中。它做得很好,但我需要它在到达行尾后停止输入数组,并开始将第二行放入第二个数组,在行尾停止,以此类推 我的文件包含4个单独的读取行 这是第一行。C++ C++;一旦文本到达行尾,停止将其输入2D数组,c++,arrays,text-files,C++,Arrays,Text Files,我找了又读,但我想不出这个。我只是尝试将文本文件输入到[I][j]字符串数组中。它做得很好,但我需要它在到达行尾后停止输入数组,并开始将第二行放入第二个数组,在行尾停止,以此类推 我的文件包含4个单独的读取行 这是第一行。 这是第二行。 这是第三行。 这是四号线 我的代码读取它,并且主要做我需要它做的事情。但它会将所有内容放入数组中,直到空间用完,然后继续到下一行。当它到达第一行的末尾时,它不会停止。这是我的密码 #include <iostream> #include <st
这是第二行。
这是第三行。
这是四号线 我的代码读取它,并且主要做我需要它做的事情。但它会将所有内容放入数组中,直到空间用完,然后继续到下一行。当它到达第一行的末尾时,它不会停止。这是我的密码
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string arrayTextIn[25][25];
ifstream textIn;
textIn.open("sampleText.txt");
for(int i=0; i<25; i++)
for(int j=0; j<25; j++)
{
textIn >> arrayTextIn[i][j];
if(arrayTextIn[i][j] == "\n") //This is where I dont know how to proceed.
break;
}
for(int i=0; i<25; i++)
for(int j=0; j<25; j++)
cout << i << " " << j << " "<< arrayTextIn[i][j] << endl;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串arrayTextIn[25][25];
ifstreamtextin;
textIn.open(“sampleText.txt”);
对于(int i=0;i arrayTextIn[i][j];
if(arrayTextIn[i][j]=“\n”)//这就是我不知道如何继续的地方。
打破
}
对于(inti=0;i这是一个两步过程
第一步是读取输入,一次读取一行,这将读取输入文件中的每行文本:
ifstream textIn;
textIn.open("sampleText.txt");
for(int i=0; i<25; i++)
{
std::string line;
if (std::getline(textIn, line).eof())
break;
// Magic goes here.
}
ifstreamtextin;
textIn.open(“sampleText.txt”);
对于(int i=0;i这是一个两步过程
第一步是读取输入,一次读取一行,这将读取输入文件中的每行文本:
ifstream textIn;
textIn.open("sampleText.txt");
for(int i=0; i<25; i++)
{
std::string line;
if (std::getline(textIn, line).eof())
break;
// Magic goes here.
}
ifstreamtextin;
textIn.open(“sampleText.txt”);
对于(int i=0;i如果您真的想要维护您的设计(2D阵列),那么这可能是一个快速的解决方案:
for (int i = 0; i < 25; i++) {
std::string line;
std::getline(textIn, line); // read the entire line
if (textIn.eof()) {
// if reach the end of file, then break the cycle.
break;
}
if (line.size() > 0) { // avoid empty lines
int j = 0;
size_t finder;
// Cycle and looking for whitespace delimiters.
while (finder = line.find(' '), finder != std::string::npos && j < 25) {
// get a single word
auto token = line.substr(0, finder);
if (token != " ") {
// if that word is not a simple white-space then save it in the array. You can also std::move if C++11
arrayTextIn[i][j] = token;
++j;
}
// erase that part from the main line
line.erase(0, finder + 1);
}
}
}
for(int i=0;i<25;i++){
std::字符串行;
std::getline(textIn,line);//读取整行
if(textIn.eof()){
//如果到达文件末尾,则中断循环。
打破
}
如果(line.size()>0){//避免空行
int j=0;
尺寸探测器;
//循环并查找空格分隔符。
while(finder=line.find(“”),finder!=std::string::npos&&j<25){
//只字片语
自动令牌=line.substr(0,finder);
如果(令牌!=“”){
//如果该单词不是简单的空白,则将其保存在数组中
arrayTextIn[i][j]=令牌;
++j;
}
//从主线上删除该部分
行。擦除(0,查找器+1);
}
}
}
如果您真的想维护您的设计(2D阵列),那么这可能是一个快速的解决方案:
for (int i = 0; i < 25; i++) {
std::string line;
std::getline(textIn, line); // read the entire line
if (textIn.eof()) {
// if reach the end of file, then break the cycle.
break;
}
if (line.size() > 0) { // avoid empty lines
int j = 0;
size_t finder;
// Cycle and looking for whitespace delimiters.
while (finder = line.find(' '), finder != std::string::npos && j < 25) {
// get a single word
auto token = line.substr(0, finder);
if (token != " ") {
// if that word is not a simple white-space then save it in the array. You can also std::move if C++11
arrayTextIn[i][j] = token;
++j;
}
// erase that part from the main line
line.erase(0, finder + 1);
}
}
}
for(int i=0;i<25;i++){
std::字符串行;
std::getline(textIn,line);//读取整行
if(textIn.eof()){
//如果到达文件末尾,则中断循环。
打破
}
如果(line.size()>0){//避免空行
int j=0;
尺寸探测器;
//循环并查找空格分隔符。
while(finder=line.find(“”),finder!=std::string::npos&&j<25){
//只字片语
自动令牌=line.substr(0,finder);
如果(令牌!=“”){
//如果该单词不是简单的空白,则将其保存在数组中
arrayTextIn[i][j]=令牌;
++j;
}
//从主线上删除该部分
行。擦除(0,查找器+1);
}
}
}