翻译完成

This commit is contained in:
MjSeven 2021-07-18 21:49:38 +08:00
parent 7991865d74
commit e3b60d3f24
2 changed files with 281 additions and 284 deletions

View File

@ -1,284 +0,0 @@
[#]: subject: (How different programming languages read and write data)
[#]: via: (https://opensource.com/article/21/7/programming-read-write)
[#]: author: (Alan Smithee https://opensource.com/users/alansmithee)
[#]: collector: (lujun9972)
[#]: translator: (MjSeven)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
How different programming languages read and write data
======
Every programming language has a unique way of accomplishing a task;
that's why there are so many languages to choose from.
![Code going into a computer.][1]
In his article _[How different programming languages do the same thing][2]_, Jim Hall demonstrates how 13 different languages accomplish the same exact task with different syntax. The lesson is that programming languages tend to have many similarities, and once you know one programming language, you can learn another by figuring its syntax and structure.
In the same spirit, Jim's article compares how different programming languages read and write data. Whether that data comes from a configuration file or from a file a user creates, processing data on a storage device is a common task for coders. It's not practical to cover all programming languages in this way, but a recent Opensource.com series provides insight into different approaches taken by these coding languages:
* [C][3]
* [C++][4]
* [Java][5]
* [Groovy][6]
* [Lua][7]
* [Bash][8]
* [Python][9]
### Reading and writing data
The process of reading and writing data with a computer is similar to how you read and write data in real life. To access data in a book, you first open it, then you read words or you write new words into the book, and then you close the book.
When your code needs to read data from a file, you provide your code with a file location, and then the computer brings that data into its RAM and parses it from there. Similarly, when your code needs to write data to a file, the computer places new data into the system's in-memory write buffer and synchronizes it to the file on the storage device.
Here's some pseudo-code for these operations:
1. Load a file in memory.
2. Read the file's contents, or write data to the file.
3. Close the file.
### Reading data from a file
You can see three trends in how the languages in the Opensource.com series read files.
#### C
In C, opening a file can involve retrieving a single character (up to the `EOF` designator, signaling the end of the file) or a block of data, depending on your requirements and approach. It can feel like a mostly manual process, depending on your goal, but the general process is exactly what the other languages mimic.
```
FILE *infile;
int ch;
infile = [fopen][10](argv[1], "r");
 
do {
  ch = [fgetc][11](infile);
  if (ch != EOF) {
    [printf][12]("%c", ch);
  }
 } while (ch != EOF);
[fclose][13](infile);
```
You can also choose to load some portion of a file into the system buffer and then work out of the buffer.
```
FILE *infile;
char buffer[300];
 
infile = [fopen][10](argv[1], "r");
while (![feof][14](infile)) {
  size_t buffer_length;
  buffer_length = [fread][15](buffer, sizeof(char), 300, infile);
}
[printf][12]("%s", buffer);
[fclose][13](infile);
```
#### C++
C++ simplifies a few steps, allowing you to parse data as strings.
```
std::string sFilename = "example.txt";
std::ifstream fileSource(sFilename);
std::string buffer;
while (fileSource >> buffer) {
  std::cout << buffer << std::endl;
}
```
#### Java
Java and Groovy are similar to C++. They use a class called `Scanner` to set up a data object or stream containing the contents of the file of your choice. You can "scan" through the file by tokens (byte, line, integer, and many others).
```
[File][16] myFile = new [File][16]("example.txt");
Scanner myScanner = new Scanner(myFile);
  while (myScanner.hasNextLine()) {
    [String][17] line = myScanner.nextLine();
    [System][18].out.println(line);
  }
myScanner.close();
```
#### Groovy
```
def myFile = new [File][16]('example.txt')
def myScanner = new Scanner(myFile)
  while (myScanner.hasNextLine()) {
    def line = myScanner.nextLine()
    println(line)
  }
myScanner.close()
```
#### Lua
Lua and Python abstract the process further. You don't have to consciously create a data stream; you just assign a variable to the results of an `open` function and then parse the contents of the variable. It's quick, minimal, and easy.
```
myFile = io.open('example.txt', 'r')
lines = myFile:read("*all")
print(lines)
myFile:close()
```
#### Python
```
f = open('example.tmp', 'r')
for line in f:
    print(line)
f.close()
```
### Writing data to a file
In terms of code, writing is the inverse of reading. As such, the process for writing data to a file is basically the same as reading data from a file, except using different functions.
#### C
In C, you can write a character to a file with the `fputc` function.
```
`fputc(ch, outfile);`
```
Alternately, you can write data to the buffer with `fwrite`.
```
`fwrite(buffer, sizeof(char), buffer_length, outfile);`
```
#### C++
Because C++ uses the `ifstream` library to open a buffer for data, you can write data to the buffer, as with C (except with C++ libraries).
```
`std::cout << buffer << std::endl;`
```
#### Java
In Java, you can use the `FileWriter` class to create a data object that you can write data to. It works a lot like the `Scanner` class, except going the other way.
```
[FileWriter][19] myFileWriter = new [FileWriter][19]("example.txt", true);
myFileWriter.write("Hello world\n");
myFileWriter.close();
```
#### Groovy
Similarly, Groovy uses `FileWriter` but with a slightly "groovier" syntax.
```
new [FileWriter][19]("example.txt", true).with {
  write("Hello world\n")
  flush()
}
```
#### Lua
Lua and Python are similar, both using functions called `open` to load a file, `write` to put data into it, and `close` to close the file.
```
myFile = io.open('example.txt', 'a')
io.output(myFile)
io.write("hello world\n")
io.close(myFile)
```
#### Python
```
myFile = open('example.txt', 'w')
myFile.write('hello world')
myFile.close()
```
### File modes
Many languages specify a "mode" when opening files. Modes vary, but this is common notation:
* **w** to write
* **r** to read
* **r+** to read and write
* **a** to append only
Some languages, such as Java and Groovy, let you determine the mode based on which class you use to load the file.
Whichever way your programming language determines a file's mode, it's up to you to ensure that you're _appending_ data—unless you intend to overwrite a file with new data. Programming languages don't have built-in prompts to warn you against data loss, the way file choosers do.
### New language and old tricks
Every programming language has a unique way of accomplishing a task; that's why there are so many languages to choose from. You can and should choose the language that works best for you. But once you understand the basic constructs of programming, you can also feel free to try out different languages, without fear of not knowing how to accomplish basic tasks. More often than not, the pathways to a goal are similar, so they're easy to learn as long as you keep the basic concepts in mind.
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/7/programming-read-write
作者:[Alan Smithee][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/alansmithee
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/code_computer_development_programming.png?itok=4OM29-82 (Code going into a computer.)
[2]: https://opensource.com/article/21/4/compare-programming-languages
[3]: https://opensource.com/article/21/3/file-io-c
[4]: https://opensource.com/article/21/3/ccc-input-output
[5]: https://opensource.com/article/21/3/io-java
[6]: https://opensource.com/article/21/4/groovy-io
[7]: https://opensource.com/article/21/3/lua-files
[8]: https://opensource.com/article/21/3/input-output-bash
[9]: https://opensource.com/article/21/6/reading-and-writing-files-python
[10]: http://www.opengroup.org/onlinepubs/009695399/functions/fopen.html
[11]: http://www.opengroup.org/onlinepubs/009695399/functions/fgetc.html
[12]: http://www.opengroup.org/onlinepubs/009695399/functions/printf.html
[13]: http://www.opengroup.org/onlinepubs/009695399/functions/fclose.html
[14]: http://www.opengroup.org/onlinepubs/009695399/functions/feof.html
[15]: http://www.opengroup.org/onlinepubs/009695399/functions/fread.html
[16]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+file
[17]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string
[18]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+system
[19]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+filewriter

View File

@ -0,0 +1,281 @@
[#]: subject: "How different programming languages read and write data"
[#]: via: "https://opensource.com/article/21/7/programming-read-write"
[#]: author: "Alan Smithee https://opensource.com/users/alansmithee"
[#]: collector: "lujun9972"
[#]: translator: "MjSeven"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
不同的编程语言是如何读写数据的
======
每种编程语言都有其独特的完成任务的方式,这也说明了为什么有这么多语言可供选择。
![Code going into a computer.][1]
在 Jim Hall 的_[不同的编程语言如何完成相同的事情][2]_文章中他演示了 13 中不同的语言如何使用不同的语法来完成同一个任务。经验是,编程语言往往有很多相似之处。一旦你了解了一种编程语言,你就可以通过理解它的语法和结构来学习另一种。
本着同样的精神Jim 的文章比较了不同编程语言如何读写数据。无论数据来自配置文件还是用户创建的文件,在存储设备上处理数据都是程序员的常见任务。但以这种方式涵盖所有编程语言是不切实际的,最近的 Opensource.com 系列文章提供了对这些编程语言采用的不同方法的深入了解:
* [C][3]
* [C++][4]
* [Java][5]
* [Groovy][6]
* [Lua][7]
* [Bash][8]
* [Python][9]
### 读写数据
用计算机读写数据的过程和你在现实生活中读写数据的过程类似。要访问书中的数据,你首先要打开它,然后阅读单词或将生词写入书中,然后合上书。
当程序需要从文件中读取数据时,你向程序传入一个文件位置,然后计算机将该数据读入 RAM 中并解析它。同样,当程序需要将数据写入文件时,计算机会将新数据放入系统的内存写入缓冲区,然后将其同步到存储设备上的文件中。
下面是这些操作的一些伪代码:
1. 在内存中加载文件。
2. 读取文件内容,或将数据写入文件。
3. 关闭文件。
### 从文件中读取数据
从 Opensource.com 系列文章的语言中,你可以看到读取文件的三种趋势。
#### C
在 C 语言中,打开文件可能涉及检索单个字符,例如 `EOF` 指示符,表示文件结束,或数据块,具体取决于你的需求和方法。根据你的目标,它可能感觉像一个主要是手工的过程,但这正是其他语言所模仿的。
```c
FILE *infile;
int ch;
infile = fopen(argv[1], "r");
do {
ch = fgetc(infile);
if (ch != EOF) {
printf("%c", ch);
}
} while (ch != EOF);
fclose(infile);
```
你还可以选择将文件的某些部分加载到系统缓冲区中,然后在缓冲区外工作。
```c
FILE *infile;
char buffer[300];
infile = fopen(argv[1], "r");
while (!feof(infile)) {
size_t buffer_length;
buffer_length = fread(buffer, sizeof(char), 300, infile);
}
printf("%s", buffer);
fclose(infile);
```
#### C++
C++ 简化了一些步骤,允许你将数据解析为字符串。
```c++
std::string sFilename = "example.txt";
std::ifstream fileSource(sFilename);
std::string buffer;
while (fileSource >> buffer) {
std::cout << buffer << std::endl;
}
```
#### Java
Java 和 Groovy 和 C++ 类似。它们使用名为 `Scanner` 的类来设置数据流或对象,这样就会包含你选择的文件内容。你可以通过标记(字节、行、整数等)扫描文件。
```java
File myFile = new File("example.txt");
Scanner myScanner = new Scanner(myFile);
while (myScanner.hasNextLine()) {
String line = myScanner.nextLine();
System.out.println(line);
}
myScanner.close();
```
#### Groovy
```groovy
def myFile = new File('example.txt')
def myScanner = new Scanner(myFile)
while (myScanner.hasNextLine()) {
def line = myScanner.nextLine()
println(line)
}
myScanner.close()
```
#### Lua
Lua 和 Python 进一步抽象了整个过程。你不必有意识地创建数据流,你只需给一个变量赋值为 `open` 函数的返回值,然后解析该变量的内容。这种方式快速,最简且容易。
```lua
myFile = io.open('example.txt', 'r')
lines = myFile:read("*all")
print(lines)
myFile:close()
```
#### Python
```python
f = open('example.tmp', 'r')
for line in f:
print(line)
f.close()
```
### 向文件中写入数据
就写代码来说,写入是读取的逆过程。因此,将数据写入文件的过程与从文件中读取数据基本相同,只是使用了不同的函数。
#### C
在 C 语言中,你可以使用 `fputc` 函数将字符写入文件:
```c
fputc(ch, outfile);
```
或者,你可以使用 `fwrite` 将数据写入缓冲区。
```c
fwrite(buffer, sizeof(char), buffer_length, outfile);
```
#### C++
因为 C++ 使用 `ifstream` 库为数据打开缓冲区,所以你可以像 C 语言那样将数据写入缓冲区C++ 库除外)。
```c++
std::cout << buffer << std::endl;
```
#### Java
在 Java 中,你可以使用 `FileWriter` 类来创建一个可以写入数据的对象。它的工作方式与 `Scanner` 类非常相似,只是方向相反。
```java
[FileWriter][19] myFileWriter = new [FileWriter][19]("example.txt", true);
myFileWriter.write("Hello world\n");
myFileWriter.close();
```
#### Groovy
类似地Groovy 使用 `FileWriter`,但使用了稍微 "groovy" 的语法。
```groovy
new FileWriter("example.txt", true).with {
write("Hello world\n")
flush()
}
```
#### Lua
Lua 和 Python 很相似,都使用名为 `open` 的函数来加载文件,`writer` 函数来写入数据,`close` 函数用于关闭文件。
```Lua
myFile = io.open('example.txt', 'a')
io.output(myFile)
io.write("hello world\n")
io.close(myFile)xxxxxxxxxx myFile = io.open('example.txt', 'a')io.output(myFile)io.write("hello world\n")io.close(myFile)myFile = io.open('example.txt', 'a')io.output(myFile)io.write("hello world\n")io.close(myFile)
```
#### Python
```python
myFile = open('example.txt', 'w')
myFile.write('hello world')
myFile.close()
```
### File 模式
许多语言在打开文件时会指定一个“模式”。模式有很多,但这是常见的定义:
* **w** 表示写入
* **r** 表示读取
* **r+** 表示可读可写
* **a** 表示追加
某些语言,例如 Java 和 Groovy允许你根据用于加载文件的类来确定模式。
无论编程语言以何种方式来确定文件模式,你都需要确保你是在 _追加_ 数据,除非你打算用新数据覆盖文件。编程语言不像文件选择器那样,没有内置的提示来警告你防止数据丢失。
### 新语言和旧把戏
每种编程语言都有其独特完成任务的方式,这就是为什么有这么多语言可供选择。你可以而且应该选择最合适你的语言。但是,你一旦了解了编程的基本结构,你可以随意尝试其他语言,而不必担心不知道如何完成基本任务。通常情况下,实现目标的途径是相似的,所以只要你牢记基本概念,它们就很容易学习。
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/7/programming-read-write
作者:[Alan Smithee][a]
选题:[lujun9972][b]
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/alansmithee
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/code_computer_development_programming.png?itok=4OM29-82 "Code going into a computer."
[2]: https://opensource.com/article/21/4/compare-programming-languages
[3]: https://opensource.com/article/21/3/file-io-c
[4]: https://opensource.com/article/21/3/ccc-input-output
[5]: https://opensource.com/article/21/3/io-java
[6]: https://opensource.com/article/21/4/groovy-io
[7]: https://opensource.com/article/21/3/lua-files
[8]: https://opensource.com/article/21/3/input-output-bash
[9]: https://opensource.com/article/21/6/reading-and-writing-files-python
[10]: http://www.opengroup.org/onlinepubs/009695399/functions/fopen.html
[11]: http://www.opengroup.org/onlinepubs/009695399/functions/fgetc.html
[12]: http://www.opengroup.org/onlinepubs/009695399/functions/printf.html
[13]: http://www.opengroup.org/onlinepubs/009695399/functions/fclose.html
[14]: http://www.opengroup.org/onlinepubs/009695399/functions/feof.html
[15]: http://www.opengroup.org/onlinepubs/009695399/functions/fread.html
[16]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+file
[17]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string
[18]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+system
[19]: http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+filewriter