Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1040093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:10:53+00:00 2026-05-16T15:10:53+00:00

This has to be a common question that all programmers have from time to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:10:54+00:00Added an answer on May 16, 2026 at 3:10 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 507k
  • Answers 507k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are not putting a " before the end of… May 16, 2026 at 4:08 pm
  • Editorial Team
    Editorial Team added an answer You should escape the urlString using "stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding" instead of escaping… May 16, 2026 at 4:08 pm
  • Editorial Team
    Editorial Team added an answer something like arr = [ ["choice1", ['a', [2, 4]], ['b',… May 16, 2026 at 4:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Sorry, I know that this topic has been covered a bit. I've read the
So one of the common tasks that I do as a programmer is debugging
I have a question regarding the following code: abstract class a { public static
Does Emacs have the capability to open recent files (e.g. a menu like File
This is a very common problem when Excel Worksheet or Chart is embedded into
TL;DR: All controls within a usercontrol that's being used outside it's home project are
I'm working on a multi-site CMS that has a notion of cross-publication among sites.
There are a lot of sites out there that teach people how to build
My understanding of a factory is that it encapsulates instantiation of concrete classes that
I've heard it said that it is difficult to design for inheritance, but I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.