0
#include <iostream>

#include <fstream>

#include <iomanip>

#include <cstdlib>

using namespace std;

int main() {
    int numberArray[] = {};
    int atoi(const char * str); // this fuction is used to convert the data into int
    char line[10]; //  this is a char array 
    const char filenames[] = {
        'NumFile500.txt',
        'NumFile5K.txt',
        'NumFile25K.txt',
        'NumFile100K.txt'
    };

    FILE * file; // declaring the FILE pointer as file pointer 
    for (int k = 0; k < 4; k++) {
        int i = 0;
        file = fopen(filenames[k], "r"); // error on this line while trying to open a text file for reading mode 

        while (fgets(line, sizeof line, file) != NULL) { // keep looping until NULL pointer...  
            numberArray[i]= atoi(line);
            cout << atoi(line) << endl; // convert string to double float
        }
    }

}

Just want to read list of integers line by line from multiple text files but I am getting error invalid conversion from 'char' to 'const char*' [-fpermissive] I am new to CPP. pls help me with this

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Voting to close question as typo: All the `'` should be `"`. – user17732522 May 08 '22 at 18:38
  • You used single quotes for `filenames` instead of double quotes (e.g. `'NumFile500.txt'` should be `"NumFile500.txt"`, same for all others). Protip: always solve compiler errors from the top one. Even if you don't understand error, it may be a simple typo that induces more errors later. – Yksisarvinen May 08 '22 at 18:39
  • Or alternatively duplicate of [Single quotes vs. double quotes in C or C++](https://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c-or-c.) – user17732522 May 08 '22 at 18:39
  • The declaration `int atoi(const char * str); ` also doesn't belong there btw. I have no idea why you thought it should be declared at all. And since you are writing C++, not C, there isn't really a reason to use C's file IO instead of C++ file IO. You even included `#include ` instead of the correct C header. It seems to me that you might need a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn C++ from. – user17732522 May 08 '22 at 18:42

1 Answers1

2

For starters this declaration

int numberArray[] = {};

is invalid. The initializer list may not be empty when the size of the array is not specified.

This declaration

const char filenames[] = {
    'NumFile500.txt',
    'NumFile5K.txt',
    'NumFile25K.txt',
    'NumFile100K.txt'
};

is also incorrect. You need to use string literals instead of multicharacter literals and the element type of the array shall be const char *

const char * filenames[] = {
    "NumFile500.txt",
    "NumFile5K.txt",
    "NumFile25K.txt",
    "NumFile100K.txt"
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335