3

The program output Should be:

The numbers are: 101 102 103 104 105 106 107 108 108 110

But my output is:

The numbers are: 0 0 0 0 0 0 0 0 1606416272 32767

This is my code:

// This program reads data from a file into an array.

#include <iostream>
#include <fstream> // To use ifstream
using namespace std;

int main()
{
    const int ARRAY_SIZE = 10; // Array size
    int numbers[ARRAY_SIZE];   // Array number with 10 elements
    int count = 0;             // Loop counter variable
    ifstream inputFile;        // Input file stream object

    // Open the file.
    inputFile.open("TenNumbers.rtf");

    // Read the numbers from the file into the array.
    while (count < ARRAY_SIZE && inputFile >> numbers[count]){
        count++;
    }

    // Close the file.
    inputFile.close();

    // Display the numbers read:
    cout << "The numbers are: ";
    for (count = 0; count < ARRAY_SIZE; count++){
        cout << numbers[count] << " ";
    }

    cout << endl;

    return 0;
}

This is the contents of the TenNumbers.rtf file I'm reading the data from:

101
102
103
104
105
106
107
108
109
110

UPDATE 1: I tried using txt file but the results are similar.

The numbers are: 0 0 0 0 0 0 0 0 1573448712 32767

UPDATE 2: I found where the issue was. After running if (inputFile.good()) I found out the file was not getting opened.

5
  • Check state of inputFile after opening and each inputFile >> numbers[count] operation separately to make sure there is no errors in loading from the file. Also consider what should happen if the file contained less than ARRAY_SIZE numbers. I would also suggest using std::array or std::vector which perform more checks in good debuggers. Commented Sep 6, 2015 at 20:06
  • I do not really know the .rtf format. Are you sure it stores the text in an ASCII compatible format? If not, that might be the problem. A quick glance over the wiki page suggests that it at least might contain extra formatting characters. Try with a normal .txt file. Commented Sep 6, 2015 at 20:16
  • I tried using txt file but the results are similar. The numbers are: 0 0 0 0 0 0 0 0 1573448712 32767 Commented Sep 6, 2015 at 20:20
  • Possible duplicates: c++ read file array Commented Sep 6, 2015 at 20:26
  • I compiled your source code and executed it, it runs perfectly, probably you are tring to open a file that does not exist. Check the names. Commented Sep 6, 2015 at 20:53

5 Answers 5

4

Hi I have compiled your code, with the .txt it runs well, without gives the strage numbers that you see. So probably you are opening a file that does not exists, or can not be red.

// This program reads data from a file into an array.

#include <iostream>
#include <fstream> // To use ifstream
#include <vector>
using namespace std;

int main()
{
    std::vector<int> numbers;
    ifstream inputFile("c.txt");        // Input file stream object

    // Check if exists and then open the file.
    if (inputFile.good()) {
        // Push items into a vector
        int current_number = 0;
        while (inputFile >> current_number){
            numbers.push_back(current_number);
        }

        // Close the file.
        inputFile.close();

        // Display the numbers read:
        cout << "The numbers are: ";
        for (int count = 0; count < numbers.size(); count++){
            cout << numbers[count] << " ";
        }

        cout << endl;
    }else {
        cout << "Error!";
        _exit(0);
    }

    return 0;
}

This snippet checks if the file exists, raises an error if not, and uses a vector(more suitable in c++)

Sign up to request clarification or add additional context in comments.

Comments

1

Your file name has rtf as suffix. Does it contain any RTF info in it?

The error that I see in your code is that you are assuming ARRAY_SIZE number of ints were successfully read when you are printing the numbers.

Use:

// Display the numbers read:
cout << "Number of ints read: " << count << std::endl;
cout << "The numbers are: ";
for (int i = 0; i < count; i++){
    cout << numbers[i] << " ";
}

This will, most likely, reveal any problems in reading the data.

1 Comment

quick side note: don't use count as a variable name unless you dont do using namespace std;
1

ARRAY_SIZE is the number of ints you allocated in the array; that is, it is the max number of ints.

count is the actual number of ints read from the file. So your final loop should go up to count since that is the number of actual data. So the loop that prints your data should be:

int i;
for (i = 0; i < count; ++i)
    cout << numbers[count] << " ";

Or you can walk a pointer:

int *start;

for (start = numbers; (numbers - start) < count; ++numbers)
    cout << *numbers << " ";

Also, I think the file extension should be "txt" rather than "rtf", but that doesn't make a difference.

Comments

0

An RTF file is not just plain text (it's surrounded by markup) and the character encoding may differ, thus resulting in wrong interpretation of the numbers.

So, in your reading loop:

// Read the numbers from the file into the array.
while (count < ARRAY_SIZE && inputFile >> numbers[count]){
    count++;
}

the input stream inputFile by default is skipping white spaces which in your case could be encoded differently, thereby skipped or messed up in some way.

Note: Try and add a test line that prints the read number before you store it in the array.

Comments

0

I had met this problem before too. I copy the content into a new file and save as different name. Then it will be fine when run it again.

Comments

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.