我真的很困惑为什么“1.txt”文件无法正确打开并提示“错误”消息,无论我如何尝试去做。我确实有类似的代码可以工作。但是,我试图在此基础上更正我的代码,但它无法正常工作。 我确实尝试使用文件的完整路径,而只是“1.txt”没有一个有用。
这是带问题的代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
struct instructions
{
unsigned int xd;
int head;
string tape;
string stateCurr;
char symbCurr;
char symbNew;
char direction;
string stateNew;
};
struct instrPack
{
instructions instr[1000];
};
ifstream file;
void readInstr(instrPack A[], int &xd);
void useInstrAndHead(instrPack A[], int xd);
int main()
{
int head, xd;
string tape;
instrPack lul[4];
cout << lul[1].instr[1].symbCurr;
//cout << "Head: " << head << endl;
readInstr(lul, xd);
useInstrAndHead(lul, xd);
return 0;
}
void readInstr(instrPack lul[], int &xd)
{
for(int i=0; i<4; i++)
{
string fileNum;//, fileNum2, fileNum3, fileNum4;
fileNum = """" + fileNum + ".txt""";;
file.open("C:\\Users\\clearless\\Desktop\\1.txt");
if (file.fail()) {
cerr << "ERROR" << endl;
exit(1);
}
lul[i].instr[0].xd = 0;
ifstream fd("C:\\Users\\clearless\\Desktop\\1.txt");
fd >> lul[i].instr[0].head;
fd >> lul[i].instr[0].tape;
//cout << "Tape: |" << tape << "|" << endl;
while (!fd.eof()) {
fd >> lul[i].instr[lul[i].instr[0].xd].stateCurr;
fd >> lul[i].instr[lul[i].instr[0].xd].symbCurr;
fd >> lul[i].instr[lul[i].instr[0].xd].symbNew;
fd >> lul[i].instr[lul[i].instr[0].xd].direction;
fd >> lul[i].instr[lul[i].instr[0].xd].stateNew;
lul[i].instr[0].xd++;
}
//fileNumK++;
}
/*
cout << "Instructions: " << endl << "----------" << endl;
for (int i=0;i<xd;i++)
{
cout <<instr[i].stateCurr << " " << instr[i].symbCurr << " " << instr[i].symbNew << " " <<instr[i].direction << " " <<instr[i].stateNew << endl;
}
*/
cout << "----------" << endl;
}
void useInstrAndHead(instrPack lul[], int xd)
{
for(int k=0; k<4; k++) {
int breakIt = 0;
string size = lul[k].instr[0].tape;
string stateInit = "0";
while ((lul[k].instr[0].head > 0) || (lul[k].instr[0].head <= size.length()) || (stateInit != "X")) {
for (int i = 0; i < lul[i].instr[0].xd; i++)
{
if ((lul[k].instr[0].tape[lul[k].instr[0].head] == lul[k].instr[i].symbCurr) &&
(stateInit == lul[k].instr[i].stateCurr))
{
lul[k].instr[0].tape[lul[k].instr[0].head] = lul[k].instr[i].symbNew;
if (lul[k].instr[i].direction == 'R')
{
lul[k].instr[0].head++;
}
else
{
lul[k].instr[0].head--;
}
stateInit = lul[k].instr[i].stateNew;
cout << lul[k].instr[0].tape << endl;
}
}
}
}
}
这是代码,它的工作原理非常相似(基本上,上面的代码来自下面的代码):
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
struct instructions
{
string stateCurr;
char symbCurr;
char symbNew;
char direction;
string stateNew;
};
ifstream file;
void readInstr(int &head, string &tape, instructions A[], int &xd);
void useInstrAndHead(int head, string tape, instructions A[], int xd);
int main()
{
int head, xd;
string tape;
instructions instr[1000];
cout << "Head: " << head << endl;
readInstr(head, tape, instr, xd);
useInstrAndHead(head, tape, instr, xd);
return 0;
}
void readInstr(int &head, string &tape, instructions instr[], int &xd)
{
string fileNum;
cin >> fileNum;
fileNum = """" + fileNum + ".txt""";
file.open(fileNum);
if (file.fail())
{
cerr << "ERROR" << endl;
exit(1);
}
xd=0;
ifstream fd(fileNum);
fd >> head;
fd >> tape;
cout << "Tape: |" << tape<< "|" << endl;
while(!fd.eof())
{
fd >> instr[xd].stateCurr;
fd >> instr[xd].symbCurr;
fd >> instr[xd].symbNew;
fd >> instr[xd].direction;
fd >> instr[xd].stateNew;
xd++;
}
cout << "Instructions: " << endl << "----------" << endl;
for (int i=0;i<xd;i++)
{
cout <<instr[i].stateCurr << " " << instr[i].symbCurr << " " << instr[i].symbNew << " " <<instr[i].direction << " " <<instr[i].stateNew << endl;
}
cout << "----------" << endl;
}
void useInstrAndHead(int head, string tape, instructions instr[], int xd)
{
int breakIt = 0;
string size = tape;
string stateInit = "0";
while((head>0) || (head<=size.length()) || (stateInit!="X"))
{
for(int i=0; i<xd; i++)
{
if((tape[head] == instr[i].symbCurr) && (stateInit == instr[i].stateCurr))
{
tape[head] = instr[i].symbNew;
if(instr[i].direction == 'R')
{
head++;
}
else
{
head--;
}
stateInit = instr[i].stateNew;
cout << tape << endl;
}
}
}
}
编辑:第一句语法不正确。