-7

I have two files and in one I have created simple class :

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include <unistd.h>
class myclass{ 
    protected:
        int ima,imb,imc,tm;
        fstream file;

 public: 
void creafile(string filename){ 
    string dir; 
    dir = "txtfile/"; 
    file.open((dir + filename).c_str(), ios::in | ios::out); 

    if(file.fail()){ 
    //  file.open(filename, ios::in | ios::out); 

      //if(file.fail()) 
      cout<<"Error when creating the file"<<endl; 
      exit(1); 
    } 
file.close(); 
}}

and my main file is called data.cpp and contain only this code:

using namespace std;
#include "mylib.h"
int main() {
    myclass dat,hi;
    dat.creafile("creatorfile.txt");
    return 0;
}

My problem is that I always get an error when calling creafile Error when creating the file. To make a simpler test case, I also tried the following code:

file.open("myfile.txt");
    if(!file){ 
      cout<<"Error when creating the file"<<endl; 
      exit(1); 
    } 
file.close();

However, it still gives the error Error when creating the file. I've tried using all flags ios::app ios::in ios::out etc but nothing changes. I have 500gb free space, and running Windows 7.

Tas
  • 7,023
  • 3
  • 36
  • 51
Phoenix
  • 467
  • 1
  • 11
  • 23

2 Answers2

2

According to the reference, ios::in | ios::out std::ios_base::openmode configuration will an generate error if the file does not exist, so you won't create a new one with that.

I don't know why you're using the member std::fstream, createfile could just be a static function that does not change any object. You're even closing it afterwards! It would create a file using a local std::ofstream, open mode of which is std::ios_base::out, which will create the file:

std::ofstream ofs(dir + filename); // .c_str() not needed since C++11
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
1

Point 1: You cannot open to read if the file doesn't exist. Fortunately you probably don't want to. Simultaneously reading and writing the same file is problematic and almost always a bad idea. Until you know you have to read and write at the same time,

  1. open the file for reading
  2. read in the file
  3. close the file.
  4. edit the file in memory
  5. open the file for writing
  6. write out the file
  7. close the file

If you have a really big file you can't store in memory,

  1. open the file for reading
  2. open a temporary file for writing
  3. read in part of the file
  4. edit the part you read
  5. write the part you read to temporary
  6. if more file, goto 3 (but don't use goto), else continue
  7. close file
  8. close temporary file
  9. delete file
  10. rename temporary file to file

Point 2: You have created the txtfile folder, but have you created it in the right place? Your development environment (include of conio.h suggests Visual Studio or antique) may not be running your program from where you think it is running.

Add this to your code in main:

char buf[4097]; // really big buffer
getcwd(buf, (int)sizeof(buf)); // get working directory
std::cout << buf << std::endl; // print the working directory

If the folder printed out is not where you made the txtfile folder, you can't open the file. If you want to automatically make the folder, read here: How to make a folder/directory

Point 3: exit(1); is a really big hammer. It is a nasty hammer. Read more here. Don't use it without a really, really good reason. In this case return is more than enough to get you out of the function, and if you add a return value to the function, main can test the return value to see if it should continue or return. Or you can throw an exception.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • i found really your useful explaintion but this is not answer of my question – Phoenix Dec 15 '15 at 08:09
  • @Phoenix one thing I missed when writing that is using perror to print out the cause of the error. [Documentation on perror.](https://msdn.microsoft.com/en-us/library/9t0e6085.aspx) You likely have two problems and hopefully knowing why the file didn't open will help you out. – user4581301 Dec 15 '15 at 08:23