Home > Software engineering >  How to resolve "error: ‘The’ does not name a type" when reading from txt file?
How to resolve "error: ‘The’ does not name a type" when reading from txt file?

Time:12-05

Currently trying to read from a text file using C I created and for it to loop to display the words. I tried used fstream and istream but for some reason I still receive this error saying

HarlemRenaissance.txt:1:1: error: ‘The’ does not name a type
    1 | The Harlem Renaissance was an intellectual 

Do anyone know what the problem may be?

Heres a snippit of my code:

#include <iostream>
#include <fstream>
#include <stdio.h>
#include "HarlemRenaissance.txt"
using namespace std;

int main() {
    //reads file
    // Create a text string, which is used to output the text file
    string myText;

    // Read from the text file
    std::ifstream MyReadFile("HarlemRenaissance.txt");

    // Use a while loop together with the getline() function to read the file line by line
    while (std::getline (MyReadFile,myText)) {
        
        // Output the text from the file
        std::cout << myText << std::endl;

    }
    
    //output data 
    outputStats();
    
    //closes file 
    MyReadFile.close();
}

CodePudding user response:

Remove the #include "HarlemRenaissance.txt" from your code. Practically include means a copy-paste. The content of the included file will be pasted in the sorce file. In this case, the content of your file is pasted into your source code. Perhaps it has some words that syntacticly incorrect. This is why you get this error message.

  • Related