views:

158

answers:

5

I'm trying to get this C++ code to input a series of numbers from a text file:

    int x = 0;
    cin >> x;

    ifstream iffer;
    int numbers[12];
    iffer.open("input.txt");
    for (int i = 0; i < 12; i++){
        iffer >> numbers[i];
    }

This doesn't seem to work on the Mac. Every cell will equal to 0 regardless of the values in the text file. In other words, the ifstream isn't assigning the numbers.

How can I make this work? Is it a Mac issue and if so, how can I get it to work?

Thanks!

Anthony Glyadchenko

+1  A: 

Tried (VC9.0):

#include <iostream>
#include <fstream>

int main()
{
    using namespace std;

    int x = 0;
    cin >> x;

    ifstream iffer;
    int numbers[12];
    iffer.open("input.txt");
    for (int i = 0; i < 12; i++){
        iffer >> numbers[i];
    }
    for (int i = 1; i < 12; i++){
        numbers[i] = i;
    }

    return 0;
}

That worked, but the second loop is wrong.

Klaim
Why isn't this working on a Mac?
Anthony Glyadchenko
+2  A: 

Maybe opening the file failed?
You can check if the failbit of iffer is set with fail()

if(iffer.fail())
{
  cout << "Failed to open file." << endl;
}
Andreas Klebinger
Nope. It returns false.
Anthony Glyadchenko
Well the code works on Linux also just tested it.How did you check for the value of the fields? If you didn't use a debugger could you post the complete code?
Andreas Klebinger
Wait, you can open a non-existent file for reading?
Jefromi
So weird, I tested to make sure I wasn't getting my languages mixed up and it looked like I was right, but now I tested again and I was wrong. Sorry for the confusion. Don't know what kind of idiocy I did to make it look like open wasn't failing.
Chuck
+1  A: 

It should work, but be aware, that "get" will always just read one character. This is what I tested, also on Mac OSX, but this has nothing to do with your OS, since it is standard C++:

#include <iostream>
#include <fstream>
using namespace std;

int main(){
    ifstream iffer;
    iffer.open("input.txt");
    char numbers[12];
    int i = 0;
    while (iffer.good()){
        numbers[i] =  iffer.get();
        i++;
    }
    for (int n = 0; n < 8; n++){
        cout << numbers[n];
    }        
    cout << endl;
    iffer.close();
}

The file "input.txt" I am reading in. Make sure this file is in your working directory!:

12345678

While reading the file every character will be stored in your array. So when you have a file like

1 2 3 4 5 6 

your array will contain

numbers[0] = '1'
numbers[1] = ' ' 
numbers[2] = '2'
numbers[3] = ' '
...
Lucas
I don't think reading in individual characters really addresses the OP's question...
Jefromi
@Jefromi: I just reread the question. I think are right.
Lucas
But if the file looks like he describes it, this will also become an issue. '12' would for example be stored under two separate indices with the corresponding ascii values 49 and 50.
Lucas
A: 

This sounds like a path issue. You have an "input.txt" file somewhere, but not in the current directory. If this is in a GUI application, keep in mind that the current directory is somewhat unpredictable and you should either give an absolute path or a path relative to some known path (e.g. the path to the current application CFBundle).

Just to test, I just created a Unix program including your precise code wrapped in the following code:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    int x = 0;
    // your code
    cout << numbers[5] << endl;
    return 0;
}

It worked, so if this is in a command-line program and you launched it from the right directory, you must be changing the current directory somewhere in your app.

Chuck
A: 
Mr.Ree