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;
}

8
  • 3
    Can you explain, in your own words, what you expect arr[line] to mean? What does this C++ code mean to you? Commented Sep 29, 2022 at 22:08
  • 2
    I think you need to start with a book. You might find one here. There are multiple problems with that line. Commented Sep 29, 2022 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. Commented Sep 29, 2022 at 22:21
  • I recommend using std::vector<std::string> arr; and arr.push_back(line); Commented Sep 29, 2022 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? Commented Sep 29, 2022 at 22:29

2 Answers 2

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

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

Comments

2

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;
}

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.