0

I'm trying to read a file line by line and insert the lines into an array, but it won't compile. I'm using VS code and it highlights arr[line] and when I hover over it is says "no operator "[]" matches these operands". Can someone tell me what I'm doing wrong?

#include <fstream>
#include <string>
#include <iostream>
using namespace std;
   
int main() {
    char arr[4];
    string line;

    ifstream file;
    file.open("input.txt");
        if(file.is_open()) {
            while (getline(file, line))
        {
            file >> arr[line];  // the problem is with this line
        }
        file.close();
        }
    return 0;
}

Persixty
  • 8,165
  • 2
  • 13
  • 35
Alisha
  • 51
  • 3
  • 3
    Can you explain, in your own words, what you expect `arr[line]` to mean? What does this C++ code mean to you? – Sam Varshavchik Sep 29 '22 at 22:08
  • 2
    I think you need to start with a book. [You might find one here](https://stackoverflow.com/q/388242/631266). There are multiple problems with that line. – Avi Berger Sep 29 '22 at 22:09
  • Which approach do you want to use for reading the file? `getline(file, output)` is fine. `file >> output` is also a possibility. Combining the two is very unlikely to be what you want. – Ben Voigt Sep 29 '22 at 22:21
  • I recommend using `std::vector arr;` and `arr.push_back(line);` – Thomas Matthews Sep 29 '22 at 22:28
  • BTW, `char arr[4]` declares an array of 4 separate characters. If you are using C-Style strings, that would be 3 letters + the nul terminator. Is this intended? – Thomas Matthews Sep 29 '22 at 22:29
  • Reminder: `char` is one single character; no more, no less. `std::string` is for zero or more characters. – Thomas Matthews Sep 29 '22 at 22:31
  • `file >> arr[line];` There are several problems with that line. I am not sure what you wanted to do but it makes no sense to me to try to index an array of 4 total characters with a string containing a line of text. – drescherjm Sep 29 '22 at 22:40
  • Hi, Please don't update the question in light of answers. It makes the Question useless for any future reference. I've rolled-back the changes. – Persixty Oct 01 '22 at 13:54

2 Answers2

3

You're trying to access an array, using a string as an index, which is invalid in C++.

In plain English, this means you are telling the compiler to "Store the data from array with index called line into the file".

Then the compiler doesn't know what you want to do, because strings are not a supported type for array indexing.

I recommend reading more about indexing arrays here: https://www.cpp.edu/~elab/ECE114/Array.html

And you can try using the code located here: How to read lines of text from file and put them into an array

Kelsey
  • 174
  • 14
1

There is some scope of improvement in your code. When you are reading from file you don't know how many lines it contains. so to store those lines instead of using fixed size containers like array try to use dynamic size containers like vector.

here is example:

#include <fstream>
#include <string>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<string> mylines;
    string line;

    ifstream file;
    file.open("input.txt");
    
    if(file.is_open()) {
        while (getline(file, line))
        {
            mylines.push_back(line);
        }
        file.close();
    }
    for(int i=0;i<mylines.size();i++)
           cout<<mylines[i]<<endl;
   return 0;
}
Raj
  • 21
  • 1
  • 3