2017-08-10 35 views
-3

这里我展示了一段我的C++代码,它循环了一些k值,如果k = 0应该将输出发送到一个文件(outputk0.txt),如果k!= 0到另一个。outputnotk0.txt)编译时我有在out ofstream的文件范围的问题,任何人都知道如何解决这个问题C++:改变IF语句中的输出文件

for(int k = 0; k < number_of_l_steps; k++) 
    { 

    if(k == 0) 
     std::ofstream out("outputk0.txt", ios::out); 
    else 
     std::ofstream out("outputnotk0.txt", ios::out); 

    for(int i = 0; i < n_k; i++) 
    { 
     cout << i << endl; 
     double k = exp(log(kmin1) + i*(log(kmax) - log(kmin1))/(n_k-1.)); 
     for(int j = 0; j < n_z; j++) 
     { 
     double z = zmin + j*(zmax - zmin)/(n_z-1.); 
     // cout << k << " " << z << endl; 
     out << i << " " << j << " " << cv.U(z,k) << endl; 
     //out << i << " " << j << " " << gsl_matrix_get(Gl,i,j) << endl; 
     } 
    } 
    out.close(); 
    } 
+2

也许第一'的std :: ofstream的了;''之前声明if',然后'out.open(“outputk0.txt “,ios :: out)'和'out.open(”outputnotk0.txt“,ios :: out)'? – DimChtz

+0

你会得到一个编译时错误,抱怨'out'是一个未声明的标识符。那是因为'out'的作用范围是'if condition'。 – Raindrop7

+1

@ Raindrop7这正是OP通过声明“_我在编译时遇到'out'ofstream文件的范围有问题”。 –

回答

1

这应该修复它。?

std::unique_ptr<std::ofstream> out; 
if(k == 0) 
    out = new std::ofstream("outputk0.txt", ios::out); 
else 
    out = new std::ofstream("outputnotk0.txt", ios::out); 

你必须写

*out << i << " " << j << " " << cv.U(z,k) << endl; 

当然。


另一种选择,如评论所说,是使用open()功能:

std::ofstream out; 
if(k == 0) 
    out.open("outputk0.txt", ios::out); 
else 
    out.open("outputnotk0.txt", ios::out); 
1

你可以在一对夫妇的方式解决。 可能的解决方法#1:

申报std::ofstreamif声明的顶部,并调用open方法来打开该文件。

std::ofstream out; 
if(k == 0) 
    out.open("outputk0.txt", ios::out); 
else 
    out.open("outputnotk0.txt", ios::out); 

可能的解决方法(家庭)#2: 使用if-else在一个字符串,您可以使用传递到std::ofstream构造保存的文件名:

1.

std::ofstream out(k == 0 ? "outputk0.txt" : "outputnotk0.txt", ios::out); 

2 。

std::string fileName; 
if (k == 0) 
    fileName = "outputk0.txt"; 
else // more if cases if needed.. 
    fileName = "outputnotk0.txt"; 

std::ofstream out(fileName, ios::out); // use fileName.c_str() if not using C++-11. 

3.

std::string fileName; 
switch (k) 
    { 
    case 0: 
     fileName = "outputk0.txt"; 
     break; 
    // More cases if needed.. 
    default: 
     fileName = "outputnotk0.txt"; 
    } 

std::ofstream out(fileName, ios::out); // use fileName.c_str() if not using C++-11. 
0

标识符必须在编译时(特定类型的)这就是为什么我们不能在运行时创建新类型来定义。

在你的情况下,你正在创建一个“out”范围的对象,因此请记住,即使条件成功,标识符也不能在声明范围之外使用。

举个例子:

if(1) // always true condition 
    int a = 0; 
std::cout << a; // you get a compile-time error undeclared identifier 
  • 你可以做这样的事情的唯一情况是只有当你需要的只是内部的范围变量:

    if(SomConditionIsTrue){ 
        int var; 
        std::cin >> var; 
        std::cout << var; 
    } 
    
  • 考虑下面的例子:

    #if(true) 
        std::ofstream out("data.txt"); 
    #endif 
    
    out << "To be written into file"; // now it works 
    
    #if(false) 
        std::ofstream out("data.txt"); 
    #endif 
    
    out << "To be written into file"; // now it doesn't work because the condition fails so no `out` is declared consequently not added by the pre-processor. 
    

在你的情况外声明的对象,然后一定条件下将其初始化:

std::ofstream out("outputk0.txt", ios::out); 

for(;;){ 
    out << "something"; // out is visible here. the compiler looks up and sees it. 
}