文件的读写

C/C++的多行字符串

没有直接的多行字符串格式,可以利用多行语句的语法实现,每一行后加\,只要不是将基本词法元素分开,都可以直接折行。基本词法元素就是 关键字、字符串、数字等等。

/*
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 的文件读写

JavaScript 读取本地文件,一个是cookie 文件,也可以利用HTML5 中的FileReader 对象异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容。

参考 MDN上的文档

构造函数FileReader(),返回一个新构造的FileReader。

方法概述

PowerShell 的文件读写

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 文件的读写

Python 文件的读写是同过内置函数 open 实现的,可以读取文本文件和二进制文件,语法如下:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

file
可以是文件名(相对路径或者绝对路径),或者是一个文件的描述符(一个整数)。
mode
文件打开方式的字符串。
默认值是'r',表示打开并以文本模式读取文件。
'w' 表示可以写入文件(清空原有文件内容);'a' 表示追加文件内容;
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)
encoding
encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent, but any text encoding supported by Python can be used.

Methods of File Objects

  • fileOjbect.read(size)
  • fileOjbect.readline()
  • fileOjbect.readlines()
  • fileOjbect.write(string)
  • fileOjbect.tell()
  • fileOjbect.seek(offset, fromWhat)
  • fileOjbect.close()

通常使用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。