0

Im working on a program to take a input file, sort it and print the output. These input files will have 10 , 100, 1000 or 100000 lines which each have a number per line.

Right now, my code only works for the first 10 lines.

the main part is

int array[10];
int size;
size = sizeof array/sizeof(int);

and

if(file.is_open())
    {

        for(int i = 0; i < size ; ++i)
        {
            file >> array[i];
        }
    }

to read the lines of the file into the array.

How can i change this to take variable lengths of file line sizes and read it into a array.

#include<iostream>

#include<string>
#include<fstream>
#include<vector>
#include<ctime>
using namespace std;


void insertion_sort(int x[],int length)
{
  int key,i;
  for(int j=1;j<length;j++)
  {
     key=x[j];
     i=j-1;
     while(x[i]>key && i>=0)
     {
               x[i+1]=x[i];
         i--;
     }
     x[i+1]=key;
  }
}


int main()
{

int array[10];
int size;
size = sizeof array/sizeof(int);
int x;


char name[256];

string sort, order, dup;


cout << "enter a file\n";
cin >> name;

ofstream ofile;
ifstream file(name);


if(file.is_open())
    {

        for(int i = 0; i < size ; ++i)
        {
            file >> array[i];
        }
    }
else{
            cout << "file doesnt exist";
            return 1;
        }
size = sizeof array/sizeof(int);
cout << "enter a sort method - type insertion_sort or merge_sort or quick_sort or counting_sort\n";
cin >> sort;

cout << "Type 'dec' for non decreasing or 'inc' for increasing order\n";
cin >> order;

cout << "Type yes or no to remove duplicates\n";
cin >> dup;


if (sort ==  "insertion_sort")

{

insertion_sort(array,size);
cout << "\ninsertion sort";
}


if (order == "dec"  )
    for(int i=0;i<size/2;i++)
                swap(array[i],array[size-i-1]);

if(dup == "yes")
    {


    int i, j;


    int NewLength = 1;

    for(i=1; i< size; i++){

       for(j=0; j< NewLength ; j++)
       {

          if(array[i] == array[j])
          break;
       }



      if (j==NewLength )
          array[NewLength++] = array[i];
    }

    }




cout<<endl<<"sorted "<<endl;
      for(x=0;x<size;x++)
      {
           cout<<array[x]<<endl;
      }




    return 0;
    }
1
  • 5
    you must have considered std::vector, right? Commented Oct 13, 2013 at 4:57

2 Answers 2

2

For starters, you should be using std::vector and std::string instead of static arrays. Once you switch your code to use a vector, pulling in a file of integer data is as simple as:

std::ifstream fin("myfile.dat");
std::vector<int> myVec;
std::copy(std::istream_iterator<int>(fin), std::istream_iterator<int>(), std::back_inserter(myVec));

Which also makes the output fairly simple:

std::ofstream fout("myNewFile.dat");
std::copy(myVec.begin(), myVec.end(), std::ostream_iterator<int>(fout, " "));
Sign up to request clarification or add additional context in comments.

5 Comments

...or even simpler, like: std::vector<int> myVec{std::istream_iterator<int>(fin), std::istream_iterator<int>()};
Or... std::multiset<int> container{ std::istream_iterator<int>(fin), std::istream_iterator<int>() }; Sorting: sorted!
@Johnsyweb Given that this appears to be a homework assignment, I doubt it would be acceptable for him to use a set to sort it automatically. But outside of academia, yes.
@ZacHowland: True enough :-)
@Johnsyweb: Just keep in mind that using an std::multiset will generally trade off quite a bit of performance for the sake of a minor improvement in simplicity. For only 100000 items, it's probably fine, but if you have to deal with a lot of input, it may not be such a good idea.
1

Something like

std::vector<int> vec;
int x;
while (file >> x)
{
    vec.push_back(x);
}

might simplify your life.

4 Comments

Why write your own custom loop when you have one already made for you?
@ZacHowland I upvoted your answer! I left this one because the op is a beginner and that might be easier to grasp.
@ZacHowland Which was really a better answer than giving him the solution outright.
@CareyGregory His assignment appears to deal with an insertion sort, which none of us has addressed, so it appears safe to say that we haven't "given him the solution".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.