num; //提示输入圆半径。 cout>rad; //提示输入输出文件名。 cout>文件名; file.open(文件名,fstream::in | fstream::trunc); 如果(!file.is_open()) { cout,c++,segmentation-fault,coredump,C++,Segmentation Fault,Coredump" /> num; //提示输入圆半径。 cout>rad; //提示输入输出文件名。 cout>文件名; file.open(文件名,fstream::in | fstream::trunc); 如果(!file.is_open()) { cout,c++,segmentation-fault,coredump,C++,Segmentation Fault,Coredump" />

写入文件时转储内核? 这是我为C++文件I/O实践编写的一个非常简单的代码。但是当我运行这个时,我得到了一个分段错误(内核转储)异常。 下面是我的代码: # include <iostream> # include <string> # include <cmath> # include <fstream> using namespace std; int main() { double num, rad, i, angle, x, y; char * filename; ofstream file; // prompt ask for number cout << "Enter the number of sample points: "; cin >> num; // prompt for circle radius. cout << "Enter the circle radius: "; cin >> rad; // prompt for output file name. cout << "Enter the output filename: "; cin >> filename; file.open (filename, fstream :: in | fstream :: trunc); if(!file.is_open()) { cout << "Error opening file " << filename << endl; cout << "Exiting..." << endl; return 0; } for(i = 1; i <= num; i ++) { //angle = 2 * M_PI * (i/num); //x = rad * cos(angle); //y = rad * sin(angle); //file << "\t" << x << "\t" << y << endl; file << "this is " << i << endl; } cout << "finished"; file.close(); return 0; } #包括 #包括 #包括 #包括 使用名称空间std; int main() { 双数值,rad,i,角度,x,y; 字符*文件名; 流文件; //提示询问号码 cout>num; //提示输入圆半径。 cout>rad; //提示输入输出文件名。 cout>文件名; file.open(文件名,fstream::in | fstream::trunc); 如果(!file.is_open()) { cout

写入文件时转储内核? 这是我为C++文件I/O实践编写的一个非常简单的代码。但是当我运行这个时,我得到了一个分段错误(内核转储)异常。 下面是我的代码: # include <iostream> # include <string> # include <cmath> # include <fstream> using namespace std; int main() { double num, rad, i, angle, x, y; char * filename; ofstream file; // prompt ask for number cout << "Enter the number of sample points: "; cin >> num; // prompt for circle radius. cout << "Enter the circle radius: "; cin >> rad; // prompt for output file name. cout << "Enter the output filename: "; cin >> filename; file.open (filename, fstream :: in | fstream :: trunc); if(!file.is_open()) { cout << "Error opening file " << filename << endl; cout << "Exiting..." << endl; return 0; } for(i = 1; i <= num; i ++) { //angle = 2 * M_PI * (i/num); //x = rad * cos(angle); //y = rad * sin(angle); //file << "\t" << x << "\t" << y << endl; file << "this is " << i << endl; } cout << "finished"; file.close(); return 0; } #包括 #包括 #包括 #包括 使用名称空间std; int main() { 双数值,rad,i,角度,x,y; 字符*文件名; 流文件; //提示询问号码 cout>num; //提示输入圆半径。 cout>rad; //提示输入输出文件名。 cout>文件名; file.open(文件名,fstream::in | fstream::trunc); 如果(!file.is_open()) { cout,c++,segmentation-fault,coredump,C++,Segmentation Fault,Coredump,cin>>filename将是未定义的行为,因为filename是未初始化的指针 如果要存储字符,需要为它们分配空间。因此,可以执行以下操作: char filename[150] = {0}; cin >> filename; // OK, you provide space for 149 characters. Will still break // if more characters are provided by the user. 或

cin>>filename
将是未定义的行为,因为
filename
是未初始化的指针

如果要存储字符,需要为它们分配空间。因此,可以执行以下操作:

char filename[150] = {0};
cin >> filename; // OK, you provide space for 149 characters. Will still break
                 // if more characters are provided by the user.
或:

#包括
std::string filename;//重载运算符>>和>filename;/*将在第一个空格处停止*/
std::getline(std::cin,filename);//更好:将在任何回车处停止

cin>
将是未定义的行为,因为
filename
是未初始化的指针

如果要存储字符,需要为它们分配空间。因此,可以执行以下操作:

char filename[150] = {0};
cin >> filename; // OK, you provide space for 149 characters. Will still break
                 // if more characters are provided by the user.
或:

#包括
std::string filename;//重载运算符>>和>filename;/*将在第一个空格处停止*/
std::getline(std::cin,filename);//更好:将在任何回车处停止

请为文件名分配一些内存,您只能使用一个端口。 改变

char  filename[50];

请为文件名分配一些内存,您只能使用一个端口。 改变

char  filename[50];

您好,这似乎是问题所在,但当我将
filename
更改为string时,我在将文件名传递到
file.open()时遇到了另一个问题
因为它需要const char*而不是string。当我尝试
static\u cast
它时,它甚至无法工作。你对解决这个问题有什么想法?谢谢you@Allan蒋:
std::string
c_str()
方法就是你想要的
const char*
打开(filename.c_str(),…)
请参考基类。您好,这似乎是问题所在,但当我将
文件名
更改为字符串时,我在将文件名传递到
文件.open()时遇到了另一个问题
因为它需要const char*而不是string。当我尝试
static\u cast
它时,它甚至无法工作。你对解决这个问题有什么想法?谢谢you@Allan蒋:
std::string
c_str()
方法就是你想要的
const char*
打开(filename.c_str(),…)
请参考基类。