ofstream is not writing!!

Guys, i'm having trouble with my code... I just can't find any mistake, and this is freaking me out! It should open a file used as a Database, to write data in binary mode... But it does not write...

Please, someone help me:

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
void DatabaseHelper::EnsureInitialized()
      {
        //Get File Path (on this case, ./DB/Database.fb
        std::string destination= Database_Location;
        destination.append(DatabaseConfigsTypeName);
        destination.append(DatabaseDefaultExtension);
        
        //Close both streams
        DatabaseHelper::read.close();
        DatabaseHelper::write.close();
      
        //If file does not exist
        if (!DatabaseHelper::DoesExist(destination))
          {
            //Create File
            DatabaseHelper::write.open(destination.c_str(), std::fstream::app);
            //Close stream
            DatabaseHelper::write.close();
          }
        
       //Now, open for use 
        DatabaseHelper::write.open(destination.c_str(), std::fstream::out
            | std::fstream::ate | std::fstream::binary);
        
       //Now, open for use      
        DatabaseHelper::read.open(destination.c_str(), std::fstream::in
            | std::fstream::ate| std::fstream::binary);
        
      }
    

    //This method just check if file exists
    bool DatabaseHelper::DoesExist(std::string path)
      {
        std::FILE * file;
        
        if (file = fopen(path.c_str(), "r"))
          {
            fclose(file);
            return true;
          }
        else
          return false;
      }
    
    //This method ensure that the file has data. The file in question is a Database Register Counter
    void DatabaseHelper::EnsureHasData()
      {
       //If database is not OK, throws exception
        if (!DatabaseHelper::read.is_open())
          if (!DatabaseHelper::read.good())
            {
              std::cerr
                  << "Read stream is not OK. Check stream before calling this method!"
                  << std::endl;
              throw "Stream is not OK. Check stream before calling this method!";
            }
        
          //Go to end
          DatabaseHelper::read.seekg(0, std::ios::end);
          //Gets the pointer
          int ptrPosition = DatabaseHelper::read.tellg();

        //Compare with the data that should be there. Always go there
        if (ptrPosition != sizeof(DatabaseConfigurations))
          {
            //Although it's empty, the stream is always OK
            if (!DatabaseHelper::write.is_open())
              if (!DatabaseHelper::write.good())
                {
                  std::cerr
                      << "Write stream is not OK. Check stream before calling this method!"
                      << std::endl;
                  throw "Stream is not OK. Check stream before calling this method!";
                }
            
            //Allocate a temporary counter
            DatabaseConfigurations configs = DatabaseConfigurations();
            
            //Get's the pointer
            unsigned int * data = *configs;
            //Get's size
            int size = configs.GetSize();
            //This never happened
            if (size == 0)
              {
                std::cerr
                    << "Error recording database configuration data. Size is 0"
                    << std::endl;
                throw "Error recording database configuration data. Size is 0";
              }
            //Also this
            if (data == 0)
              {
                std::cerr
                    << "Error recording database configuration data. Data points to 0"
                    << std::endl;
                throw "Error recording database configuration data. Data points to 0";
              }
            
           //Start writing on file. Altough a simple cout tells me that the data that is being written is OK,
           // it does not write. What I do here is to get directly from the memory the values that should be
           // written, so I can add fields on DatabaseConfigurations without the need to change anything 
           // else in the code.
            for (int i = 0; i < size; ++i)
              {
                char * udata = new char[4];
                udata[0] = (unsigned char)(data[i] >> 24);
                udata[1] = (unsigned char)(data[i] >> 16);
                udata[2] = (unsigned char)(data[i] >> 8);
                udata[3] = (unsigned char)(data[i]);

                //Here it should write
                DatabaseHelper::write.write(udata, 4);
              }
            //And here it should flush
            DatabaseHelper::write.flush();
          }
        
      }




The system is running under a mipsel uprocessor, which is using a file system on my pc via NFS.

This is my /etc/exports

/opt/mipsroot 192.168.1.0/255.255.255.0(rw,sync,no_root_squash,no_subtree_check,insecure,no_wdelay,insecure_locks)

All compiled with gcc (for mipsel) version 3.2.3

I don't see any mistake, but it's not working! Hope you guys can help me!
Last edited on
I think that maybe you need to use this-> instead of DatabaseHelper:: inside of the methods. I've never seen anyone use the class name explicitly like that inside a method, so if it does work like this-> then feel free to correct me...
Using this-> means I need to have a reference for the class. Since is a static field, I need to use the DatabaseHelper:: to use it's fields on the .cpp file.
Last edited on
Also, I'm getting Illegal Seek errors from perror

1
2
3
4
5
                    const char *error_str = std::strerror(errno);
                    std::cout << error_str << std::endl;
                    error_str = new char[256];
                    perror(error_str);
                    std::cout << error_str << std::endl;


returns


1
2
Illegal seek                                                                    
Illegal seek


and sometimes

1
2
No such file or directory                                                       
No such file or directory 


altough the code is the same to create and read/write the file. It creates, but do not write.
Is there anyway you can trim what you've posted? The original post was huge and called methods that you didn't provide the source code for.

There isn't anything wrong the perror/strerror code above, you may have knackered the streams earlier with a memory overwrite.
Sure, focus on this:

1
2
    std::ifstream DatabaseHelper::read;
    std::ofstream DatabaseHelper::write;


1
2
3
4
5
6
7
8
    
       //Now, open for use 
        DatabaseHelper::write.open(destination.c_str(), std::fstream::out
            | std::fstream::ate | std::fstream::binary);
        
       //Now, open for use      
        DatabaseHelper::read.open(destination.c_str(), std::fstream::in
            | std::fstream::ate| std::fstream::binary);


and this:

1
2
3
4
5
6
7
8
9
10
11
12
13
  for (int i = 0; i < size; ++i)
              {
                char * udata = new char[4];
                udata[0] = (unsigned char)(data[i] >> 24);
                udata[1] = (unsigned char)(data[i] >> 16);
                udata[2] = (unsigned char)(data[i] >> 8);
                udata[3] = (unsigned char)(data[i]);

                //Here it should write
                DatabaseHelper::write.write(udata, 4);
              }
            //And here it should flush
            DatabaseHelper::write.flush();
Last edited on
Have you tried what you just posted? You're input and output streams are trying to use the same file. Is that what you intended?
I know that, but I can surelly tell you that I don't use both at the same time. And also, I commented the code that opens the read stream but the problem remains...

This code was working perfectly, but when I changed the compiler...

I was using gcc 4.2, now I have to use a 3.2.3 gcc compiled for mipsel.

I'll try valgrind today, let's see what it has to say...
Problem solved(?)...

If I use a <<std::endl; on the end of the writing, it flushes and write to file.

I'ts not a solution, but it's helping...
<< flush; also should work.
i m a noob in coding(see my post size).well i jus wanna ask wat this code is about as i havn't seen this kinda functions. r they inbuilt functions? n wat libraries do v need to include.
:)
Topic archived. No new replies allowed.