没有直接的多行字符串格式,可以利用多行语句的语法实现,每一行后加\,只要不是将基本词法元素分开,都可以直接折行。基本词法元素就是 关键字、字符串、数字等等。
/*
FileName:multipleLineString.cpp
Function:多行字符串
CreateDate:2017-11-14
Author:李志新
*/
#include <iostream>
using namespace std;
int main(){
const char* ptr = "Lorem ipsum dolor sit amet consectetur adipisicing elit.\
Fuga nihil quidem ratione dolore. Qui velit deleniti earum natus sed,\
rem sit, nobis facere autem, inventore tempore doloremque voluptatibus.\
Maiores, sed?";
cout << ptr << endl;
return 0;
}
JavaScript 读取本地文件,一个是cookie 文件,也可以利用HTML5 中的FileReader 对象异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容。
参考 MDN上的文档。
构造函数FileReader(),返回一个新构造的FileReader。
方法概述
PowerShell 文件读写可以有两种方式实现,一种使用内置命令 Get-Content、 Set-Content、Out-File、重定向>和>>;
Get-Content -Path path/fileName -totalCount number | Set-Content path/fileName
另一种方式是使用.net 框架中的System.IO.StreamReader、System.IO.StreamWrite。
<#
filename:inputOupt.ps1
function:use .net implement file reader and writer.
author:李志新
datetime:2017-11-17
#>
$filename = "D:\Learn\PowerShell\a.txt";
$write = New-Object System.IO.StreamWriter($filename)
$write.Write("Hello, world!")
$write.Close()
$stringReader = New-Object System.IO.streamReader($filename);
$content = $stringReader.ReadToEnd()
$stringReader.Close()
$content
Python 文件的读写是同过内置函数 open 实现的,可以读取文本文件和二进制文件,语法如下:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
| Character | Meaning |
|---|---|
| 'r' | open for reading (default) |
| 'w' | open for writing, truncating the file first |
| 'x' | open for exclusive creation, failing if the file already exists |
| 'a' | open for writing, appending to the end of the file if it exists |
| 'b' | binary mode |
| 't' | text mode (default) |
| '+' | open a disk file for updating (reading and writing) |
| 'U' | universal newlines mode (deprecated) |
Methods of File Objects
通常使用with 语句打开文件with open("filename", "mode") as f:
#filename:scoreAnaylse.py
#function:file object read and write with use open function
#authoer:李志新
#datetime:2017-11-16
with open("score.txt") as f:
ls =f.readlines()
totalScore = 0
count = 0
for line in ls:
arr = line.split(",")
totalScore = totalScore + int(arr[4])
count = count + 1
print("There are have %d students in exam and avaerage is %.2f" %(count, totalScore / count))
JavaScript、PowerShell、Python都有专门的多行字符串语法,对应的分别是撇号`...`、@"..."@、''' ... '''。 还有一种变通利用多行语句实现(行尾加\),有C/C++、JavaScript、Python。