Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int fnextc(FILE* in) {
  5. int ch = fgetc(in);
  6. ungetc(ch, in);
  7. return ch;
  8. }
  9.  
  10. void checkFile(FILE* in, FILE* out) {
  11. if (in == NULL) {
  12. fputs("File is not found", out);
  13. printf("File is not found");
  14. fclose(out);
  15. exit(1);
  16. }
  17.  
  18. if (fnextc(in) == EOF) {
  19. fputs("File is empty", out);
  20. printf("File is empty");
  21. fclose(in);
  22. fclose(out);
  23. exit(2);
  24. }
  25. }
  26.  
  27. int main() {
  28. const char *freadname = "../input.txt";
  29. const char *fwritename = "../output.bin";
  30. const int L = 256;
  31.  
  32. FILE* in = fopen(freadname, "rt");
  33. FILE* out = fopen(fwritename, "wb");
  34.  
  35. checkFile(in, out);
  36.  
  37. char string[L];
  38.  
  39. int n = 0;
  40. int q = fgets(string, L, in);
  41. while (q != 0) {
  42. while (string[n] != '\0' && string[n] != '\n') {
  43. n++;
  44. }
  45. fwrite(&n, sizeof(int), 1, out);
  46. fwrite(&string, sizeof(char), n, out);
  47. n = 0;
  48. q = fgets(string, L, in);
  49. }
  50.  
  51. fclose(in);
  52. fclose(out);
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement