Cannot output in text file.

Hello there.

I have Ubuntu 14.04 with g++14 compiler. I need to output some arrays to a text file. It is all for debug but really important. I get empty files with no output.

I have this fstream definition:

1
2
3
struct Commons
{
  std::fstream outFile22,outFile33,outFile44,outFile55;


Then at one point in a subroutine:

1
2
3
4
5
6
7
8
public : void integrateNONrotated (int NN,int rotMatr)
  { Commons com;
    Prints pr;
    ...........
    com.outFile22.open ("dumpTest1.dat", std::ofstream::app);
    com.outFile33.open ("dumpTest2.dat", std::ofstream::app);
    ...........
    pr.printDebugThreeVar22 (NN,Input," Block ");


Then:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
void Prints::printDebugThreeVar22 (int NN, dcomp * Input, std::string explain)  // Input is dcomp array 1D 
  {
    Commons com;
    int countUnit=0,countZero=0, couBlockUnit=0,couBlockZero=0;
    bool unitsBlock = false;    
    if (rint (Input [0].real()) == 1)
    {
      unitsBlock = true;
      couBlockUnit += 1;
    }
    else
      couBlockZero += 1;
    for (int jj = 0; jj < NN; jj++)
    {
      if (unitsBlock) 
      {
        if (rint (Input [jj].real()) == 1)
        {
          countUnit += 1;
          com.outFile22 << jj << "  " << Input [jj].real() << endl;
          cout << jj << "  " << Input [jj].real() << endl;
        }
        else  
        {       
          countZero += 1;
          unitsBlock = false;
          com.outFile22 << "# Previous block of Units has length   " << countUnit << endl; 
          com.outFile22 << jj << "  " << Input [jj].real() << endl;
          cout << jj << "  " << Input [jj].real() << endl;
          countUnit = 0;
          couBlockUnit += 1;
        } 
      }
      else
      {
        if (rint (Input [jj].real()) == 0)
        {
          countZero += 1;
          com.outFile22 << jj << "  " << Input [jj].real() << endl;
          cout << jj << "  " << Input [jj].real() << endl;
        }
        else
        {
          countUnit += 1;
          unitsBlock = true;
          com.outFile22 << "# Previous block of Zeros has length " << countZero << endl; 
          com.outFile22 << jj << "  " << Input [jj].real();
          countZero = 0;
          couBlockZero += 1;
        }
      }
      if (jj == NN)
        if (countUnit > 0)
          com.outFile22 << "# Previous block of Units has length " << countUnit << endl;
        else
          com.outFile22 << "# Previous block of Zeros has length " 
                      << countZero << endl;  
        com.outFile22 << "Total Unit Blocks = ";
    }
    com.outFile22 << "  " << endl;
    com.outFile22 << "  " << endl;
  }     //  printDebugThreeVar22 


The idea here is to use this file dumpTest1.dat for periodic outputs in a for loop.

I also placed cout << statements for control and they work well. Why don't I have any output in my text files?

Then at the end of routine integrateNONrotated I close the files com.outFile22 and com.outFile33:

1
2
com.outFile22.close();
com.outFile33.close();


Thanks, - Alex
Last edited on
Look at this snippet:

1
2
3
4
5
6
7
8
public : void integrateNONrotated (int NN,int rotMatr)
  { Commons com;
    Prints pr;
    ...........
    com.outFile22.open ("dumpTest1.dat", std::ofstream::app);
    com.outFile33.open ("dumpTest2.dat", std::ofstream::app);
    ...........
    pr.printDebugThreeVar22 (NN,Input," Block ");


Is this function part of the Commons class?

Do you realize that you're creating a local instance of Commons class called com in this function? What happens when a local variable goes out of scope?

What is on line 3 of your printDebugThreeVar22()?
@jib, Commons is a structure. I use other variables located in this Structure in other parts of the program. I can always access those variables and change them.

I don't understand why I cannot use the std:fstream variables in a similar way.

integrateNONrotated is not a part of Commons structure. It is a part of another class: RealWork.

Do you realize that you're creating a local instance of Commons class called com in this function? What happens when a local variable goes out of scope?


It does not make sense to me. Could you be more specific?

@keskiverto, the line 3 is a declaration. printDebugThreeVas22 is a part of the class "Prints."

It is an awfully long and complicated program and I need a lot of intermediate output for controls.

Thank you guys for looking into it. Any more questions?

Thanks, - Alex

Last edited on
I want to stress that everything compiles, I get output in the std::output which is the terminal, it is exactly what I need. The output is simply 1 or 0 but I am looking into the blocks: how many units and how many zeroes I get. I can count them only if they are output in an external file, not the terminal. And I need not only the simple count. I need to evaluate the relative positions of the blocks of 0's & 1's.
Last edited on
I don't understand why I cannot use the std:fstream variables in a similar way.

You can, you're just doing it wrong. You seem to be creating a new instance of this class everywhere you want print something. This new instance does not have files attached to any of the variables.


It does not make sense to me. Could you be more specific?

Remember a class or a struct is just a User Defined Type of variable. They act the same way any of the base types would behave. If you create an instance of a variable within a function that variable is destroyed when the function ends, and unless you have provided a constructor the variables are not initialized to any valid values. You are using a local variable, not some more global variable that was defined earlier.

@jib What is the practical solution? I need to define an output file outside of the printDebugThreeVer22 routine and reuse it many times by appending. How can I do it?

Thanks, - A.
Last edited on
I have solved the problem. Thank you much @jib & @keskiverto.

- Alex
Last edited on
Topic archived. No new replies allowed.