0

In my code I'm opening an output stream and appending the data to the end of the file .. if there is no such file the stream should create one but the problem that it does not.

here is the code snippet:

char output_file[100];
strcpy(output_file, predicate.c_str());            
ofstream output_file_ptr1; 

output_file_ptr1.open(output_file,ios::out | ios::app | ios::binary );

if(output_file_ptr1.is_open()){

        output_file_ptr1 << subject <<" " << object <<"\n";
        output_file_ptr1.close();
}

else{
            printf("Error opening out file \n");
            return -1;
}

subject, object and predicate are variable strings that I created earlier.

Any idea it does not create the file? + it is very important for me that the data is appended to the end of the file.

Update: predicate is the exact file name that I need , but it is not a usual naming i.e

< http://www.w3.org/1999/02/22-rdf-syntax-ns#type>

is an example

Salma
  • 119
  • 3
  • 15
  • You don't need to the temporary `output_file` array. Since C++11 you can use `std::string` directly as the file name, and if you're using an older compiler and standard library just pass `predicate.c_str()` directly in the `open` call. – Some programmer dude Dec 06 '16 at 12:08
  • More related to your problem, what is the value of `predicate`? Is it an absolute or relative path? Do all directories in the path exist? Do you have the right to write in the directory where you want to create your file? – Some programmer dude Dec 06 '16 at 12:09
  • @Someprogrammerdude thank's but that's not acutely the issue here .. BTW I did try what you suggested but it did not solve the problem – Salma Dec 06 '16 at 12:13
  • @Someprogrammerdude it is the file name itself .. my file name are like "" – Salma Dec 06 '16 at 12:14
  • Indent your code properly ! http://stackoverflow.com/questions/17337602/how-to-get-error-message-when-ifstream-open-fails learn to look for error message. [Doc](http://stackoverflow.com/questions/17337602/how-to-get-error-message-when-ifstream-open-fails) – Stargateur Dec 06 '16 at 12:14

1 Answers1

1

A value such as <w3.org/1999/02/22-rdf-syntax-ns#type> is not a valid file name in most environments. Unix-style operating systems (e.g. Linux) do not support "/" inside the file name (unless the directory structure matches).

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65