I've been trying PSet4's Recover. I've been trying to obtain the size of the entered file (card.raw) but unfortunately size gives and output of 0.

Here's the code that I've made to find the file's size:

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

int main(int argc, char *argv[])
    int counter = 0;
    if(argc != 2)
        printf("Usage: ./recover image\n");
        return 1;
    FILE *recover = fopen(argv[1], "r");
    if(recover == NULL)
        printf("ERRNO 1 IS %s\n", strerror(errno));
    fseek(recover, 0L, SEEK_END);
    unsigned long size = ftell(recover);
    fseek(recover, 0L, SEEK_SET);
    printf("Size is : %lu", size);

One thing that seems to be happening is that changing argv[1] to argv[0] actually gives me a non zero file size. Any reason why this happens?

  • I ran the program on my machine and it works correctly, the size of the size is somewhat different from the real one, something more than 20 MB (well I still have the oldest pset), but it is not a problem, fseek is not reliable because there may be a memory segmentation problem – MARS Nov 30 '20 at 19:00
  • @MARS This program doesn't give me a Seg. fault though. Also card.raw says 0 bytes when I hover the cursor over the file. Does that mean that the file is not there? – MelPradeep Dec 1 '20 at 3:47
  • the program does not have a segmentation fault, that is not what I mean, memory segmentation is something that the operating system does by itself. It consists of placing a file in non-contiguous memory areas. If card.raw gives you zero I advise you to download the complete problem again. – MARS Dec 1 '20 at 9:51
  • @MARS Already downloaded the .zip file and extracted the files within twice. I'll try again. – MelPradeep Dec 1 '20 at 10:02

The code is working correctly. If it's giving a file size of 0, the file size really is zero. Maybe it was overwritten? If the file weren't there, it would gen an error.

Why are you getting a file size when changing to argv[0]? Simple. The code is using the name of the executable file recover.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Does this mean that it wouldn't be possible for me to finish recover? Because I've tried unzipping the file given in the program to obtain card.raw many times. – MelPradeep Dec 2 '20 at 4:12
  • That would be a totally different issue. You should be able to unzip the zip file and extract the needed file. How are you unzipping the file? – Cliff B Dec 2 '20 at 4:28
  • I use wget https://cdn.cs50.net/2019/fall/psets/4/recover/recover.zip to get card.raw and then use unzip recover.zip to extract the file. – MelPradeep Dec 2 '20 at 10:42
  • I've got the right file now. Thanks for helping! – MelPradeep Dec 3 '20 at 5:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .