反斜杠如何在传递给printf的双引号格式字符串中工作?

时间:2017-04-11 13:39:44

标签: bash printf

在bash中。

bash -version
GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)

debian8@debian:~$ printf "%-5s\n"  "hah"
hah 

很简单,要使用 \ n

换行
debian8@debian:~$ printf "%-5s\\n"  "hah"
hah 

因为 \\=\ printf"% - 5s \ n" "哈" == printf"% - 5s \ n" "哈"

debian8@debian:~$ printf "%-5s\\\n"  "hah"
hah  \ndebian8@debian:~$ 

字面意思是 \ n ,为什么没有新行呢? 为什么不\ = \ - > \\ = \ = \ - > \\ n = \ n = \ n?

我对以下多次逃避行动感到困惑。

debian8@debian:~$ printf "%-5s\\\\n"  "hah"
hah  \ndebian8@debian:~$ printf "%-5s\\\\\n"  "hah"
hah  \
debian8@debian:~$ printf "%-5s\\\\\\n"  "hah"
hah  \

请详细说明。

1 个答案:

答案 0 :(得分:1)

enter image description here 这里有两个逃脱,首先逃离&#34; ;第二次通过printf逃避 2. #include <iostream> #include<string> #include<fstream> #include<sstream> #include<cstdlib> #include<vector> using namespace std; int const number_of_columns = 25; // declares number of columbs of type int and a constant value // creating a string array for names of columns string column_names[number_of_columns] = { "Date Time", "Year", "Month", "Day", "Time", "Data Quality", "Temperature", "Temp Flag", "Dew Point Temp Flag", "Rel Hum Flag", "Wind Dir Flag", "Dew Point", "Rel Hum", "Wind Dir", "Wind Spd", "Wind Spd Flag", "Visibility", "Visibility Flag", "Stn Press", "Stn Press Flag", "Hmdx", "Hmdx Flag", "Wind Chill", "Weather" }; struct Row { string columns[number_of_columns]; }; int main() { // reading data from a string ifstream myfile("C:\\Users\\kaileba\\Downloads\\WindEnergy\\data.dat"); ofstream Output; if (myfile.fail()) { cout << "There seems to have been an error!, Good Bye!" << endl; } else { vector<Row> rows; string line; int row_index = 0; //Read rows from file and store in a vector for later use while (getline(myfile, line)) { stringstream linestream(line); string data; //create a new row struct and insert into the rows vector rows.push_back(Row()); int col_index = 0; while (getline(linestream, data, '\t')) { rows[row_index].columns[col_index] = data; col_index += 1; // if last piece of data increase the row index if (data.compare("NA") == 0) { row_index += 1; } } } //Open file so we can read to Output.open("Results.txt"); //Get the number of rows in the vector int number_of_rows = rows.size(); //Output all column names for (int i = 0; i < number_of_columns; i++) { if (column_names[i].compare("Weather") == 0) { cout << column_names[i]; Output << column_names[i]; } else { cout << column_names[i] << "\t"; Output << column_names[i] << "\t"; } } // Output all rows data for (int i = 0; i < number_of_rows; i++) { string temp_str = ""; for (int j = 0; j < number_of_columns; j++) { if (rows[i].columns[j].compare("NA") == 0) temp_str += rows[i].columns[j]; else temp_str += rows[i].columns[j] + "\t"; } cout << temp_str << "\n"; Output << temp_str << "\n"; } } system("pause"); return 0; } 从字面上看,它没有逃避功能 3.&#34;逃脱的结果首先进入printf,继续以与第二点相同的规则逃脱。