Segmentation fault:无法修复问题
Segmentation fault: unable to fix the problem
我是 C++ 的新手,我正在尝试编写一个通过命令行进行交互的项目。现在,每当我 运行 我的主程序(可执行文件)时,我总是在主程序完成时收到分段错误。
编辑评论:
导师告诉我尽可能少地使用 C++ 功能,例如向量或字符串...我对 C++ 也很陌生,所以我正在尝试尽可能多地使用基本 C 函数。
我是
我的主要功能如下所示:
int main(int argc, char** argv) {
cout << "starting mvote..." << endl;
int run_flag = 1;
char* actionBuffer = (char*)malloc(100 * sizeof(char));
char* action = (char*)malloc(16 * sizeof(char));
char* readPtr;
char exit[4] = { 'e','x','i','t' };
//parse command line argumentand get the filename
char* filename = argv[2];
cout << filename;
FILE* fp;
char line[64];
//from here, I'm opening the file and read it by lines
fp = fopen(filename, "r");
if (fp == NULL) {
cout << "file not exists";
return -1;
}
while (fgets(line, 64, fp) != NULL) {
cout << line << "\n";
}
fclose(fp);
while (run_flag == 1) {
cout << "what do you want?\n " << endl;
cin.getline(actionBuffer, 1024);
if (strcmp(actionBuffer, exit) == 0) {
cout << "bye!";
run_flag = 0;
break;
}
//if not exit, Look for the space in the input
readPtr = strchr(actionBuffer, ' ');
int size = readPtr - actionBuffer;
//extract the operation
strncpy(action, actionBuffer, size);
for (int i = 0; i < size; i++) {
cout << "operation:" << action[i];
}
// depend on the operation specified before the first empty space
run_flag = 0;
}
free(actionBuffer);
free(action);
return 0;
}
描述:
我首先尝试打开一个与 main 位于同一文件夹中的 csv 文件,然后逐行读取该文件。然后,我只是实现了一个简单的命令,您可以在其中键入 exit 并退出程序。
我分配了两个内存,actionBuffer和action,用来存放command
问题:当我键入 exit 并按回车然后进程结束时,分段错误 [core dumped] 总是存在。
研究:所以我了解到分段错误是由于访问了不属于我的内存。但是我在我的程序中的什么地方试图访问这样的内存?
如有任何建议,我们将不胜感激!谢谢。
只是给你一个想法,这将是一个 C++ 代码示例
#include<iostream>
#include<fstream>
#include<string_view>
#include<string>
#include<sstream>
#include<exception>
int main(int argc, char** argv) {
std::cout << "starting mvote...\n";
//parse command line argumentand get the filename
std::string filename = argv[2]; // NO CHECKS!
std::cout << filename <<'\n';
//from here, I'm opening the file and read it by lines
{
std::ifstream ifs(filename);
if (!ifs) {
throw std::invalid_argument("file not exists");
}
std::string line;
while (std::getline(ifs, line)) {
std::cout << line << '\n';
}
}
bool run_flag = true;
while (run_flag) {
std::cout << "what do you want?\n";
std::string userInput;
std::getline(std::cin, userInput);
if (userInput == "exit") {
std::cout << "bye!\n";
return 0;
}
std::stringstream userInputSs(userInput);
std::string operation;
while(userInputSs >> operation){
std::cout << "operation: " << operation << '\n';
}
}
}
我是 C++ 的新手,我正在尝试编写一个通过命令行进行交互的项目。现在,每当我 运行 我的主程序(可执行文件)时,我总是在主程序完成时收到分段错误。
编辑评论:
导师告诉我尽可能少地使用 C++ 功能,例如向量或字符串...我对 C++ 也很陌生,所以我正在尝试尽可能多地使用基本 C 函数。
我是 我的主要功能如下所示:
int main(int argc, char** argv) {
cout << "starting mvote..." << endl;
int run_flag = 1;
char* actionBuffer = (char*)malloc(100 * sizeof(char));
char* action = (char*)malloc(16 * sizeof(char));
char* readPtr;
char exit[4] = { 'e','x','i','t' };
//parse command line argumentand get the filename
char* filename = argv[2];
cout << filename;
FILE* fp;
char line[64];
//from here, I'm opening the file and read it by lines
fp = fopen(filename, "r");
if (fp == NULL) {
cout << "file not exists";
return -1;
}
while (fgets(line, 64, fp) != NULL) {
cout << line << "\n";
}
fclose(fp);
while (run_flag == 1) {
cout << "what do you want?\n " << endl;
cin.getline(actionBuffer, 1024);
if (strcmp(actionBuffer, exit) == 0) {
cout << "bye!";
run_flag = 0;
break;
}
//if not exit, Look for the space in the input
readPtr = strchr(actionBuffer, ' ');
int size = readPtr - actionBuffer;
//extract the operation
strncpy(action, actionBuffer, size);
for (int i = 0; i < size; i++) {
cout << "operation:" << action[i];
}
// depend on the operation specified before the first empty space
run_flag = 0;
}
free(actionBuffer);
free(action);
return 0;
}
描述:
我首先尝试打开一个与 main 位于同一文件夹中的 csv 文件,然后逐行读取该文件。然后,我只是实现了一个简单的命令,您可以在其中键入 exit 并退出程序。
我分配了两个内存,actionBuffer和action,用来存放command
问题:当我键入 exit 并按回车然后进程结束时,分段错误 [core dumped] 总是存在。
研究:所以我了解到分段错误是由于访问了不属于我的内存。但是我在我的程序中的什么地方试图访问这样的内存?
如有任何建议,我们将不胜感激!谢谢。
只是给你一个想法,这将是一个 C++ 代码示例
#include<iostream>
#include<fstream>
#include<string_view>
#include<string>
#include<sstream>
#include<exception>
int main(int argc, char** argv) {
std::cout << "starting mvote...\n";
//parse command line argumentand get the filename
std::string filename = argv[2]; // NO CHECKS!
std::cout << filename <<'\n';
//from here, I'm opening the file and read it by lines
{
std::ifstream ifs(filename);
if (!ifs) {
throw std::invalid_argument("file not exists");
}
std::string line;
while (std::getline(ifs, line)) {
std::cout << line << '\n';
}
}
bool run_flag = true;
while (run_flag) {
std::cout << "what do you want?\n";
std::string userInput;
std::getline(std::cin, userInput);
if (userInput == "exit") {
std::cout << "bye!\n";
return 0;
}
std::stringstream userInputSs(userInput);
std::string operation;
while(userInputSs >> operation){
std::cout << "operation: " << operation << '\n';
}
}
}