1.

Solve : Drawing a blank on C++ and write " " to file?

Answer»

Drawing a blank on how to write "." to config.conf file without C++ thinking " " is the start and stop of string to write to file. Did a quick google search for like cout<<"""\n"; to display "" and no results, since this is related to the same problem.

Below is quick example code similar to what I am using.



Code: [Select]#include <IOSTREAM>
#include <fstream>
using namespace STD;

int main () {
  ofstream myfile;
  myfile.open ("config.conf", ios::app);
  myfile << " "." \n";
  myfile.close();
  return 0;
}Maybe if you had typed "C++ quotes in string" into Google you would have had better luck...

MODIFYING a character in a string from its normal meaning is called "escaping" and is done in many languages by preceding the character you want to modify by an "escape character". The escape character in Java,  Perl, Python, C and C++ is a backslash. In these languages there are two escape styles.

1. More usual, I think, you put the backslash before the character you want to modify.

printf("Dave Lembke said \"I want quotes in a string\" string.\n");

2. Another way is to use a backslash, a lowercase 'x' and the ASCII code (in hex) of the character (omitting the character itself thus...

printf("Dave Lembke said \x22I want quotes in a string\x22 string.\n");

This will only work in an ASCII environment.




Sorry, hasty editing made those examples get the word "string" twice... this looks a bit better...

printf("Dave Lembke said \"I want quotes in a string\" in his post today.\n");

printf("Dave Lembke said \x22I want quotes in a string\x22 in his post today.\n");

So try this in your code

myfile << " \".\" \n";

Thanks Salmon that fixed it. For some reason I was thinking it was Quote

'
for escape character. Now I know its Quote
\
. Was thinking it was like cout<<"Print this '".'" \n"; Figured not to include that in my post for help since it was way wrong. Also forgot the correct terminology of "Escaping", but knew that there was a way to lead it with another character to have it ignore the " inside the string to print or write.

Been playing with C++ off and on for last 13 years, and it wasnt until now that I need to MAKE a dynamic config file that I ran into this issue, since I normally dont quote out information to user or to files. When I went to compile my source and saw the many errors blatting about the same thing I was like hmm its looking at those quotes and way back in 1998 I remember the college professor going into this issue and that a leading character tagged the quotes as ignore, but what was it and what was the correct terminology for what I need to do, which you answered both.

Thanks


Discussion

No Comment Found