Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Wednesday, March 19, 2025

Working with Files in the C Programming Language, Practical Examples and Tips for Efficient Programming

Working with files in the C programming language represents a key functionality that enables programmers to write, read, and manipulate data that is permanently stored on disk. This capability is essential for developing applications that require data storage between program executions, such as databases, configuration files, or logs. In the C programming language, file operations are achieved using the standard <stdio.h> library, which provides functions for opening, reading, writing, and closing files. In the C programming language, files are treated as data streams. There are two basic types of files:

  • Text files – data is stored as a sequence of characters, often human-readable, such as .txt files.
  • Binary files – data is stored in binary format, which is more efficient for machine processing, such as images, executable files, etc.

The FILE data type, defined in <stdio.h>, is used for working with files. This type is actually a pointer to a structure that contains information about the file, such as its location, current pointer position, and operating mode.

Working with files is straightforward in C programming

Working with files is straightforward in C programming

The C standard library provides several functions for working with files, including:

fopen() - Opens a file. 

fclose() - Closes a file.

fprintf() - Writes formatted text to a file.

fscanf() - Reads formatted text from a file.

fputc() - Writes a single character to a file.

fgetc() - Reads a single character from a file.

fwrite() - Writes a block of bytes to a file.

fread() - Reads a block of bytes from a file.

Working with files in the C programming language provides a powerful tool for data management, but requires attention to resource handling and error checking. Through the standard library, programmers can efficiently implement functionalities for both text and binary files, with flexibility for various types of applications. With basic operations such as opening, reading, writing, and closing, the C programming language also enables advanced techniques such as positioning and binary data processing, making it suitable for a wide range of tasks. Understanding basic operations such as opening, reading, writing, and closing files is crucial for developing applications that handle large amounts of data. First, we will focus on and examine C code for working with text files.

Reading and Writing Text Files in C Programming

Thursday, November 28, 2024

Introduction to Working with Text Files in PHP, Reading and Writing

Working with text files in PHP is a fundamental yet crucial skill for any PHP developer. Files allow for the storage, reading, and management of data outside of an application's memory, which is essential for many applications, such as logging, storing configurations, or managing user data. This blog post provides a detailed introduction to the basics of working with text files in PHP, including reading, writing, and some advanced operations. Although databases are the predominant storage medium for data today, text files still hold their place in many applications. To proficiently work with text files in PHP, a solid grasp of fundamental concepts is essential. These include understanding file paths, which can be either relative or absolute, and file modes, which dictate whether you're reading, writing, or performing both actions on a file.

  • r - Read only. - Opens a file for reading only. Any attempt to write to the file will fail.
  • w - Write only. - Opens a file for writing only. If the file exists, it will be truncated (empty). If it doesn't exist, a new file will be created.
  • a - Append. - Opens a file for writing. Any data written will be placed at the end of the file. This is useful for adding new data to an existing file without overwriting the existing content.
  • r+ - Read and write. - Opens a file for both reading and writing. The file pointer will be positioned at the beginning of the file.

Programmers are pleasantly surprised by PHP's functions for reading and writing to text files

Programmers are pleasantly surprised by PHP's functions for reading and writing to text files

PHP offers several methods for reading content from text files. Some of the most commonly used methods include using the file_get_contents function which allows reading the entire file content in one go, using the fopen and fread functions which provide more control, especially when working with large files, and using the file function which loads the file as an array where each element is a line. Writing data to text files is a fundamental aspect of PHP programming. The file_put_contents function provides a straightforward way to write to a file, overwriting any existing content. For more granular control over the writing process, the fopen and fwrite functions offer greater flexibility. When appending data to an existing file, the FILE_APPEND mode should be used with file_put_contents.

It's essential to consider security when working with files, implementing measures such as file locking and input validation to prevent potential vulnerabilities. By mastering these concepts, you can effectively leverage file operations to manage data in your PHP applications. Working with text files in PHP is simple and efficient thanks to the built-in functions that the language provides. Whether you need to read data from a file, write logs, or even work with CSV format, PHP offers robust tools for these tasks. We hope our practical examples and tips will help you better understand and utilize the capabilities that PHP provides for working with text files. Let's move on to the practical part.

Efficient Management for Adding, Deleting, and Saving Items in an HTML List

Thursday, July 21, 2016

Rad sa drajverima, folderima i fajlovima u C# programskom jeziku


Bez obzira da li radite sa drajverima, folderima ili fajlovima; retko će te biti u situaciji da radite samo sa folderima ili samo sa fajlovima. U većini slučajeva gde su vam potrebni fajlovi, tu su vam potrebni i drajveri i folderi. Naravno kad kažem drajveri ne mislim na drajvere datoteke za hardverske uređaje u vašim računarima, nego drajvere zovemo i alfabete koji vidite kao root node u direktorijumu kad pokrenete File Explorer ili Command Prompt. Srećom i zahvaljujući Microsoft programerima; .Net Framework vam svojim klasama itekako olakšava rad sa istim, s time da vi trebate da poznajete određene klase i metode. Jednostavno treba da poznajete rad sa klasama poput Environment, DriveInfo, Directory, DirectoryInfo, File i FileInfo. Poznavanjem ovih par klasa niste ni svesni koliki programerski posao će one obavljati za vas.


( Klase koje morate znati; Environment, DriveInfo, Directory, DirectoryInfo, File i FileInfo )  

Do sada ste verovatno primetili da smo klasu okruženja Environment već koristili skoro u svim C# primerima na ovom blogu; jednostavno smo umesto eskejp sekvence \n za prelazak u novi red koristili Environment.NewLine. Neki od mojih kolega smatraju da sam to radio radi čitljivosti koda i nije im jasno zašto ne koristim eskejp sekvence \n jer skraćuje kod; često se koristi u školama i u knjigama za C# programiranje. Istina je da uvek trebate voditi računa i o čitljivosti koda, mada treba da znate i da se prelazak u novi red na različitim operativnim sistemima ne kompajlira isto. Npr. u Windows-u je to "\r\n" (\u000D\u000A) , na iOS operativnom sistemu je to "\r" (\u000D) dok je na Unix sistemu "\n" (\u000A). Većini programera; pogotovo onih koji koriste samo Windows operativni sistem to nije ni toliko bitno, ali iz tog razloga postoji preporuka Microsoft-a da se uvek koristi Environment.NewLine umesto eskejp sekvence \n kad god je to moguće i ja jednostavno poštujem preporuku. Međutim, klasa okruženja Environment ne služi samo za tu svrhu, već takođe obezbeđuje i informacije o platformi i okruženju na kom radite. Pogledajte samo neke od propertije i metode klase Environment.
  • CurrentDirectory – properti koji omogućava set-ovanje i čitanje tekućeg direktorijuma 
  • Is64BitOperatingSistem – properti koji vraća vrednost true ukoliko koristite 64bit-ni operativni sistem
  • MachineName – properti koji prikazuje NetBIOS ime lokalnog kompjutera ili ne iz bezbednosnih razloga
  • NewLine – properti koji se koristi za prelazak u novi red
  • OSVersion – properti koji prikazuje indikator za tekući OS i verziju
  • SystemDirectory – properti koji prikazuje putanju sistemskog direktorijuma
  • GetLogicalDrives – metoda prikazuje niz imena drajvera u tekućem kompjuteru
  • Itd.
Za razliku od klase Environment, klasa Directory ne sadrži uopšte propertije, samo korisne metode za rad sa direktorijuma poput:
  • CreateDirectory – kreira direktorijume i pod direktorijume ukoliko već ne postoje
  • Delete – briše prazan direktorijum
  • Exists – proverava da li postoji navedeni direktorijum
  • GetCurrentDirectory – metoda koja vraća string tekućeg direktorijuma
  • GetDirectories – metoda koja vraća pod direktorijume u direktorijumu prema zadatom obrascu
  • Itd.
Kako se koriste navedene klase najbolje će te shvatiti njihovom praktičnom primenom. U svakom slučaju da bi ste koristili mogućnosti navedeni klasa predlažem vam da prvo uključite imenski prostor System.IO u vašem projektu.

Praktičan primer rada sa drajverima i folderima

Friday, November 06, 2015

Rad sa tekstualnim i binarnim datotekama u C++ programskom jeziku


Najvažnija stvar svake aplikacije je da negde čuva informacije i da koristi iste po potrebi. Inače rezultati vaše aplikacije bi trajali samo dok radi program; čim bi ste ga pokrenuli ponovo, ne bi ste mogli videti informacije koje ste pre unosili. Čak i najbanalnija igrica mora negde da pamti rezultate igrača, međutim RAM memorija nije postojana i ona traje dok se računar ne ugasi. Zato se podaci čuvaju u nekom fajlu ili u bazi podataka. Kad C++ programeri pričaju o tokovima ili drugačije rečeno; streams – strimovima; to znači da pričaju o nečemu gde mogu da se učitaju ili upisuju podaci. Strimovi u suštini obezbeđuju jednostavan rad i sa unosom i sa čitanjem podataka u tekstualni ili binarni fajl. Prvo je potrebno da omogućite podršku operacijama fajl tokova direktivom #include <fstream> koja vam omogućava korišćenje objekata ifstrem i ofstream koji regulišu otvaranje i zatvaranje datoteka.

 

( Input and Output Stream )
 

Zahvaljujući <fstream> koji inače uključuje iostream objekte koji održavaju flagove i obaveštavaju o stanju ulaza i izlaza, vama je omogućeno da koristite logičke funkcije poput eof(), bad(), fail() ili good() i sa takvim funkcijama rad sa bilo kojim tekstualnim fajlom je jednostavan.

Kako da upisujem i učitavam podatke u tekstualnu datoteku?