-3
int main()
{

    FILE *fp = fopen("/root/ds/filehandling/a.pdf", "rb");
    unsigned char magic[4];
    fread((void *)magic, 1, 4, fp);
    cout << hex << "magic:";
    for (int i = 0;  i < 4;  i++)
        cout << " 0x" << int(magic[i]);
    cout << dec << endl;
    return 0;
}

The problem here is I have to specify the extension then only i would get the magic number , but my objective is to specify the name of file without giving its extension so that on comparing magic number i could determine its type.

This is what i want:

int main() {

    FILE *fp = fopen("/root/ds/filehandling/a", "rb");
    unsigned char magic[4];
    fread((void *)magic, 1, 4, fp);
    cout << hex << "magic:";
    for (int i = 0;  i < 4;  i++)
        cout << " 0x" << int(magic[i]);
    cout << dec << endl;
    return 0;
}

ouput : magic: 0x25 0x50 0x44 0x46

but I a m getting "SEGMENTATION FAULT"

Rishav Kumar
  • 23
  • 1
  • 3
  • So, you want to specify `a` as the file name and get `a.pdf` as the file opened? What do you wan to have happen if a.txt also exists in the directory? – NathanOliver Dec 11 '18 at 17:20
  • if possible read both files magic number – Rishav Kumar Dec 11 '18 at 17:21
  • 1
    I dont understand what you want. Please take some time and ask a better question. You could for instance provide us with a wished user code and expected output/side-effect. Example: "I with to call `get_magic("/path/a")` which will return a list of filenames and associated magic number, the magic number of a file being..." – YSC Dec 11 '18 at 17:22
  • Is it possible to just provide path of the file without specifying the file type – Rishav Kumar Dec 11 '18 at 17:23
  • I want to determine the magic number of a file – Rishav Kumar Dec 11 '18 at 17:24
  • 4
    You don't provide a file type anywhere in your question. An extension is not a file type, it's merely part of the filename. – YSC Dec 11 '18 at 17:24
  • Open the directory, read the directory's contents, and save the names of all files that have a matching name and any extension. Then handle each file found (if any) individually using the existing code. Shouldn't take more than a few minutes to add these bits to your existing code. That seems rather obvious, so what exactly is your question? How to read the contents of the directory? `opendir()` and `readdir()`. See your C++ book for more information. – Sam Varshavchik Dec 11 '18 at 17:25
  • Sorry that's what i meant i dont wan't to take extension as an input – Rishav Kumar Dec 11 '18 at 17:26
  • Can you use C++17? If so you can use the utilities in [``](https://en.cppreference.com/w/cpp/filesystem) to get all of the files in a directory. If not you should be able to use boost. – NathanOliver Dec 11 '18 at 17:26
  • 1
    The "extension" part (`.pdf`) is just part of the file name and says nothing about the contents of the file except by convention. You can not open a file without its **full name** so what you want to do is not possible. – Galik Dec 11 '18 at 17:29
  • *"what i meant i dont wan't to take extension as an input"* Then do what Sam Varshavchik said. – HolyBlackCat Dec 11 '18 at 17:30
  • How would your program behave if there was both `a.txt` and `a.pdf` in the same folder? Which one would it open in your scenario? – Galik Dec 11 '18 at 17:31
  • Thankyou everyone,i understood my mistake – Rishav Kumar Dec 11 '18 at 17:34

1 Answers1

3

You're getting a segmentation fault because there is no such file as /root/ds/filehandling/a, and fopen thus fails, but you never even check its return value. fp is going to be nullptr, and passing that to functions like fread is just begging for trouble.

The question really has nothing to do with magic numbers; if you want to open a file without knowing its extension (i.e. not knowing its full name), you will have to look in its directory and find the file you want by pattern matching. Note that there may not only be just one; e.g. if there's a file a.txt and a file a.pdf, you're going to have to decide which one you pick for the search term "a".

Remember, the file's "extension" is just the last part of its name; it's just a convention. Many files don't even have such a thing!

By the way, don't forget to fclose.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055