.txt File Location

I can't seem to create a .txt file in C:\.
NOTE: I have tried many different combinations including C:\, C:\\, C:/, and C://.

I don't get any errors when I use C:\ but it doesn't create any text file.

1
2
3
4
5
6
7
8
9
10
11
12
int main() {
    char newtext[] = "inputText";
    ofstream file;
    
    file.open("C:\example.txt");
    if (file.is_open()) {
        file << newtext;
    } else {
        cout << "Something went wrong" << endl;
    }
return 0;
}
For a "\" to appear in a string you have to type it twice
try: file.open("C:\\example.txt"); this should work

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main()
{
    std::string a = "before\after",
              b = "before\\after";

    std::cout << a << "\n" << b << "\n";

    return 0;
}


Will display
beforefter
before\after


EDIT: Note that in my example, while reading the string the program encounters "\a" which makes a beep when encountered. In your case for file.open("C:\example.txt"); the program will read "\e" and since it doesn't mean anything to it, it will discard it, leaving you with the following string: "C:xample.txt"

See the following link for more ressource: https://en.cppreference.com/w/cpp/language/escape
Last edited on
Thank you for the quick reply.
I did mention that I tried two like this: "C:\\"; but that doesn't seem to work
I also tried using a string or char[] when using file.open.
I tried your code and it works, maybe you just failed to locate the file where it created it in your C: folder?
It is incorrect to say that a non-standard escape sequence such as \e is "ignored". It is implementation-defined what happens. In particular, many implementations will interpret \e as \x1b (ASCII ESC), so for me "\example" becomes "\x1bxample" which (again, for me at least) prints xample in red. As another example, \hello prints hello, so although the \ is "ignored", the h isn't.

And it's probably best to just use a forward slash instead of two backslashes even in Windows. So "C:/whatever" should work. But you may not have permission to write to C:/, depending on which version of Windows you're using.
Check the output operation, check that the close operation succeeded.

The best way to get information out of the filestream interface is to check errno. The library isn't guaranteed to set it, but the exception mask doesn't carry sufficient information, and ios_base::failure is even worse.

The mainstream implementations leave a useful value in errno, in any event. Best check it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cerrno>
#include <cstring>
#include <fstream>
#include <iostream>

int main() {
  std::ofstream os;
  os.exceptions(~std::ios::iostate{});

  try {
    os.open("C:\\example.txt");
    os << "whatever\n";
    os.close();
  } catch (std::ios_base::failure const &e) {
    std::cerr << "error:" << std::strerror(errno) << '\n';
    return 1;
  }
  
  std::cout << "no error\n";
}



@tpb
It is incorrect to say that a non-standard escape sequence such as \e is "ignored". It is implementation-defined what happens. In particular, many implementations will interpret \e as \x1b (ASCII ESC), so for me "\example" becomes "\x1bxample" which (again, for me at least) prints xample in red. As another example, \hello prints hello, so although the \ is "ignored", the h isn't.


My bad, thank you for clarifying that!
@hoogo, I mispoke saying \e printed the following text in red (I don't know what I was thinking!). It is indeed interpretted as the "escape" character, but to print in a color you need to follow it by a special sequence of characters including the color code. So to print in red you need to do something like this (where 31 means red, the foreground color codes going from 30 to 37):

 
    std::cout << "\e[31mTHIS IS PRINTED IN RED\e[m\n";

Last edited on
@tpb
No worries :). Thanks for the info by the way, I had never looked into printing different colours in the command prompt, always keen on learning new things!
Cheers!
Topic archived. No new replies allowed.