Compiler Thinks Backslashes Are Escape Sequences

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

40 Replies - 2723 Views - Last Post: 31 March 2017 - 08:28 AM

#1 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 238
  • Joined: 29-October 16

Compiler Thinks Backslashes Are Escape Sequences

Posted 22 March 2017 - 11:32 AM

Hey all,
So I'm trying to write a string that contains "\\Hostname\" but when I do that it's thinking that they are escape characters.

I tried doubling them but got some weird results.

Googled it and didn't find a straight answer.
Thoughts?

Thanks guys/dals
Is This A Good Question/Topic? 0
  • +

Replies To: Compiler Thinks Backslashes Are Escape Sequences

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13220
  • View blog
  • Posts: 52,372
  • Joined: 12-June 08

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 22 March 2017 - 11:43 AM

Two back slashes nets you one. If you need two then you need four.

Quote

I tried doubling them but got some weird results.

I do not know what this means.
Was This Post Helpful? 1
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1983
  • View blog
  • Posts: 5,288
  • Joined: 27-December 05

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 22 March 2017 - 11:54 AM

You didn't specify a language, so here's C.
    printf("%s", "\\\\Hostname\\\"");

The first backslash escapes the second (so the second prints).
The third backslash escapes the fourth (so the fourth prints).
The fifth backslash escapes the sixth (so the sixth prints).
The seventh backslash escapes the second " (so the " prints).

(Maybe I should have said the 4th ". Depends on where you start counting.)

This post has been edited by r.stiltskin: 22 March 2017 - 11:56 AM

Was This Post Helpful? 1
  • +
  • -

#4 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 238
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 22 March 2017 - 01:08 PM

Whoops, sorry C++. :stupid:
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1983
  • View blog
  • Posts: 5,288
  • Joined: 27-December 05

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 22 March 2017 - 01:45 PM

No matter. The same principle applies in C++. Both \ and " are special characters so you have to use an escape sequence to print them. So \\ prints a backslash, \" prints a double quote.

But I just realized that you probably didn't want to print that " mark, so what I wrote earlier is wrong. It should be \\\\Hostname\\, so in C++ if you write something like
string s = "\\\\Hostname\\"
cout << s << endl;

I think you'll get what you wanted.

But if you did want the quotation marks just add \" before the first backslash and after the last one.
Was This Post Helpful? 1
  • +
  • -

#6 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 238
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 22 March 2017 - 01:57 PM

Awesome!! I'll try it right away!

Thanks guys!
Was This Post Helpful? 0
  • +
  • -

#7 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 238
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 22 March 2017 - 03:29 PM

Nice! Worked.

Now I'll have to figure out what's going on when I try and do the next phase which is basically going to take an input from a text box and inject that as a variable.
:bigsmile:
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1262
  • View blog
  • Posts: 4,970
  • Joined: 09-June 09

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 22 March 2017 - 04:12 PM

Quote

Now I'll have to figure out what's going on when I try and do the next phase which is basically going to take an input from a text box and inject that as a variable.


Shouldn't have to worry about the escape characters when the string is entered at runtime, since escape sequences are processed at compile time.
Was This Post Helpful? 1
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1983
  • View blog
  • Posts: 5,288
  • Joined: 27-December 05

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 22 March 2017 - 04:28 PM

Depends ...

that's true if the characters come in as input from an istream. But if entered as a command line argument the escapes are necessary.
Was This Post Helpful? 1
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1262
  • View blog
  • Posts: 4,970
  • Joined: 09-June 09

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 22 March 2017 - 04:35 PM

However, that is irrelevant to the user's software and has to do with how the shell handles backslashes. Thus the statement I made in post 4 is not a conditional statement.

He mentions that he will be receiving input from a text box so worrying about how the shell handles back slashes won't be necessary.

This post has been edited by jjl: 22 March 2017 - 04:37 PM

Was This Post Helpful? 1
  • +
  • -

#11 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 238
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 23 March 2017 - 10:06 AM

Nice!! thanks for the help. I've been too busy today to implement lol.

Probably going to do

		string hostname = "test";
		string myIpVar = "\\\\" + hostname + "\\";
		ofstream a_file("move.ps1");
		
		a_file << "\\\\Hostname\\";
		// Close the file stream explicitly
		a_file.close();

Was This Post Helpful? 0
  • +
  • -

#12 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 238
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 23 March 2017 - 10:35 AM

whoops sorry that was wrong. Disregard
Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1262
  • View blog
  • Posts: 4,970
  • Joined: 09-June 09

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 23 March 2017 - 12:15 PM

You could always just use forward slashes too if you don't want to worry about escaping back slashes..
Was This Post Helpful? 0
  • +
  • -

#14 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 238
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 23 March 2017 - 02:28 PM

Hey! To everyone helping me with this:

SAWEEET and THANK You

making progress...

#include <string>
#include <fstream>
#include <iostream>
#include <istream>
#include <ostream>


	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace std;
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
	{
		string hostname = "test";
		string myIpVar = "\\\\" + hostname + "\\";
		ofstream a_file("move.ps1");
		
		a_file << myIpVar;
		
		a_file.close();
		

	}




Yields Powershell script:

\\test\

AWWW GEEAAA ... now just need to take input from textbox as ^String and change it to a std string then throw into the IPvar... w00p w00p...

next step is to finish the rest of PS script then have it automatically move the files for me.

I'm using Copy-Item \\xxx.xxx.xxx.xxx\xxxx\xxx\GENERICDRIVERHERE.zip \\[InsertipVarHere]\

:gunsmilie: :bananaman:
Was This Post Helpful? 0
  • +
  • -

#15 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 238
  • Joined: 29-October 16

Re: Compiler Thinks Backslashes Are Escape Sequences

Posted 23 March 2017 - 02:55 PM

View Postjjl, on 23 March 2017 - 12:15 PM, said:

You could always just use forward slashes too if you don't want to worry about escaping back slashes..

Yeah normally I would do that but on my network here at work PS won't accept them ... this is a pet project of mine with C++/.NET/PS integration for lightweight ease of moving driver and vendor software files around
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3