2015-01-31 60 views
0

我试图读取.txt文件中的行并使用std::cout将其打印出来。我的代码发布在下面,但目前没有在while循环中传递任何行到向量。我哪里做错了?从文件中获取getline的问题

int main(int argc, const char * argv[]) { 
    std::ifstream input("input1.txt");//(argv[1]); 
    if (!input.good()) { 
     cerr << "Can not open the grades file "; //<< argv[1] << "\n"; 
     return 1; 
    } 

    std::vector<string> art; 
    string x;     // Input variable 
    // Read the scores, appending each to the end of the vector 
    cout << "Start While:"; //For Debugging Purposes 
    while (getline(input, x)) { 
     art.push_back(x); 
    } 
    for (int i=0; i<art.size(); i++) { 
     cout << art[i] << "\n"; 
    } 
    return 0; 
} 
+3

文件中是否有行?代码对我来说看起来很好。 – Barry 2015-01-31 19:47:45

+0

验证确实w/VS2013.4 – 2015-01-31 20:01:14

+2

我对这段代码看到的唯一奇怪之处是混合使用和不使用合格的'std'命名空间(你应该更喜欢“use”侧,btw)。否则代码看起来合理,应该在包含数据行的文件上打开'input'。 – WhozCraig 2015-01-31 20:04:29

回答

0

原来Xcode正在困难中。移动几个文件并在编译器中运行后,它就可以工作了。