Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- int fnextc(FILE* in) {
- int ch = fgetc(in);
- ungetc(ch, in);
- return ch;
- }
- void checkFile(FILE* in, FILE* out) {
- if (in == NULL) {
- fputs("File is not found", out);
- printf("File is not found");
- fclose(out);
- exit(1);
- }
- if (fnextc(in) == EOF) {
- fputs("File is empty", out);
- printf("File is empty");
- fclose(in);
- fclose(out);
- exit(2);
- }
- }
- int main() {
- const char *freadname = "../input.txt";
- const char *fwritename = "../output.bin";
- const int L = 256;
- FILE* in = fopen(freadname, "rt");
- FILE* out = fopen(fwritename, "wb");
- checkFile(in, out);
- char string[L];
- int n = 0;
- int q = fgets(string, L, in);
- while (q != 0) {
- while (string[n] != '\0' && string[n] != '\n') {
- n++;
- }
- fwrite(&n, sizeof(int), 1, out);
- fwrite(&string, sizeof(char), n, out);
- n = 0;
- q = fgets(string, L, in);
- }
- fclose(in);
- fclose(out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement