# stdio

***

**1. Read a Character from Standard Input**

```c
#include <stdio.h>

int main() {
  char ch;
  ch = getchar();
  printf("%c", ch);
  return 0;
}
```

**2. Write a Character to Standard Output**

```c
#include <stdio.h>

int main() {
  char ch = 'a';
  putchar(ch);
  return 0;
}
```

**3. Read a String from Standard Input**

```c
#include <stdio.h>

int main() {
  char str[50];
  fgets(str, 50, stdin);
  printf("%s", str);
  return 0;
}
```

**4. Write a String to Standard Output**

```c
#include <stdio.h>

int main() {
  char str[] = "Hello World!";
  fputs(str, stdout);
  return 0;
}
```

**5. Read an Integer from Standard Input**

```c
#include <stdio.h>

int main() {
  int num;
  scanf("%d", &num);
  printf("%d", num);
  return 0;
}
```

**6. Write an Integer to Standard Output**

```c
#include <stdio.h>

int main() {
  int num = 123;
  printf("%d", num);
  return 0;
}
```

**7. Read a Floating-Point Number from Standard Input**

```c
#include <stdio.h>

int main() {
  float num;
  scanf("%f", &num);
  printf("%f", num);
  return 0;
}
```

**8. Write a Floating-Point Number to Standard Output**

```c
#include <stdio.h>

int main() {
  float num = 123.45;
  printf("%f", num);
  return 0;
}
```

**9. Read a Character Array from Standard Input**

```c
#include <stdio.h>

int main() {
  char arr[50];
  scanf("%s", arr);
  printf("%s", arr);
  return 0;
}
```

**10. Write a Character Array to Standard Output**

```c
#include <stdio.h>

int main() {
  char arr[] = "Hello World!";
  printf("%s", arr);
  return 0;
}
```

**11. Print a Formatted String**

```c
#include <stdio.h>

int main() {
  printf("%s %d %f\n", "Hello", 123, 123.45);
  return 0;
}
```

**12. Scan a Formatted String**

```c
#include <stdio.h>

int main() {
  char s[50];
  int i;
  float f;
  scanf("%s %d %f", s, &i, &f);
  return 0;
}
```

**13. Read a Line from Standard Input**

```c
#include <stdio.h>

int main() {
  char line[50];
  gets(line);
  printf("%s", line);
  return 0;
}
```

**14. Write a Line to Standard Output**

```c
#include <stdio.h>

int main() {
  char line[] = "Hello World!";
  puts(line);
  return 0;
}
```

**15. Redirect Standard Input**

```c
#include <stdio.h>

int main() {
  freopen("file.txt", "r", stdin);
  char line[50];
  gets(line);
  printf("%s", line);
  return 0;
}
```

**16. Redirect Standard Output**

```c
#include <stdio.h>

int main() {
  freopen("file.txt", "w", stdout);
  printf("Hello World!");
  return 0;
}
```

**17. Check for End of File**

```c
#include <stdio.h>

int main() {
  if (feof(stdin)) {
    printf("End of file reached.");
  }
  return 0;
}
```

**18. Check for File Error**

```c
#include <stdio.h>

int main() {
  if (ferror(stdin)) {
    printf("File error occurred.");
  }
  return 0;
}
```

**19. Clear File Error Indicator**

```c
#include <stdio.h>

int main() {
  clearerr(stdin);
  return 0;
}
```

**20. Flush Standard Output**

```c
#include <stdio.h>

int main() {
  fflush(stdout);
  return 0;
}
```

**21. Get File Size**

```c
#include <stdio.h>
#include <sys/stat.h>

int main() {
  struct stat buf;
  stat("file.txt", &buf);
  printf("%d", buf.st_size);
  return 0;
}
```

**22. Copy File**

```c
#include <stdio.h>

int main() {
  FILE *f1, *f2;
  char ch;
  f1 = fopen("file1.txt", "r");
  f2 = fopen("file2.txt", "w");
  while ((ch = getc(f1)) != EOF) {
    putc(ch, f2);
  }
  fclose(f1);
  fclose(f2);
  return 0;
}
```

**23. Append to File**

```c
#include <stdio.h>

int main() {
  FILE *f;
  char ch;
  f = fopen("file.txt", "a");
  putc('a', f);
  fclose(f);
  return 0;
}
```

**24. Read from Pipe**

```c
#include <stdio.h>

int main() {
  FILE *stream;
  char buffer[256];
  stream = popen("ls", "r");
  while (fgets(buffer, 256, stream) != NULL) {
    printf("%s", buffer);
  }
  pclose(stream);
  return 0;
}
```

**25. Write to Pipe**

```c
#include <stdio.h>

int main() {
  FILE *stream;
  stream = popen("wc", "w");
  fprintf(stream, "Hello World!");
  pclose(stream);
  return 0;
}
```

**26. Get Current File Position**

```c
#include <stdio.h>

int main() {
  FILE *f;
  f = fopen("file.txt", "r");
  printf("%ld", ftell(f));
  fclose(f);
  return 0;
}
```

**27. Set Current File Position**

```c
#include <stdio.h>

int main() {
  FILE *f;
  f = fopen("file.txt", "r");
  fseek(f, 10, SEEK_SET);
  fclose(f);
  return 0;
}
```

**28. Rewind File Position**

```c
#include <stdio.h>

int main() {
  FILE *f;
  f = fopen("file.txt", "r");
  rewind(f);
  fclose(f);
  return 0;
}
```

**29. Create Temporary File**

```c
#include <stdio.h>

int main() {
  FILE *f;
  f = tmpfile();
  fwrite("Hello World!", 12, 1, f);
  fclose(f);
  return 0;
}
```

**30. Remove Temporary File**

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

int main() {
  char *tempFileName;
  tempFileName = tmpnam(NULL);
  remove(tempFileName);
  return 0;
}
```

**31. Open File in Binary Mode**

```c
#include <stdio.h>

int main() {
  FILE *f;
  f = fopen("file.bin", "rb");
  fclose(f);
  return 0;
}
```

**32. Open File in Text Mode**

```c
#include <stdio.h>

int main() {
  FILE *f;
  f = fopen("file.txt", "rt");
  fclose(f);
  return 0;
}
```

**33. Check if File Exists**

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

int main() {
  FILE *f;
  if ((f = fopen("file.txt", "r")) == NULL) {
    printf("File does not exist.");
  } else {
    fclose(f);
  }
  return 0;
}
```

**34. Rename File**

```c
#include <stdio.h>

int main() {
  rename("file1.txt", "file2.txt");
  return 0;
}
```

**35. Delete File**

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

int main() {
  remove("file.txt");
  return 0;
}
```

**36. Create Directory**

```c
#include <stdio.h>
#include <sys/stat.h>

int main() {
  mkdir("mydir", 0777);
  return 0;
}
```

**37. Remove Directory**

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

int main() {
  rmdir("mydir");
  return 0;
}
```

**38. Change Directory**

```c
#include <stdio.h>

int main() {
  chdir("mydir");
  return 0;
}
```

**39. Get Current Directory**

```c
#include <stdio.h>

int main() {
  char *cwd;
  cwd = getcwd(NULL, 0);
  printf("%s", cwd);
  free(cwd);
  return 0;
}
```

**40. List Files in Directory**

```c
#include <stdio.h>
#include <dirent.h>

int main() {
  DIR *d;
  struct dirent *dir;
  d = opendir(".");
  while ((dir = readdir(d)) != NULL) {
    printf("%s\n", dir->d_name);
  }
  closedir(d);
  return 0;
}
```

**41. Check if File is a Directory**

```c
#include <stdio.h>
#include <sys/stat.h>

int main() {
  struct stat buf;
  stat("mydir", &buf);
  if (S_ISDIR(buf.st_mode)) {
    printf("mydir is a directory.");
  } else {
    printf("mydir is not a directory.");
  }
  return 0;
}
```

**42. Check if File is a Regular File**

```c
#include <stdio.h>
#include <sys/stat.h>

int main() {
  struct stat buf;
  stat("file.txt", &buf);
  if (S_ISREG(buf.st_mode)) {
    printf("file.txt is a regular file.");
  } else {
    printf("file.txt is not a regular file.")

```
