# stdio

***

**1. Reading and Writing to Standard Input/Output**

```cpp
#include <stdio.h>

int main() {
  printf("Hello, world!\n");
  int x;
  scanf("%d", &x);
  printf("You entered: %d\n", x);
  return 0;
}
```

**2. File Input/Output**

```cpp
#include <stdio.h>

int main() {
  FILE *fp = fopen("data.txt", "r");
  if (fp == NULL) {
    perror("Error opening file");
    return 1;
  }

  char line[100];
  while (fgets(line, sizeof(line), fp)) {
    printf("%s", line);
  }

  fclose(fp);
  return 0;
}
```

**3. Formatted Printing**

```cpp
#include <stdio.h>

int main() {
  int x = 10;
  double y = 3.14;
  printf("x = %d, y = %.2f\n", x, y);
  return 0;
}
```

**4. Error Handling**

```cpp
#include <stdio.h>
#include <errno.h>

int main() {
  FILE *fp = fopen("data.txt", "w");
  if (fp == NULL) {
    fprintf(stderr, "Error opening file: %s\n", strerror(errno));
    return 1;
  }

  fclose(fp);
  return 0;
}
```

**5. Binary Input/Output**

```cpp
#include <stdio.h>

int main() {
  FILE *fp = fopen("data.bin", "wb");
  if (fp == NULL) {
    perror("Error opening file");
    return 1;
  }

  int x = 10;
  fwrite(&x, sizeof(int), 1, fp);

  fclose(fp);
  return 0;
}
```

**6. Reading and Writing to a Pipe**

```cpp
#include <stdio.h>
#include <unistd.h>

int main() {
  int pipefd[2];

  if (pipe(pipefd) == -1) {
    perror("Error creating pipe");
    return 1;
  }

  int pid = fork();

  if (pid == 0) {
    // Child process

    close(pipefd[0]);
    dup2(pipefd[1], STDOUT_FILENO);

    printf("Hello from child process!\n");

    close(pipefd[1]);
    exit(0);
  } else {
    // Parent process

    close(pipefd[1]);
    char buf[100];

    while (read(pipefd[0], buf, sizeof(buf)) > 0) {
      printf("Received from child: %s\n", buf);
    }

    close(pipefd[0]);
    waitpid(pid, NULL, 0);
  }

  return 0;
}
```

**7. Redirecting Standard Input/Output**

```cpp
#include <stdio.h>
#include <unistd.h>

int main() {
  dup2(open("data.txt", O_RDONLY), STDIN_FILENO);
  dup2(open("output.txt", O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);

  int x;
  scanf("%d", &x);
  printf("You entered: %d\n", x);

  return 0;
}
```

**8. String Formatting**

```cpp
#include <stdio.h>
#include <stdarg.h>

int main() {
  char buf[100];
  snprintf(buf, sizeof(buf), "Hello, %s!\n", "John");
  printf("%s", buf);

  return 0;
}
```

**9. Error Logging**

```cpp
#include <stdio.h>
#include <stdlib.h>

int main() {
  FILE *fp = fopen("error.log", "a");
  if (fp == NULL) {
    exit(1);
  }

  fprintf(fp, "Error occurred: %s\n", strerror(errno));

  fclose(fp);
  return 0;
}
```

**10. Debugging**

```cpp
#include <stdio.h>
#include <assert.h>

int main() {
  int x = 10;
  assert(x == 10);
  return 0;
}
```

**11. Reading from a Keyboard**

```cpp
#include <stdio.h>

int main() {
  char c;

  while ((c = getchar()) != EOF) {
    putchar(c);
  }

  return 0;
}
```

**12. Parsing Command-Line Arguments**

```cpp
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
  for (int i = 0; i < argc; i++) {
    printf("%s\n", argv[i]);
  }

  return 0;
}
```

**13. Reading from a Socket**

```cpp
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("Error creating socket");
    return 1;
  }

  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = INADDR_ANY;
  addr.sin_port = htons(8080);

  if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("Error binding socket");
    return 1;
  }

  if (listen(sockfd, 5) < 0) {
    perror("Error listening on socket");
    return 1;
  }

  while (1) {
    int newsockfd = accept(sockfd, NULL, NULL);
    if (newsockfd < 0) {
      perror("Error accepting connection");
      continue;
    }

    char buf[100];
    int n = read(newsockfd, buf, sizeof(buf));
    if (n < 0) {
      perror("Error reading from socket");
      continue;
    }

    write(newsockfd, buf, n);

    close(newsockfd);
  }

  close(sockfd);
  return 0;
}
```

**14. Writing to a Socket**

```cpp
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("Error creating socket");
    return 1;
  }

  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = INADDR_ANY;
  addr.sin_port = htons(8080);

  if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("Error connecting to socket");
    return 1;
  }

  const char *msg = "Hello from client!\n";
  int n = write(sockfd, msg, strlen(msg));
  if (n < 0) {
    perror("Error writing to socket");
    return 1;
  }

  close(sockfd);
  return 0;
}
```

**15. Reading and Writing to a File Descriptor**

```cpp
#include <stdio.h>
#include <unistd.h>

int main() {
  int fd = open("data.txt", O_RDWR);
  if (fd < 0) {
    perror("Error opening file");
    return 1;
  }

```
