I am trying to get each elemnt of a text file with 13 integers in one line, each seperated by a space, into each slot of an array, any idea how to do this?

if each number has a space after it you can just use the >> operator of the ifstream object your using inside of a loop.

How far along are you?
Are you having troubles opening the text file? Reading the file? Creating the array? Putting the data into the array? etc.?

yeah, I tried that but im just getting null as a return.

Im writing a program that takes in a text file and prints in then prints it in reverse.

The contents of the text file is: 1 2 3 4 5 6 7 6 5 4 3 2 1

this is what ive already tried:

int main(void) 
{   
    int i, numbers[13], hold;

    ifstream fin("F:\problem5 YES.txt");
    if (! fin)
    {
        cout << "Cant open file";
    }


    cout << "The First Array is: " << '\n';     
    {   
        for(i=0;i<13;i++)
            fin >> numbers[i];
            cout << numbers << " ";
    }         

    cout << '\n';

    cout << ("The Array in reverse order is: ");        
    {        for(i=12;i>=0;i--)
                fin >> numbers[i];
                cout << numbers << " ";        
    } 
    cin >> hold;
} 

im having a problem with getting the text file into the array, other than that ive got it sorted

Is line 16 printing nothing to the screen?

So the name of the file you are trying to open is named "F:\problem5 YES.txt"?
(I am curious if that whitespace is allowed.)

Also, I usually put a second argument in the file opener:
For example,

ifstream fin("F:\problem5 YES.txt", ios::fin);

shouldn't cout << numbers << " "; be cout << numbers[i] << " ";?

When printing out the array backwards, you don't need to read from the file again, since you've already stored the values in the array. You have also done something odd with the curly braces, so that I'm not sure that the loop is doing what you want it to. So, you can change this:

cout << ("The Array in reverse order is: ");        
{        for(i=12;i>=0;i--)
            fin >> numbers[i];
            cout << numbers << " ";        
} 

to this:

cout << ("The Array in reverse order is: ");        
for( i = 12; i >= 0; i-- )
{
    cout << numbers[ i ] << " ";
}

Note how the braces start after the for ( i = 12... part. The way you had it before, only the line immediately after the for would be in the loop. I don't know if you meant to do that but the indentation suggested not.

Also, in C/C++ there is no automatic way to print out the contents of an array; you have to print each element explicitly (using the index to the element that you want to print), so you need to have cout << numbers[ i ] << " "; in a loop to print the whole array.

On a side note, there is a more succinct way to read a file like this, using algorithms:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
    std::ifstream fin( "F:\problem5 YES.txt" );
    if ( ! fin.is_open() )   // Use the "is_open" method to decide if the file is open
    {
        // Output to std error, not std out
        std::cerr << "Cant open file" std::endl;

        // Return here, rather than just carrying on
        return -1;
    }

    // A std::vector for holding the values from the file
    std::vector< int > v;

    // Use the std::copy algorithm to copy values from the file into the vector
    //
    // The important thing here is that you can iterate over the contents of a
    // file using a std::istream_iterator. The other "trick" is that the version
    // of std::istream_iterator that has no argument (the second one used in the
    // code below) represents the end of the stream that is being iterated over.
    //
    // std::back_inserter is an object that is able to insert values into a
    // std::vector as they arrive.  The vector will resize itself to take care
    // of the new values
    std::copy( std::istream_iterator< int >( fin ), std::istream_iterator< int >(),
        std::back_inserter( v ) );

    // Write the contents of the vector to screen
    std::copy( v.begin(), v.end(), std::ostream_iterator< int >( std::cout, " " ) );

    // Write the contents of the vector to the screen in reverse order
    std::copy( v.rbegin(), v.rend(), std::ostream_iterator< int >( std::cout, " " ) );

    return 0;
}

Containers (like std::vector), algorithms (like std::copy) and iterators (like std::istream_iterator) are a fundamental part of C++. They're also pretty cool, so definitely take some time to look them up :0)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.