0

I'm trying to read a file called numbers.txt and insert the integers into an array. My code outputs only the last integer in the file.

//numbers.txt
1
10
7
23
9
3
12
5
2
32
6
42

My code:

int main(){
ifstream myReadFile;
myReadFile.open("/Users/simanshrestha/Dev/PriorityQueue/PriorityQueue/numbers.txt");
char output[100];
int count = 0;
if (myReadFile.is_open()) {
    while (!myReadFile.eof()) {
        myReadFile >> output;
        //cout<< output << endl;
        count++;
    }
    for(int i=0;i<count;i++)
    {
        cout << output[i];
    }
    cout<<endl;
}
cout << "Number of lines: " << count<< endl;
myReadFile.close();
return 0;
}
wawaloo_17
  • 109
  • 2
  • 9
  • you are overriding output every time in the while loop... – Omid CompSCI Apr 10 '18 at 01:29
  • change char output[100] to vector output; and inside the while loop instead of myReadfile >> output do int temp_int = myReadFile >> output; output.push_back(temp_int); – Omid CompSCI Apr 10 '18 at 01:30
  • 1
    Recommend a read of [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Apr 10 '18 at 01:43
  • 1
    Possible duplicate of [Read integers from file - line by line](https://stackoverflow.com/questions/16123013/read-integers-from-file-line-by-line) – Joseph D. Apr 10 '18 at 02:27

3 Answers3

2
int main()
{
    std::ifstream myReadFile;
    myReadFile.open("/home/duoyi/numbers.txt");
    char output[100];
    int numbers[100];
    int count = 0;
    if (myReadFile.is_open())
    {
        while (myReadFile >> output && !myReadFile.eof())
        {
            numbers[count] = atoi(output);
            count++;
        }
        for(int i = 0; i < count; i++)
        {
            cout << numbers[i] << endl;
        }
    }
    cout << "Number of lines: " << count<< endl;
    myReadFile.close();
    return 0;
}

try this. atoi is a function Convert a string to an integer.

C.yixian
  • 49
  • 5
1

you can try this also: same as the bottom

basically you need to define a "temp" or holder variable to store your data. and anything inside a loop stays in that loop cause of scope resolution, and overriding data each time you store it since it doesn't exit at all.

Hope this helps!

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
const string FILE = "your file name here";

int main()
{
    ifstream myReadFile;
    myReadFile.open(FILE);
    char output[100];
    int numbers[100];
    int count = 0;
    if (myReadFile.is_open()){
        while (myReadFile >> output && !myReadFile.eof()) //not stopping until we reach end of file
        {
            numbers[count] = atoi(output); //converts string to int
            count++;
        }
        for(int i = 0; i < count; i++)
        {
            cout << numbers[i] << endl;
        }
    }
    cout << "Number of lines: " << count+1 << endl; //total number of lines in file
    myReadFile.close();
    else{ cout << "Error: File name not loaded" << endl;}
    return 0;
}
DeCastroAj
  • 40
  • 7
0

May I hazard a guess that your code is getting the sum of all the numbers and it is saved in the 1st element of your array? May I also guess you want the number of the first line in the text file to be saved in the first element of the array? 2nd line in 2nd element so on and so forth?

If so, the following code might need updating:

myReadFile >> output;

to

myReadFile >> output[count];

I'm quite sure this will work in C and assume this would work in C++ too

updated: another thing to add is to have 2D array like this:

char output[100][5]; //assuming our number is at most 5 char long

edm
  • 57
  • 7
  • He would have to cast each member to a character before storing into output since that is a character array. Unless it happens explicitly, but could have warnings turned on as compiler errors. – Omid CompSCI Apr 10 '18 at 01:36