每种编程语言中的文件I / O [英] File I/O in Every Programming Language

查看:138
本文介绍了每种编程语言中的文件I / O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是所有程序员不时遇到的常见问题。
如何从文本文件读取一行?那么下一个问题总是如何写回来。

当然,大多数人在日常编程中使用高级框架(在答案中可以很好地使用),但是有时知道如何在我也知道如何在 C C ++ Objective-C ,但是如果只是为了帮助我们做一个更好地决定使用什么语言来完成我们的文件。尤其是我认为看看它是如何在字符串操作语言中完成的,比如: python ruby​​ ,当然还有 perl



所以我想在这里我们可以创建一个 社区资源 ,我们都可以 star 添加到我们的配置文件中,并在需要使用某种新语言执行文件I / O 时参考。更不用说我们将接触到的语言,我们不会每天都处理这些语言。



这是您需要回答的方式:


  1. 创建一个名为 fileio.txt 的新文本文件
  2. 将第一行world添加到文本文件中。

  3. 将第一行hello li>将第二行world读入到输入字符串中。

  4. 将输入字符串输出到控制台。

澄清:


  • 每个答案都应该显示如何以一种编程语言只有。
  • 假设文本文件事先不存在

  • 在写入第一行之后,不需要重新打开文本文件



对语言没有特别的限制。
C C ++ C# Java Objective-C 都很棒。

如果您知道如何在 Prolog Haskell Fortran Lisp Basic 继续前进。

解决方案

Python 3



  open('fileio.txt','w')as f:
f.write('hello')
with open('fileio.txt','a')作为f:
f.write('\\\
world')
with open('fileio.txt')as f:
s = f.readlines()[1]
打印

说明




  • readlines()返回文件中所有行的列表
    因此,调用readlines()会导致读取文件的每一行。
    在这种情况下,使用readlines()是很好的,因为我们必须读取整个文件(我们希望它的最后一行)。
    但是如果我们的文件包含很多行,而我们只想打印它的第n行,则不需要读取整个文件。
    这里有一些更好的方法来获取Python中的第n行:什么是Python 3中的xreadlines()?

  • 这是什么声明?
    with语句启动一个代码块,您可以使用变量f作为流对象
    当with块结束时,python会自动调用f.close()。
    这样可以保证当你退出with block时,无论你何时退出块
    (即使你通过一个未处理的异常退出),文件都会被关闭。你可以明确地调用f.close(),但是如果你的代码引发了一个异常,并且你没有得到f.close()调用呢?这就是为什么with语句是有用的。
  • 在每个操作之前,不需要重新打开文件。你可以把整个代码写在一个块中。

      with open('fileio.txt','w +')as f :
    f.write('hello')
    f.write('\\\
    world')
    s = f.readlines()[1]
    print(s)

    我用三个块来表示这三个操作之间的区别:
    write(mode'w '),append(模式'a'),读取(模式'r',默认)。

    This has to be a common question that all programmers have from time to time. How do I read a line from a text file? Then the next question is always how do i write it back.

    Of course most of you use a high level framework in day to day programming (which are fine to use in answers) but sometimes it's nice to know how to do it at a low level too.

    I myself know how to do it in C, C++ and Objective-C, but it sure would be handy to see how it's done in all of the popular languages, if only to help us make a better decision about what language to do our file io in. In particular I think it would be interesting to see how its done in the string manipulation languages, like: python, ruby and of course perl.

    So I figure here we can create a community resource that we can all star to our profiles and refer to when we need to do file I/O in some new language. Not to mention the exposure we will all get to languages that we don't deal with on a day to day basis.

    This is how you need to answer:

    1. Create a new text file called "fileio.txt"
    2. Write the first line "hello" to the text file.
    3. Append the second line "world" to the text file.
    4. Read the second line "world" into an input string.
    5. Print the input string to the console.

    Clarification:

    • You should show how to do this in one programming language per answer only.
    • Assume that the text file doesn't exist beforehand
    • You don't need to reopen the text file after writing the first line

    No particular limit on the language. C, C++, C#, Java, Objective-C are all great.

    If you know how to do it in Prolog, Haskell, Fortran, Lisp, or Basic then please go right ahead.

    解决方案

    Python 3

    with open('fileio.txt', 'w') as f:
       f.write('hello')
    with open('fileio.txt', 'a') as f:
       f.write('\nworld')
    with open('fileio.txt') as f:
       s = f.readlines()[1]
    print(s)
    

    Clarifications

    • readlines() returns a list of all the lines in the file. Therefore, the invokation of readlines() results in reading each and every line of the file. In that particular case it's fine to use readlines() because we have to read the entire file anyway (we want its last line). But if our file contains many lines and we just want to print its nth line, it's unnecessary to read the entire file. Here are some better ways to get the nth line of a file in Python: What substitutes xreadlines() in Python 3?.

    • What is this with statement? The with statement starts a code block where you can use the variable f as a stream object returned from the call to open(). When the with block ends, python calls f.close() automatically. This guarantees the file will be closed when you exit the with block no matter how or when you exit the block (even if you exit it via an unhandled exception). You could call f.close() explicitly, but what if your code raises an exception and you don't get to the f.close() call? That's why the with statement is useful.

    • You don't need to reopen the file before each operation. You can write the whole code inside one with block.

      with open('fileio.txt', 'w+') as f:
          f.write('hello')
          f.write('\nworld')
          s = f.readlines()[1]
      print(s)
      

      I used three with blocks to emphsize the difference between the three operations: write (mode 'w'), append (mode 'a'), read (mode 'r', the default).

    这篇关于每种编程语言中的文件I / O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆