Results 1 to 6 of 6

Thread: Reading in a file in C++

  1. #1
    Join Date
    Jan 2013
    Beans
    17

    Reading in a file in C++

    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

    Code:
        
            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;
    		}
    	}

    Last edited by Amulon; March 2nd, 2013 at 10:23 PM.

  2. #2
    Join Date
    May 2007
    Beans
    60

    Re: Reading in a file in C++

    Do you initialize count1 anywhere? If not, doing so is the first thing I'd try.

  3. #3
    Join Date
    Jan 2013
    Beans
    17

    Re: Reading in a file in C++

    Yeah, all of the variables have been previously declared. Do you want me to provide that too?

  4. #4
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Reading in a file in C++

    Quote Originally Posted by Amulon View Post
    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.

  5. #5
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Reading in a file in C++

    Quote Originally Posted by Amulon View Post
    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?

    Code:
    
        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.

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

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Reading in a file in C++

    Use a set, not a vector, if you want your string entries sorted.

    Code:
    #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:
    Code:
    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:
    Code:
    string s;
    while (textFile1 >> s)
    {
        // do something with s
    }
    P.S. If you anticipate having duplicate strings, then use a multiset.
    Last edited by dwhitney67; March 3rd, 2013 at 02:37 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •