Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8014053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:48:59+00:00 2026-06-04T19:48:59+00:00

I am trying to sort a file based on the users choice of 2

  • 0

I am trying to sort a file based on the users choice of 2 files. I’m using a string variable and passing it into the instream() but it keeps going into the if statement that says the file is corrupt or does not exist. I know it exists because when I hardcode the file name in then it works perfectly fine! I’m sure its something simple but I just can’t figure it out. I’m very new to c++ so please be thorough with your answers so that I can understand and learn. Thanks in advance!

#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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:49:02+00:00Added an answer on June 4, 2026 at 7:49 pm

    You’re still checking the file given by the name "". When you put the line

    std::ifstream in(file);
    

    it opens the stream to the file specified by the name “” (the value of file at that point). Later, you say

    if (!in)
    

    without actually updating the file used.

    Try this:

    std::ifstream in; //no file specified
    
    //the following comes before if (!in)
    in.open (file);
    

    This will open the stream to the inputted value of file.

    A better way to do this would be to just declare and open the file in place of the second line and lose the first:

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

    Using global variables is a generally bad idea if you don’t have any reason to. It’s better to pass them around in functions, or contain it all in a class.

    Also, I notice you’ve declared using namespace std;, but still use std::vector etc. I would definitely opt for the latter and remove the former. Take care to add the resolution to the couple of things there missing it then though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a problem trying to sort with an XSL file using the
Trying to load a file into python. It's a very big file (1.5Gb), but
I am trying to read and sort a csv file that has data that
I am trying to sort but there is a nil. How can i get
I'm trying to sort a Vector in java but my Vector is not a
I'm trying to sort through a collection of DeepZoom sub-images based on arbitrary data
I found some this promising code on activestate.com to sort huge files. I'm trying
I'm trying to sort a folder on a Mac with hundreds of jpgs into
I am trying to sort a table based on the first value from smallest
I'm trying to sort the contents of several files (sometimes moving a line from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.