我正在尝试逐行读取文件输入到数组中,然后逐行将其输出到另一个文件。我的想法是读取每个单词并将其分配给数组索引,然后立即将存储在该索引中的单词输出到输出文件。我对使用Visual Studio的调试器或任何调试知之甚少。我只是尝试编译没有错误,但在使用此代码时,我收到一个错误说:
" EndOfFileDemo.exe中0x0119A1C6抛出异常:0xC0000005:访问冲突写入位置0xCCCCCCCC。
如果有此异常的句柄,程序可以安全地继续"
它给了我选项:Break,Continue或Ignore。
我希望有人可以查看我的代码并找到我没有看到的逻辑错误,或者我是否使用了错误的语法错误。
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
using std::string;
using std::ifstream;
using std::ofstream;
using std::cout;
int main( )
{
ifstream fin;
ofstream fout;
fin.open("Programming Assignment 3 Data.txt");
if (fin.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
fout.open("newOut.txt");
if (fout.fail( ))
{
cout << "Output file opening failed.\n";
exit(1);
}
char next; // a variable to store each word
fin >> next;
while(fin >> next) // I've read on several other questions that
{ // this is better than: while (! fin.eof())
string myArray[8];
myArray[0] = next;
fout << next << " ";
int i = 1;
while(next!='\n') //read and write until end of line
{
fin >> myArray[i];
fout << myArray[i] << " ";
i++;
fin >> next;
}
fout << endl; //output an end of line to begin next output on new line.
fin >> next;
}
fin.close();
fout.close();
system("Pause");
return 0;
}
输入的.txt文件读取:
h Vito 123
d Michael 234 Heart
n Sonny 456 6
a Luca 567 Business
r Tom 678 Talking Y
j Anthony 789 Maintenance N
d Nicos 891 Bone
它是医院员工的名单,每个单词稍后将存储在不同模型类中的自己的成员变量中。 例如,第二个条目将存储在类&#34; Doctor&#34;的对象中。使用以下成员变量: 角色 名称 数 专业
为了索引多个Doctors实例,我稍后会为该类的对象创建一个数组&#34; Doctor&#34;要存储。
但我想先了解如何正确输入和存储单个单词(稍后分配给变量)并将数据输出到文件。