samedi 2 mai 2020

convert ".py" to ".txt" in c++

I'm trying to create an output.txt file from a .py python file in c++.

I'm trying to: - copy code from python into vector - create outputText file - open that outputText file - read the outputText file (which has python code) but I'm getting troubles (please look at code)

using namespace std;
int main(int argc, char** argv) {
  string fileName = argv[1];
  vector<string> pythonData;
  ifstream pythonFile(fileName);
  string linePython;
  while (getline(pythonFile, linePython)) {
    pythonData.push_back(linePython);
  }
  pythonFile.close();

  cout << fileName << endl;
  for (unsigned int i = 0; i < pythonData.size(); i++) {
    cout << pythonData[i] << endl;
  }
  string pythonFileName = "pythonCode.txt";
  ofstream pythonCode(pythonFileName.c_str());
  if (pythonCode.is_open()) {
    // cout << "outputfile inshallah\n";
    for (int i = 0; i < pythonData.size(); i++) {
      pythonCode << pythonData[i].c_str() << "\n";
    }
  }
  pythonCode.close();
  /// UP UNTIL THIS POINT I WAS FINE...
  // BUT TROUBLE COMES IN WITH CODE BELOW
  ifstream readFile(pythonCode);
  string line;
  int i = 0;
  if (readFile.is_open()) {
    while (getline(readFile, line)) {
      cout << line << endl;
      cout << i++ << endl;  // processes( call functions..)
    }
  }
}

I would greatly appreciate any solutions! Hopefully its clear. Also assume that I included all the header files

Aucun commentaire:

Enregistrer un commentaire