PDA

View Full Version : Reading in a file in C++



Amulon
March 2nd, 2013, 10:20 PM
Here is the code to do sorting by insertion. I am getting an 'out of range error'. How do I better read in the text file and store it in the Vector without running into this problem?

Thanks in advanced



cout << "please enter the address of the file: " << endl;
getline(cin, file1); ifstream textFile1 (file1);


//Tests the file
if (textFile1.fail())
{
cout << "File was not found"<< endl;
system("PAUSE");
exit (1);
}
else

cout << "\nFile opened successfully" << endl;
do
{
textFile1 >> insertion[count1];
count1 ++;
}while(!textFile1.eof());


//Insertion Sort1
for(first_unsorted1 = 1; first_unsorted1 < count1; first_unsorted1++)
{
if(insertion[first_unsorted1] < insertion[first_unsorted1 - 1])
{
position1 = first_unsorted1;
current1 = insertion[first_unsorted1];
do
{
insertion[position1] = insertion[position1 - 1];
position1--;
}while(position1 > 0 && insertion[position1 - 1] > current1);
insertion[position1] = current1;
}
}

DougB1958
March 2nd, 2013, 10:38 PM
Do you initialize count1 anywhere? If not, doing so is the first thing I'd try.

Amulon
March 3rd, 2013, 03:25 AM
Yeah, all of the variables have been previously declared. Do you want me to provide that too?

trent.josephsen
March 3rd, 2013, 03:53 AM
Yeah, all of the variables have been previously declared. Do you want me to provide that too?

declaration != initialization

But when you're asking for help finding an error in your code, it's always good to provide a compilable example.

spjackson
March 3rd, 2013, 09:29 AM
Here is the code to do sorting by insertion. I am getting an 'out of range error'. How do I better read in the text file and store it in the Vector without running into this problem?



do
{
textFile1 >> insertion[count1];
count1 ++;
}while(!textFile1.eof());



In the absence of complete code, I'm going to guess that insertion is a std::vector<std::string>. If so, you need to grow the size of the vector, then write to the new element. push_back() does this.



do
{
std::string s;
textFile1 >> s;
insertion.push_back(s);
count1 ++;
}while(!textFile1.eof());

dwhitney67
March 3rd, 2013, 02:26 PM
Use a set, not a vector, if you want your string entries sorted.



#include <set>
#include <iterator>
#include <iostream>

int main()
{
using namespace std;

set<string> myStrings;

myStrings.insert("fum");
myStrings.insert("fee");
myStrings.insert("fi");
myStrings.insert("fo");

copy(myStrings.begin(), myStrings.end(), ostream_iterator<string>(cout, "\n"));
}

If you must use a vector, then I "smell" homework.

As for your do-while loop, there's an issue there as well:


do
{
textFile1 >> insertion[count1];
count1 ++;
}while(!textFile1.eof());

Do not attempt to insert into your container until you know that you have a valid entry to insert. eof() will NOT return true until you have actually attempted to read past the end of the file. Thus if the last entry in the file has successfully been read, then eof() will report false. It will take another read (and it will be unsuccessful) for eof() to return true.

This may be a better style loop to use:


string s;
while (textFile1 >> s)
{
// do something with s
}

P.S. If you anticipate having duplicate strings, then use a multiset.