2016-01-20 62 views
0

我想获得一个txt文件与条目part1/1 part2/4 etc ...从用户和存储在向量“part_name”和“rev_id”。因此“part_name”包含part1 part2 ...并且“rev_id”包含1 4 ..... 该程序在命令提示符下以program.exe list.txt的形式运行。C++矢量迭代失败的单个元素输入

当txt文件有2个或多个输入时,程序工作正常,但是当它有单个输入时,vector大小显示为2(但必须是1)。

如果list.txt包含part1/1part2/4=> part_name.size()是2

如果list.txt包含part1/1=> part_name.size()仍然是2

有人可以帮我解决这个问题?

#include <iostream> 
#include <string> 
#include <fstream> 
#include <sstream> 
#include <vector> 

using namespace std; 

int main(int argc, char*argv[]) 
{ 

std::string s ; 
std::string delimiter = "/"; 
size_t pos; 
std::vector<std::string> part_name; 
std::vector<std::string> rev_id; 
std::string token1,token2; 


ifstream readFile (argv[1]); 
if (readFile.is_open()) 
{ 
while (!readFile.eof()) 
{ 
    readFile >> s; 
    pos=s.find(delimiter); 
    if((pos!=std::string::npos)&&(pos!=0)) 
    { 

    token1 = s.substr(0, s.find(delimiter)); 
    token2 = s.substr(pos + delimiter.length()); 

    part_name.push_back(token1); 
    rev_id.push_back(token2); 
    } 
} 
} 
else{ 

    std::cout<<"Cannot open file"<<endl; 
} 
readFile.close(); 

    for (unsigned j=0; j < part_name.size(); j++) 
{ 
    cout<<part_name.size()<<endl; 
    cout<<"part name j is " <<part_name[j]<<endl; 
    cout<<"part id j is " <<rev_id[j]<<endl; 
} 


} 
+0

'part1/1'是一行还是每行可以有多个“部分”? – NathanOliver

回答

3

我认为问题就出在这里

while (!readFile.eof()) 
{ 
readFile >> s; 
pos=s.find(delimiter); 
if((pos!=std::string::npos)&&(pos!=0)) 
{ 

token1 = s.substr(0, s.find(delimiter)); 
token2 = s.substr(pos + delimiter.length()); 

part_name.push_back(token1); 
rev_id.push_back(token2); 
} 

变化上面

while (readFile >> s) 
{ 

pos=s.find(delimiter); 
if((pos!=std::string::npos)&&(pos!=0)) 
{ 

token1 = s.substr(0, s.find(delimiter)); 
token2 = s.substr(pos + delimiter.length()); 

part_name.push_back(token1); 
rev_id.push_back(token2); 
} 
+0

谢谢Ajay,谢谢乔治 – vipin

0

有一些线路比让我怀疑.. 为什么由索引1开始的迭代循环?

for (unsigned j=1; j < part_name.size(); j++) 
{ 
    cout<<part_name.size()<<endl; 
    cout<<"part name j is " <<part_name[j]<<endl; 
    cout<<"part id j is " <<rev_id[j]<<endl; 
} 

你总是跳过你载体的第一个元素。

+0

嗨亚历克西斯,这是一个错字,我没有纠正它(j = 0),但问题仍然存在... – vipin

1

你最后一次读取可能是不读任何东西。尝试:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <sstream> 
#include <vector> 

using namespace std; 

int main(int argc, char*argv[]) 
{ 
    std::string s ; 
    std::string delimiter = "/"; 
    size_t pos; 
    std::vector<std::string> part_name; 
    std::vector<std::string> rev_id; 
    std::string token1,token2; 

    ifstream readFile (argv[1]); 
    if (readFile.is_open()) 
    { 
     while((readFile >> s).good()) 
     { 
      std::cerr << "Field '" << s << "' and " << readFile.good() << std::endl; 
      pos=s.find(delimiter); 
      if((pos!=std::string::npos)&&(pos!=0)) 
      { 
       token1 = s.substr(0, pos); 
       token2 = s.substr(pos + delimiter.length()); 
       part_name.push_back(token1); 
       rev_id.push_back(token2); 
      } 
     } 
    } 
    else { 
     std::cout<<"Cannot open file"<<endl; 
    } 
    readFile.close(); 

    for (unsigned j=0; j < part_name.size(); j++) 
    { 
     cout<<part_name.size()<<endl; 
     cout<<"part name j is " <<part_name[j]<<endl; 
     cout<<"part id j is " <<rev_id[j]<<endl; 
    } 
} 
1

eof()返回true时不将文件位置是在年底,但你在文件的最后尝试读取之后。因此,在循环中第一次将输入消耗到eof(不包括在内)。现在eof()是错误的。因为这个原因,循环再次进入,这次提取器(>>)到达文件的结尾。读取操作失败,字符串不变(这就是为什么你看到相同的值两次)。

到代码更好(更惯用现代C++)的方法是使用流迭代

#include <iterator> 

// [...] 

std::istream_iterator<std::string> scan(readFile); 
std::istream_iteartor<std::string> eof; 
while(scan != eof) 
{ 
    s = *scan++; 
    // s is your string 
} 

而且这将是更好为在第二部分中的环向从0向上运行,因为元件向量中的编号从0开始编号。