0
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;


int main()
{
    vector<int> temp;

    ifstream infile;
    infile.open("numbers");

    if (infile.fail())
    {
    cout << "Could not open file numbers." << "\n";
    return 1;
    }

    int data;
    infile >> data;
    while (!infile.eof()) {
    temp.push_back(data);
    infile >> data;
    }

    cout << data << " " << endl;



}

I am simply trying to cout all the numbers from the text file "numbers" using a vector.

15
10
32
24
50
60
25

My experience is pretty much nil, and some guidance on why this fails to open would be very helpful.

  • 3
    `data` isn't in scope when you print it. And [`while (!eof())` is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – chris Jun 09 '14 at 19:58
  • possible duplicate of http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – πάντα ῥεῖ Jun 09 '14 at 20:01
  • Not that I recommend using `eof` but done in that order it should be okay as far as not duplicating the last value. Of course, any other error would be ignored. `data` also looks like it is in scope to me when it is printed, but outside the loop it will only print whatever the last value happened to be. – Retired Ninja Jun 09 '14 at 20:15
  • Reading the last line of the question and some of your other comments, can you clarify the problem you're actually having? It sounds like you're having trouble opening the file in the first place. Is it in the same directory as the executable? Are you running this inside an ide? If so, you may need to set the working directory to where the numbers file is in order for your program to find it. – Retired Ninja Jun 09 '14 at 20:30

3 Answers3

2

Your code isn't working because you haven't attempted to print anything from the vector?

How do I print a vector?

Well first you have to understand how not to print a vector. The last line in your code, particularly this one:

cout << data << " " << endl;

is only printing out the last integer from the text file. In the loop where you performed the input, infile >> data overwrote each previous value of data and assigned it to the currently read value from the file. The result is that when the loop finishes, data will be equal to the last read value, particularly 25 looking at your file.

There's no overload for operator<<() that will allow you to do something like cout << temp, though you can implement one yourself. There exist several ways to print a vector, the easiest being a simple loop:

for (unsigned i = 0; i < temp.size(); ++i)
    std::cout << temp[i] << " ";

Bonus: A faster way to print all the integers would be to print data from inside the loop. There's also the answer @KerrekSB made.

0x499602D2
  • 87,005
  • 36
  • 149
  • 233
1

Your code is fine but you're printing the wrong thing. Change the bottom of main to this

int data;
while (infile >> data) {
    temp.push_back(data);
}

for( vector<int>::iterator i = temp.begin(); i != temp.end(); i++) {
    cout << *i << endl;
}

*Edited after reading the suggested dup.

Praxeolitic
  • 17,768
  • 12
  • 57
  • 109
  • Really? Compiled and ran for me. – Praxeolitic Jun 09 '14 at 20:02
  • See the suggested dupe! – πάντα ῥεῖ Jun 09 '14 at 20:03
  • "'i' does not name a type" I haven't seen auto before. :S –  Jun 09 '14 at 20:11
  • 1
    It's a C++11 feature. If you're using g++ or clang++ compile with -std=c++11. – Praxeolitic Jun 09 '14 at 20:12
  • Is your edited code working if I only replace "cout << data << " " << endl;" with your for loop? It still says couldn't find file. –  Jun 09 '14 at 20:26
  • Is a file named "numbers" in the same directory? Check file extension. The C++ code expects no extension. Your OS might hide the extension if you're viewing the directory graphically. – Praxeolitic Jun 09 '14 at 20:28
  • It was. However I didn't have .txt inside the quotation marks (wow). Your code also printed the numbers vertically whereas others printed horizontally. This is all I needed. Thank you. –  Jun 09 '14 at 20:31
  • FYI on the printing vertically - std::endl inserts a newline in the output stream (and flushes the output buffer, meaning it forces the program to print at that very moment). The '\n' in Kerrek's answer should also insert a newline after every number so the numbers should also come out vertical with that code. – Praxeolitic Jun 09 '14 at 20:40
  • If you were using C++11, you can use a range-based for loop to get rid of that ugly iterator code. `for( vector::iterator i = temp.begin(); i != temp.end(); i++)` turns into `for(auto i : temp)` – Doug Jun 09 '14 at 21:13
  • Sorry, I should have left a message in the answer. I removed that exact line. I felt like I shouldn't have been adding complexities. – Praxeolitic Jun 09 '14 at 21:15
0

Try this:

#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::ifstream infile("data.txt");

    if (!infile) { /* error opening file */ }

    for (int n : std::vector<int>(std::istream_iterator<int>(infile), {}))
    {
        std::cout << n << '\n';
    }
}

Of course you don't need the vector if you just want to process the numbers:

    for (std::istream_iterator<int> it(infile), end; it != end; ++it)
    {
        std::cout << *it << '\n';
    }
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • [Live demo](http://ideone.com/kAu5WT), [vectorless demo](http://ideone.com/gENOIy) – Kerrek SB Jun 09 '14 at 20:04
  • I would +1 this if it contained some explanation beside pure code. The OP apparently doesn't know about istream iterators and why it's bad to iterate on eof, just to mention these two. – Hiura Jun 09 '14 at 20:32