Actually, the program is working exactly as designed. Unfortunately, it isn't what you think. ;-)
If you run the program, it will essentially copy the input file to the output file. Along the way, it will print each char that is read. The reason for your confusion is that you're expecting it to printf what you see in the file. Because of the formatting ...
You could try printing out the filename before trying to open the file to confirm that it's correct. You should also hardcode the filename into the program, as specified by the pset and get rid of the argc test completely. It was a nice idea, but it won't pass check50 as it is currently written.
// open input file
FILE* inptr = fopen("card.raw", "r");
OK, so it turns out there was already an answer to this on Reddit. The problem was that I was declaring my pointer again, instead of just reassigning.
In other words, I was doing:
FILE* img = fopen(...);, fclose(img), FILE* img = fopen(...);.
When I should be doing:
FILE* img = fopen(...);, fclose(img), img = fopen(...);.
Check out the reddit post for ...
You're trying to read 2 elements of size 512 bytes into a buffer which I assume is of size 512 bytes only. That's definitely one reason why your program segfaults.
Also, fread() returns the number of bytes that were successfully read if the number of elements is 1. I don't really know why you're checking whether the return value is not equal to 1 in this ...
I'll bet money on this one. What's the declaration for filename? Is it less than 8 chars? I bet it's 7 chars long and you forgot about the end of string marker.
This is a very common error in this pset. When the filename string is written to filename, the entire string is written out, including the EOS marker, no matter what the length of filename ...
The logic of the program has some serious flaws. While individual statements are functioning correctly, its the overall logic that isn't working. Go back and look at what the program is doing.
Read until first signature is found.
Open the first file and write out the first block.
In the inner while loop, read the next block.
Write the block out to the ...
Remember that you need to make a new file to write the images two. It looks like you successfully open card.raw, but all you do is name it in your for loop. You probably want to first read from your file to some sort of buffer that is 512 bytes long. Why? Because you know that each jpeg is sectioned into blocks each using the FAT system explained in the ...
The fread and fwrite functions are working fine, as expected, the problems are elsewhere, a somewhat more coherent way of writing your program will be the following:
typedef uint8_t BYTE;
FILE* open = fopen("open.txt","r");
fopen takes a pointer to the file and you've given it a string "input". Try fopen(input, "r"); instead of fopen("input", "r"); Do you see the difference? In the first, you are telling fopen that the filename is stored in the char array called input. In the second, you are telling fopen to open the file called "input".