Hot answers tagged

3

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 ...


2

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"); BTW,...


2

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 ...


2

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 ...


1

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 ...


1

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 ...


1

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 ...


1

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: #include <stdio.h> #include <stdint.h> #include <stdlib.h> typedef uint8_t BYTE; int main(void) { FILE* open = fopen("open.txt","r"); BYTE read[10]; FILE* ...


1

You added an extra unwanted ; on this line: if ($handle == FALSE); Remove the semicolon and it will work as wanted.


1

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".


Only top voted, non community-wiki answers of a minimum length are eligible