i/o files with arrays

Pages: 12
i'm supposed to input a file of numbers into an array and I get an infinite loop of "<<<<<<<<". How do you read the numbers into the array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#include <fstream>
//Jacob Wilt
//Array Work-Out
using namespace std;
const int MAX = 50;
int numbers[MAX];
//int swaps(), negative(), average1(), average2(), average3(), larger(), smaller(), sub();
int main()
{
    ifstream inputFile;
    ofstream outputFile;
    inputFile.open("numbers.txt");
    outputFile.open("workout.dat");
    inputFile >> numbers;
    while (!inputFile.eof())
    {
        inputFile >> numbers;
        outputFile << numbers << " ";
        cout << numbers << " ";
    }

    return 0;
}
Well, I tried that code, but it doesn't compile as written. I'm not sure about the infinite loop, but most likely the input file was not opened successfully.

There are a few things wrong with the code. When accessing the values in an array, you need to supply a subscript, for example inputFile >> numbers[0] rather than just inputFile >> numbers.

Also, testing for eof() is generally a poor idea. The file access may fail for many reasons other than end of file, for example there may be non-numeric data, or (as you perhaps discovered) the file might not even be open.

So here's some code to read the numbers into an array. After that has completed, the contents of the array are displayed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>

using namespace std;

const int MAX = 50;
int numbers[MAX];

int main()
{
    ifstream inputFile;

    inputFile.open("numbers.txt");

    if (!inputFile.is_open())
    {
        cout << "inputFile is not open" << endl;
        return 1;
    }

    int n = 0; // Count the numbers as we read them in

    while (n<MAX && inputFile >> numbers[n]) 
    {
        n++;
    };

    // Output the numbers
    for (int i=0; i<n; i++)
    {
        cout << i << " " << numbers[i] << endl;
    }

    return 0;
}

The one line which may look a little tricky is line 23
while (n<MAX && inputFile >> numbers[n])
Here the while condition is testing two separate things. First, that the subscript n is within the size of the array, and then it tries to read an integer and checks whether it was successful. If all was well, n is increased by 1 and the loop is repeated.
My file is not opening. Do you have to save the file to a certain location? Or is the file supposed to be opened or closed?
The file needs to be in the location where the program expects to find it. By default that could be one of two places. Either the same directory as the executable program, or perhaps in the "current working directory" which might vary depending on how your system is configured.

One way of making sure the file will be found is to specify the full path, something like "D:\\temp\\input.txt"
Notice that any '\' character in the path must be represented by a double '\\'
how can i get the file from the desktop?
> how can i get the file from the desktop?

Give the complete path. Should be something like this, IIRC:

ifstream inputFile( "C:\\Documents and Settings\\jwilt\\Desktop\\numbers.txt" ) ;
i used the location given in the properties of the file and still it didn't work
Did you remember to use double backslashes?
yeah and still doesn't open
Can you show your latest code please.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
//Jacob Wilt
//Array Work-Out
using namespace std;
const int MAX = 50;
int numbers[MAX];
//int swaps(), negative(), average1(), average2(), average3(), larger(), smaller(), sub();
ifstream inputFile;
ofstream outputFile;
int main()
{
    int i = 0;
    inputFile.open("C:\\Users\\Jacob\\Desktop\\numbers.txt");
    //outputFile.open("workout.dat");
    inputFile >> numbers[i];
    if (!inputFile.is_open())
    {
        cout << "inputFile is not open" << endl;
        return 0;
    }
    else
    {
    int n = 0;
    while (n<MAX && inputFile >> numbers[n])
    {
        n++;
    }
    for (int i=0; i<n; i++)
    {
        cout << i << " " << numbers[i] << endl;
    }
    }
    return 0;
}
Perhaps your machine is set up differently lo mine. In my case it would be something like this:
 
    inputFile.open("C:\\Documents and Settings\\Jacob\\Desktop\\numbers.txt");

rather than
 
    inputFile.open("C:\\Users\\Jacob\\Desktop\\numbers.txt");


Though you need to get over this obstacle first, a couple of other comments. Line 16 is not needed, this would discard the first data item.
 
    inputFile >> numbers[i]; // not needed 

Also the else and corresponding braces at line 22 isn't necessary, it just clutters up the code. If the previous if was satisfied, the return statement at line 20 has already ended the program, so if you get past that point, you know the file must be ok.
well i just tried starting a new program and copied my code into it and now it runs
so now when i try to output that to my file should the file open when i compile my code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
//Jacob Wilt
//Array Work-Out
using namespace std;
const int MAX = 50;
int numbers[MAX];
//int swaps(), negative(), average1(), average2(), average3(), larger(), smaller(), sub();
ifstream inputFile;
ofstream outputFile;
int main()
{
    int i = 0;
    inputFile.open("C:\\Users\\Jacob\\Desktop\\numbers.txt");
    outputFile.open("workout.dat");
    if (!inputFile.is_open())
    {
        cout << "inputFile is not open" << endl;
        return 0;
    }
    int n = 0;
    while (n<MAX && inputFile >> numbers[n])
    {
        n++;
    }
    for (int i=0; i<n; i++)
    {
        outputFile << i << " " << numbers[i] << endl;
    }
    return 0;
}
That looks reasonable. Is it working?
my file doesn't open but when i go to find the file the information is there. is that how it is supposed to work?
I don't understand. There are two files, one input, one output. They must have both opened successfully if you can find the information there.

Though it is worth checking the date and time the output was created, just to make sure it's the one you think it is.
well i mean i'm putting the information in the file and it goes there but is the notepad supposed to pop up after it compiles or do you always have to find the file?
It sounds like it's working the way it's supposed to.
okay. How would i pass the array to the "negative" function and return the value?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <fstream>
//Jacob Wilt
//Array Work-Out
using namespace std;
const int MAX = 50;
int numbers[MAX];
int swaps(), negative[int], average1(double, double), average2(), average3(), larger(), smaller(), sub();
ifstream inputFile;
ofstream outputFile;
int main()
{
    double total = 0;
    int cnt = 0;
    int i = 0;
    inputFile.open("C:\\Users\\Jacob\\Desktop\\numbers.txt");
    outputFile.open("C:\\Users\\Jacob\\Desktop\\workout.txt");
    if (!inputFile.is_open())
    {
        cout << "inputFile is not open" << endl;
        return 0;
    }
    int n = 0;
    while (n<MAX && inputFile >> numbers[n])
    {
        n++;
    }
    for (int i=1; i<n; i++)
    {
        total = total + numbers[i];
        outputFile << i << " = " << numbers[i] << "   ";
        cnt++;
        if (cnt % 8 == 0)
        {
            outputFile << endl;
        }
        if (numbers[i] < 0)
        {
            negative(numbers[i]);
        }
    }
    average1(total, i);

    outputFile << endl << average1 << endl;
    inputFile.close();
    outputFile.close();
    return 0;
}
int swaps()
{

}
int negative[int number[i]]
{
    number[i] = number[i] * (-1);
    return number[i];
}
int average1(double tot, double i1)
{
    double av1 = tot / i1;
    return av1;
}
int average2()
{

}
int average3()
{

}
int larger()
{

}
int smaller()
{

}
int sub()
{

}
Pages: 12