English 中文(简体)
基于用户输入的排序文件
原标题:sort a file based on user input

我试图根据用户选择 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)?
";
  std::cin >> sortType;

  std::cout << "Which file would you like to sort?
";
  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, "	"));
      std::cout << std::endl;

        // Write the vector to a text file
      std::copy(numbers.begin(), numbers.end(),
                std::ostream_iterator<int>(out, "	"));
        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;
}
最佳回答

您仍然在重新检查给定文件的名称 < code> “ 。 当您放行时

std::ifstream in(file);

它打开流到名为“ ” 指定的文件( 值为 < code> 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

如果您没有任何理由使用全球变量, 使用全局变量通常是一个坏主意。 最好是在函数中传递这些变量, 或者将其全部包含在类中 。

另外,我注意到您已经宣布了 < code> 使用命名空间std; , 但仍使用 < code>std:: victor 等 。 我绝对会选择后一种方法, 并删除前一种方法。 注意将分辨率添加到其中缺少的几件东西中 。

问题回答

您需要先 Open () 文件才能检查状态 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签