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;
}
arr[line]to mean? What does this C++ code mean to you?getline(file, output)is fine.file >> outputis also a possibility. Combining the two is very unlikely to be what you want.std::vector<std::string> arr;andarr.push_back(line);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?