50% developed

Applied Programming/Files

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Overview[edit | edit source]

Computer Files[edit | edit source]

What is it?

When you use your computer to create things, those things are stored in units of information called files. A computer file can be a document you write with your word processor. A computer file can also be a graphical image from a digital camera or an image you create with a digital paintbrush, a piece of music, a video, or just about anything. Whatever it is, the computer stores that information as a file.[1]

File sizes

Since data storage on computers is still limited, file sizes still matter. File sizes are always measured in bytes. A byte is a sequence of 8 bits (and remember, a bit is the smallest piece of digital information, 000 or 111). A single byte is enough bits to represent 256 numbers, because 28 = 2562. That also means a byte is big enough to represent a single letter in the ASCII encoding standard. Larger files are referred to in larger byte sizes. Terms include kilobyte (1000 bytes), megabyte (10002 bytes), gigabyte (10003 bytes), terabyte (10004 bytes), petabyte (10005 bytes).[2]

File types

Computers store all files as binary data, long strings of 111s and 000s. Files represent all different types of data, however—like images, videos, documents, text files, and spreadsheets. Even applications are files. Each file has a type/kind/format, which is often reflected in its file extension.[3] Each individual file in Windows (and most other operating systems) will also have a file attribute which sets a condition to the specific file. For example, you can't write new information to a file that has the read-only attribute turned on. A filename is just the name that a user or program titles the file to help identify what it is. An image file may be named something like kids-lake-2017.jpg. The name itself doesn't affect the contents of the file, so even if a video file is named something like image.mp4, it doesn't mean it's suddenly a picture file. Files in any operating system are stored on hard drives, optical drives, and other storage devices. The specific way a file is stored and organized is referred to as a file system, which starts with the root directory and then continues to countless subdirectories or folders.[4]

File operations

Include:[5]

  • Create a new file
  • Change the access permissions and attributes of a file
  • Open a file, which makes the file contents available to the program
  • Read data from a file
  • Write data to a file
  • Delete a file
  • Close a file, terminating the association between it and the program
  • Truncate a file, shortening it to a specified size within the file system without rewriting any content
  • Updating contents to an existing file
  • Searching data on a file

File Systems[edit | edit source]

In computing, a file system or filesystem (often abbreviated to fs) controls how data is stored and retrieved. Without a file system, data placed in a storage medium would be one large body of data with no way to tell where one piece of data stops and the next begins. By separating the data into pieces and giving each piece a name, the data is easily isolated and identified. Taking its name from the way paper-based data management system is named, each group of data is called a "file." The structure and logic rules used to manage the groups of data and their names is called a "file system."[6]

A file system functions in much the same way a book's table of contents does. It is a way for the computer to refer to the location of data on the storage device. Where a book may refer to a chapter and range of pages, a file system does the same with directories and folders. A filename consists of a sequence of characters followed by an extension type, such as myfile.txt. If we were to continue the book analogy, a filename would be the title of the chapter, and the directory would be the chapter number or page range. Directories can structured so that they are flat/linear or hierarchical; meaning they can be either one long list of files, or stored within many different folders and sub-folders.

Common file systems include FAT32 and NTFS on Windows platforms and APFS on macOS.

Directories[edit | edit source]

Directory Structures[edit | edit source]

Text Files[edit | edit source]

What are they?[7][edit | edit source]

A text file (sometimes spelled textfile; an old alternative name is flatfile) is a kind of computer file that is structured as a sequence of lines of electronic text. A text file exists stored as data within a computer file system. In operating systems such as CP/M and MS-DOS, where the operating system does not keep track of the file size in bytes, the end of a text file is denoted by placing one or more special characters, known as an end-of-file marker, as padding after the last line in a text file. On modern operating systems such as Microsoft Windows and Unix-like systems, text files do not contain any special EOF character, because file systems on those operating systems keep track of the file size in bytes. There are for most text files a need to have end-of-line delimiters, which are done in a few different ways depending on operating system. Some operating systems with record-orientated file systems may not use new line delimiters and will primarily store text files with lines separated as fixed or variable length records.

Binary Files[edit | edit source]

What are they?[8][edit | edit source]

A binary file is a computer file that is not a text file. The term "binary file" is often used as a term meaning "non-text file". Many binary file formats contain parts that can be interpreted as text; for example, some computer document files containing formatted text, such as older Microsoft Word document files, contain the text of the document but also contain formatting information in binary form.

Binary files contain formatting information that only certain applications or processors can understand. While humans can read text files, binary files must be run on the appropriate software or processor before humans can read them. For example, only Microsoft Word and certain other word processing programs can interpret the formatting information in a Word document. Executable files, compiled programs, SAS and SPSS system files, spreadsheets, compressed files, and graphic (image) files are all examples of binary files.[9]

Reading and Writing to Files[edit | edit source]

Reading and Writing to files in C#[edit | edit source]

The StreamReader and StreamWriter tools are used in C# for reading and writing to files. The declaration “using System.IO;” must be included in the heading of the program for the classes to be accessible, and the StreamReader or StreamWriter object is created with the keyword “new”. For example, the below code will use a StreamReader object that we’ll name sr to read and display a file called “filename.txt”:

    using System;
    using System.IO;
    
    public class MyProgram
    {
        public static void Main(string[] Args)
        {
            StreamReader sr = new StreamReader(filename.txt);
            /*the StreamReader object takes different commands to read or write to the file.*/
            while(line = sr.ReadLine() != null)
                Console.WriteLine(line);
        }
    }

Reading and Writing to files in Python[edit | edit source]

The same program can be performed in Python with the following code.

    file = open("filename.txt")
    text = file.readlines()
    for line in text:
        print(line)
    file.close()

Activities[edit | edit source]

Key Terms[edit | edit source]

Absolute Path - Contains the root element and the complete directory list required to locate the file.[10]

Binary File - A computer file that is not a text file. The term "binary file" is often used as a term meaning "non-text file".[11]

Directory structure - The way an operating system's file system and its files are displayed to the user.[12]

Directory - A location for storing files on your computer. Directories are found in a hierarchical file system, such as Linux, MS-DOS, OS/2, and Unix.[13]

File System - Alternatively referred to as file management or FS, a file system is a method of organizing and retrieving files from a storage medium.[14]

File Utilities - Allow users to create, list, copy, move and delete files, and alter metadata.[15]

Fully-Qualified File Name - A string that uniquely identifies a file stored on the computer by including the path, name, and extension of the file.[12]

Metadata - Information that is typically associated with each file within a file system. File systems might store the file creation time, the time it was last accessed, etc.[15]

Parent and Children - A parent directory houses "children" files or subdirectories. A child is a file or subdirectory housed in a parent directory.[16]

Path - Complete location or name of where a computer, file, device, or web page is located.[17]

Relative Path - Needs to be combined with another path in order to access a file.[10]

Root - The highest-level directory in the file system hierarchy, found in UNIX-like operating systems.[18]

Text File - A file that is structured as a sequence of lines of electronic text, it exists stored as data within a computer file system.[19]

References[edit | edit source]