2012-05-28 62 views
0

我正在尝试根据用户对2个文件的选择对文件进行排序。我正在使用一个字符串变量并将其传递到instream(),但它继续进入if声明,该声明表示该文件已损坏或不存在。我知道它存在,因为当我硬编码的文件名,然后它工作得很好!我确定它很简单,但我无法弄清楚。我对C++很陌生,所以请对你的回答彻底,以便我能理解和学习。提前致谢!基于用户输入对文件进行排序

#include <iterator> 
#include <algorithm> 
#include <vector> 
#include <fstream> 
#include <iostream> 
#include <string> 
using namespace std; 

std::string file = ""; 
std::ofstream out("outputfile.txt"); 
std::vector<int> numbers; 
std::string sortType = ""; 

void sort(std::vector<int>); 
void MergeSort(vector<int> &numbers); 


int main() 
{ 
    std::cout << "Which type of sort would you like to perform(sort or mergesort)?\n"; 
    std::cin >> sortType; 

    std::cout << "Which file would you like to sort?\n"; 
    std::cin >> file; 

    std::ifstream in(file); 
    //Check if file exists 
    if(!in) 
    { 
    std::cout << std::endl << "The File is corrupt or does not exist! "; 
    return 1; 
    } 

    // Read all the ints from in: 
    std::copy(std::istream_iterator<int>(in), std::istream_iterator<int>(), 
      std::back_inserter(numbers)); 

    //check if the file has values 
    if(numbers.empty()) 
    { 
     std::cout << std::endl << "The file provided is empty!"; 
     return 1; 
    } else 
    { 
     if(sortType == "sort") 
     { 
      sort(numbers); 
     }else 
     { 
      MergeSort(numbers); 
     } 

     // Print the vector with tab separators: 
     std::copy(numbers.begin(), numbers.end(), 
       std::ostream_iterator<int>(std::cout, "\t")); 
     std::cout << std::endl; 

     // Write the vector to a text file 
     std::copy(numbers.begin(), numbers.end(), 
       std::ostream_iterator<int>(out, "\t")); 
     std::cout << std::endl; 
    } 
    return 0; 
} 

void sort(std::vector<int>) 
{ 
    // Sort the vector: 
    std::sort(numbers.begin(), numbers.end()); 
    std::unique(numbers.begin(), numbers.end()); 

    return; 
} 

vector<int> Merge(vector<int> &left, vector<int> &right) 
{ 
    std::vector<int> result; 

    while (left.size() > 0 && right.size() > 0) 
    { 
     if (left[0] <= right[0]) 
     { 
      result.push_back(left[0]); 
      left.erase(left.begin()); 
     } else 
     { 
      result.push_back(right[0]); 
      right.erase(right.begin()); 
     } 
    } 

    if (left.size() > 0) 
    { 
     result.insert(result.end(), left.begin(), left.end()); 
    } else 
    { 
     result.insert(result.end(), right.begin(), right.end()); 
    } 
    return result; 
} 

void MergeSort(vector<int> &numbers) 
{ 
    if (numbers.size() <= 1) 
    { 
     return; 
    } 

    // split vector into two peices 
    vector<int> left, right; 
    unsigned int Middle = numbers.size()/2; 

    for (unsigned int i = 0; i < Middle; i++) 
    { 
     left.push_back(numbers[i]); 
    } 

    for (unsigned int i = Middle; i < numbers.size(); i++) { 
     right.push_back(numbers[i]); 
    } 

    MergeSort(left); 
    MergeSort(right); 
    numbers = Merge(left, right); 
    return; 
} 
+0

我现在得到'错误:没有匹配函数调用'std :: basic_ifstream > :: basic_ifstream(std :: string&)'' – Jmh2013

回答

1

您仍然在检查名称为""的文件。当你把线

std::ifstream in(file); 

它打开流被命名为“”(的file在该点的值)指定的文件。后来,你说

if (!in) 

没有实际更新使用的文件。

试试这个:

std::ifstream in; //no file specified 

//the following comes before if (!in) 
in.open (file); 

这将打开流的file的输入值。

一个更好的办法来做到这一点是只申报和到位的二线打开该文件,失去了第一:

std::ifstream in (file); //after they input the filename 

使用全局变量是一个普遍不好的想法,如果你不有任何理由。最好将它们传递给函数,或者全部包含在一个类中。

此外,我注意到你已经宣布using namespace std;,但仍然使用std::vector等我肯定会选择后者,并删除前者。注意添加解决方案,然后在那里忽略它然后。

+0

这很有道理。现在即时通讯我得到了一个错误,我用这两种方式打开文件,你建议。 '错误:没有匹配的函数调用'std :: basic_ifstream > :: basic_ifstream(std :: string&)'' 我只做了您所建议的更改。除此之外,它与上述完全相同。 – Jmh2013

+0

@ Fourthmeal70,你能更新你的代码来反映这些变化吗?这似乎很奇怪。 – chris

+0

代码已更新。除了将“(file)”中的std :: ifstream移动到“if(!in)'语句之前,没有任何变化。 – Jmh2013

0

在检查状态之前,您需要open()文件。

相关问题