☠️
smadi0x86 Playground
  • 💀Welcome to smadi0x86 Playground
    • 🍷Resources
    • 🚬Projects
    • 🎓Certifications
    • 📌Pinned
    • ❓Questions
    • 📞Contact
  • 🏞️Cloud Native
    • Docker
      • Quick Reference
      • Introduction
      • Containers
      • Images
      • Storage & Volumes
      • Security
      • Cheatsheet
    • Git
    • Serverless Framework
    • YAML
  • 🔨Software Engineering
    • System Design
    • Environment Variables
    • JSON Web Tokens
  • 👾Architecture
    • C Language
      • Introduction
      • Calling Conventions
      • GCC Compilation
      • Libraries & Linking
      • I/O
      • Files
      • Pointers
      • Dynamic Memory Allocation
      • Data Types
      • Strings Manipulation
      • Bit Manipulation
      • Pre-processors
      • Macros
      • Type Qualifiers
    • C/C++ Build Systems
      • Fundamentals for Linking
      • Symbolic Linking
      • Cross-Platform Compilation
      • CMake for Building and Linking
      • Shared Libraries
      • Dynamic Linking and Dependency Management
    • Operating Systems
      • OS & Architecture
      • Processes
      • CPU Scheduling
      • Memory Management
  • 🛩️Cyber Warfare
    • Flight Physics
    • Communication
      • PWM & PPM
      • MAVLink
  • 🏴‍☠️Offensive Security
    • Active Directory
      • Introduction
    • Web Attacks
      • Server Side
        • OS Command Injection
        • Information Disclosure
        • Directory Traversal
        • Business Logic
        • Authentication
        • File Upload
        • SSRF
      • Client Side
        • CSRF
        • XSS
    • Recon
      • Active
        • Host discovery
        • Nmap
        • Mass Scan
      • Passive
        • Metadata
      • Web Applications
        • Discovery
        • Subdomains & Directories
        • SSL Certs
        • CMS
        • WAF Detection
      • Firewall Evasion
  • Binary Exploitation
    • Stack Smashing
      • x86
      • x86_64
    • pwntools
      • Processes and Communication
      • Logging and Context
      • Cyclic
      • Packing
      • ELF
      • ROP
  • 😈Advanced Persistent Threat
    • C2
      • Sliver
    • Malware
      • Windows Internals
        • PEB
      • Academy
        • Basics
      • Sektor7
        • Essentials
  • 💌Certifications
    • AWS Certified Cloud Practitioner (CLF-C01)
      • Cloud Foundations
      • Domain 1: Cloud Concepts
      • Domain 2: Security and Compliance
      • Domain 3: Technology
      • Domain 4: Billing and Pricing
    • AWS Certified Solutions Architect - Associate (SAA-C03)
      • Foundation
    • Certified Kubernetes Administrator (CKA)
      • Core Concepts
      • Scheduling
      • Logging & Monitoring
      • Application Lifecycle Management
      • Cluster Maintenance
      • Security
      • Storage
      • Networking
      • Design Kubernetes Cluster
      • Kubernetes The Kubeadm Way
      • Troubleshooting
      • JSONPATH
      • Lightning Lab
      • Mock Exams
      • Killer Shell
    • Certified Kubernetes Security (CKS)
      • Foundation
      • Cluster Setup
      • Cluster Hardening
      • Supply Chain Security
      • Runtime Security
      • System Hardening
      • Killer Shell
    • (KGAC-101) Kong Gateway Foundations
      • Introduction to APIs and API Management
      • Introduction to Kong Gateway
      • Getting Started with Kong Enterprise
      • Getting Started with Kong Konnect
      • Introduction to Kong Plugins
  • 📜Blog Posts
    • Modern Solutions For Preventing Ransomware Attacks
Powered by GitBook
On this page
  • Character Functions for Input
  • Examples
  • Character Functions for Output
  • Example
  • Example
  • String Functions for Input
  • Example
  • Formatting Functions
  1. Architecture
  2. C Language

I/O

This guide discusses the C functions used for reading and writing characters and strings, with examples of their usage.

Character Functions for Input

getc()Function:

  • Purpose: Reads a single character from a file.

  • Syntax: int getc(FILE *stream);

Examples

Reading from a file:

#include <stdio.h>
int main() {
    char ch = '\0';
    FILE *fp;
    if ((fp = fopen("test","r")) != NULL) {
        while ((ch = getc(fp)) != EOF) {
            printf("%c",ch);
        }
        fclose(fp);
    }
    return 0;
}

Reading from stdin (i.e., keyboard):

int main() {
    char ch = getc(stdin);
    printf(">>> %c\n", ch);
    return 0;
}

Alternative methods:

int main() {
    int ch;
    while ((ch = getchar()) != EOF) printf("%c\n", ch);
    return 0;
}

With spaces captured:

int main() {
    int ch;
    while (isspace(ch = (char)getchar()));
    printf("%c\n", ch);
    return 0;
}

ungetc()Function:

  • Purpose: Pushes a character back into the stream.

  • Syntax: int ungetc(int char, FILE *stream);

Example:

#include <stdio.h>
int main() {
    FILE *fp;
    int c;
    char buffer[256];
    fp = fopen("test", "r");
    while (!feof(fp)) {
        c = getc(fp);
        if (c == '!') ungetc('+', fp);
        else ungetc(c, fp);
        fgets(buffer, 255, fp);
        fputs(buffer, stdout);
    }
    return 0;
}

Character Functions for Output

putc()Function:

  • Purpose: Writes a single character to a file or stdout.

  • Syntax: int putc(int char, FILE *fp);

  • Usage: putc('\n', stdout);

Example

Redirecting input to a file:

int main() {
    int ch;
    while ((ch = getchar()) != EOF) putchar(ch);
    ungetc(ch, stdin);
    printf("EOF signal detected!\n");
    return 0;
}

Use the program like:

./main < infile

fputc()Function:

  • Purpose: Writes a character to the specified stream.

  • Syntax: int fputc(int character, FILE *stream);

Example

Writing characters a-z to a file:

int main() {
    FILE *pfile = NULL;
    char c;
    pfile = fopen("test", "w");
    for (c = 'A'; c <= 'Z'; c++) fputc(c, pfile);
    fclose(pfile);
    return 0;
}

String Functions for Input

getline()Function:

  • Purpose: Reads a line from the specified stream.

  • Syntax: ssize_t getline(char **buffer, size_t *size, FILE *stream);

Example

Reading a line with getline():

#include <stdio.h>
#include <stdlib.h>
int main() {
    char *buffer = NULL;
    size_t bufsize = 32;
    size_t characters;
    buffer = malloc(bufsize * sizeof(char));
    if (buffer == NULL) exit(1);
    printf("Type something: ");
    characters = getline(&buffer, &bufsize, stdin);
    printf("%zu characters were read.\n", characters);
    printf("You typed: %s\n", buffer);
    return 0;
}

fscanf()Function:

  • Purpose: Reads formatted input from a file.

  • Syntax: int fscanf(FILE *fp, const char *format, ...);

Example:

#include <stdio.h>
int main() {
    FILE *fp;
    char buff[255];
    fp = fopen("test", "r");
    while (fscanf(fp, "%s", buff) != EOF) {
        printf("%s ", buff);
    }
    fclose(fp);
    return 0;
}

Formatting Functions

sprintf()Function:

  • Purpose: Writes formatted output to a string.

  • Syntax: int sprintf(char *str, const char *format, ...);

Example:

#include <stdio.h>
int main() {
    char buffer[50];
    int a = 10, b = 20;
    sprintf(buffer, "%d plus %d is %d", a, b, a+b);
    printf("%s", buffer);
    return 0;
}
PreviousLibraries & LinkingNextFiles

Last updated 8 months ago

👾