0
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;

void make_array(ifstream& num, int (&array)[50]);

int main()
{
    ifstream file; // variable controlling the file
    char filename[100]; /// to handle calling the file name;
    int array[50];

    cout << "Please enter the name of the file you wish to process:";
    cin >> filename;
    cout << "\n";

    file.open(filename);

    if (file.fail()) {
        cout << "The file failed to open.\n";
        exit(1);
    } else {
        cout << "File Opened Successfully.\n";
    }

    make_array(file, array);

    file.close();

    return (0);
}

void make_array(ifstream& num, int (&array)[50])
{
    int i = 0; // counter variable

    while (!num.eof() && i < 50) {
        num >> array[i];
        i = i + 1;
    }

    for (i; i >= 0; i--) {
        cout << array[i] << "\n";
    }
}

I am trying to read values from a file to an array using fstream. When I try to display the contents of the array, I get 2 really big negative numbers, and then the contents of the file.

Any ideas what I did wrong?

9
  • 2
    I don't know what kind of values you're trying to read, but get reads single bytes (for reading numbers formatted as string, use num >> array[i]). Single bytes in C++ usually have the type char, and reference parameters need to match the type (no automatic type conversion possible). Commented Oct 29, 2014 at 0:21
  • 3
    Also, don't use while (!eof()). Replace it with the actual input operation. Commented Oct 29, 2014 at 0:22
  • I am trying to get int values. I am assuming the num >> array[i] only works with strings? Commented Oct 29, 2014 at 0:25
  • num >> array[i] works with any supported data-type, including int Commented Oct 29, 2014 at 0:27
  • Files never contain int values, files are composed of bytes. You can encode an int into bytes in a binary form or as text, and to read them you need to know how they where encoded. I'm pretty sure you mean the latter (i.e. you can read the number when opening it in a text editor), so yes you need indeed num >> array[i] (i.e. get is the wrong function). Commented Oct 29, 2014 at 0:27

2 Answers 2

1

Your use of num.get(array[i]) doesn't match any of its signatures. See get method description. What you want is this:

array[i] = num.get();
Sign up to request clarification or add additional context in comments.

Comments

0

As discussed in the comments, you try to read an integer which is encoded as text. For this, you need to use operator>> (which reads any type encoded as string) instead of get (which reads a single byte):

num >> array[i];

3 Comments

Good point but the OP hasn't clarified whether the file is text or binary. My answer assumed binary, which may be incorrect.
I have edited my post to include all of my code. After populating the array with "num >> array[i]" the output from the array is a load of random numbers that don't resemble those in the .dat file. any ideas why?
more specifically, it is two rather large negative numbers, and then the contents of the file itself.

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.