I know the first few lines of 'card.raw' are (by opening in a texteditor): bit.ly/18gECvy ˇÿˇ‡JFIFHHˇ€Cˇ€Cˇ¿Vˇƒ

Currently all I want the app to do is print back what's in the .raw file, and then I'll move on to building out the complicated features. Instead of printing out what's above, it starts and continues printing out text that looks like this:

\377\304 'u\204\206\226\262\302\3227\205\246\266\342GSc\224\225\245\265\305\306\325\326Wgs\244\346(w\345\362\366\207\264\304ǃ\223\227\2678H\247\250\343\344\365\377\304

My code:

    #include <stdio.h>
    #include <string.h>
    #include <limits.h>
    int main(int argc, const char * argv[]) {

    FILE* file;

    file = fopen("/Users/jamesgoldstein/CS50/CS50Week4/CS50Recovery/CS50Recovery/CS50Recovery/card.raw", "r");

    char output[LINE_MAX];

    if (file != NULL)
        for (int i = 1; fgets(output, LINE_MAX, file) != NULL; i++)
            printf("%s\n", output);


    return 0;
  • LINE_MAX is not standard C, it must be POSIX. LINE_MAX also think this thought for well-formed strings, I do not think your program to work with a binary file. – MARS Jun 24 '16 at 20:13

Actually, you're getting exactly what's in the file. You do understand that the contents of card.raw is either garbage data or image data, right? The data is not ASCII data, so it will print as whatever the random data maps to. You'll get graphics, strange characters, non-printables, etc., or it may map to something that doesn't translate to anything in the ASCII table.

If you want to see what is in the file, you could change the format string in the printf to output the hex data, or you could use the xxd tool to look at the hex data directly.

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

  • I understand that exactly. That's my point, I'm not seeing all of the characters. I can solve this in a hex editor, I already got one of the images. But using C to read the file I'm getting octal escape characters instead. At least I think that's what all the /###/### output is. – James Goldstein Jun 24 '16 at 23:45
  • I updated my code and snippets of input and output: stackoverflow.com/q/38019939/2113567 – James Goldstein Jun 24 '16 at 23:47
  • But that's my point. They're not characters! It's pure hex data. Add that you're printing using the %s formatter, which means to print the ASCII symbol, not the actual data and the results won't be what you're looking for. Review the format strings at tutorialspoint.com/c_standard_library/c_function_printf.htm and pick something more appropriate. – Cliff B Jun 24 '16 at 23:52
  • So printf(output)? I'm not by a computer, how do I get it to print just hex data to the console? – James Goldstein Jun 25 '16 at 0:32
  • Had a chance to check that url and try %X, and no luck. It just repeats the same 9 hex characters over and over. – James Goldstein Jun 25 '16 at 4:22

You must log in to answer this question.

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