How can I fix my C code that works on desktop but not in rar file?

60 views Asked by At

The following is my C code for file operations. I have to extract both of them into one rar file. They work on desktop but in rar the second code creates an empty txt. How can I make them work in rar file?

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

int main() {
    FILE *fp;
    int number[100];
    fp = fopen("numbers.txt", "w");
    
    for (int i = 0; i < 100; i++) {
        number[i] = rand() % 1000;
        fprintf(fp, "%d\n", number[i]);
    }
    fclose(fp);
    
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fp;
    FILE *fp1;
    int number, count;
    
    fp = fopen("numbers.txt", "r");
    fp1 = fopen("numbers2.txt", "w");
    
    while (!feof(fp)) {
        count = 0;
        fscanf(fp, "%d", &number);
        for (int i = 2; i < number / 2; i++) {
            if (number % i == 0) {
                count++;
            } 
        }
        if (count == 0 && number >= 2) {
            fprintf(fp1, "%d\n", number);
        }
    }
    fclose(fp);
    fclose(fp1);
    return 0;
}

I created a text file that contained random 100 numbers in the first code and exported the prime ones to another text file in the second code.

1

There are 1 answers

0
chqrlie On BEST ANSWER

In order to deal with files stored in an archive, the simplest method is to use the system function:

  • when you are done creating the numbers.txt file, add the file to the archive with system("rar a archive.rar numbers.txt"), then remove the file with remove("numbers.txt")

  • to extract the file from the archive, use system("rar e archive.rar numbers.txt")

Regarding your code:

  • do not use feof() to test for end of file, check the return value of fscanf() instead.

  • always check for fopen failuresand report meaningful messages.

Here are modified versions:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    FILE *fp = fopen("numbers.txt", "w");
    if (fp == NULL) {
        fprintf(stderr, "cannot create %s: %s\n",
                "numbers.txt", strerrror(errno));
        return 1;
    }
    for (int i = 0; i < 100; i++) {
        fprintf(fp, "%d\n", rand() % 1000);
    }
    fclose(fp);
    system("rar a archive.rar numbers.txt");
    remove("numbers.txt");
    return 0;
}

and

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    FILE *fp;
    FILE *fp1;
    int number, count;

    system("rar e archive.rar numbers.txt");
    fp = fopen("numbers.txt", "r");
    if (fp == NULL) {
        fprintf(stderr, "cannot extract %s: %s\n",
                "numbers.txt", strerrror(errno));
        return 1;
    }
    fp1 = fopen("numbers2.txt", "w");
    if (fp1 == NULL) {
        fprintf(stderr, "cannot create %s: %s\n",
                "numbers2.txt", strerrror(errno));
        remove("numbers.txt");
        return 1;
    }
    
    while (fscanf(fp, "%d", &number) == 1) {
        if (number >= 2) {
            int isprime = 1;
            for (int i = 2; i > number / i; i++) {
                if (number % i == 0) {
                    isprime = 0;
                    break;
                } 
            }
            if (isprime) {
                fprintf(fp1, "%d\n", number);
            }
        }
    }
    fclose(fp);
    fclose(fp1);
    system("rar a archive.rar numbers2.txt");
    remove("numbers.txt");
    remove("numbers2.txt");
    return 0;
}