文件读取期间的隔离错误
Segfault during file read
程序应该提示用户输入用户名。 收到用户名后,它会将其与".history"连接以创建用户名.history。 然后它打开该文件(用户名.history)并从中读取输入。 不过,我在这里遇到了一个段错误。 每当它打开文件(由于文件不存在而为空)时,它都会读取多行,然后抛出段错误。 我认为问题可能源于我尝试打开文件的方式,但我不确定。 以下是导致问题的部分:
// File input and output
ifstream f_in;
ofstream f_out;
// Prompt user for their username.
char username[80];
cout << "Please input your username: " << endl;
cin >> username;
cout << endl;
cout << "Loading history file if it exists." << endl;
// Create file naem and initialize the file line counter to 0.
strcat(username, ".history");
int fcount = 0;
// Open file and read in lines if there are any.
// Place read lines into the command string for use later.
char tmp[50];
f_in.open(username);
while(!f_in.eof()){
f_in >> tmp;
cmd[fcount] = tmp;
fcount++;
}
f_in.close();
其他相关信息:cmd 被声明为全局变量(char cmd[200][50])
任何帮助都将得到极大的赞赏。
不确定这是否是唯一的问题,但cmd[fcount] = tmp
是错误的。您应该使用 strcpy()
.
而(f_in.good()){ f_in>> TMP; cmd[fcount] = tmp; fcount++;}
相关文章:
- 从矢量中删除元素后出现隔离错误
- 在 gtest 中初始化堆栈上的引用变量的隔离错误
- 线程时访问静态映射时出现隔离错误
- 并行快速排序分区中的隔离错误
- C++多线程程序:变量定义为类成员的隔离错误
- TFLite 隔离错误,通过获取C++输入和输出
- 我只是在寻找模板,在我的书中找到了这段代码,这显示了隔离错误?
- pthread_create中错误 4 的隔离错误
- 递归树遍历/分支删除的隔离错误
- 在类模板上使用 arm gcc 编译期间的隔离错误
- 从大量文件读取时出现隔离错误
- 在PHP扩展中使用emalloc从线程时出现隔离错误
- 通过 Boost Python 在C++对象之间传递共享指针的隔离错误
- 在QT中单击菜单时出现隔离错误
- 尝试访问标头声明成员时出现隔离错误
- 搜索链表时出现隔离错误
- 模板化子类析构函数中的隔离错误
- 插件中节点.js/Nan 回调C++不频繁的隔离错误
- 将行添加到 GTKTreeView 时的隔离错误
- 找不到命令时打开的隔离错误