Skip to main content
deleted 389 characters in body
Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

No, your code only partially works. It is creating 50 files, but they don't contain valid images. Also, if you run ls -al *.jpg, you'll see that every file is 512 bytes long. The code is only writing the signature blocks out to the files.

After the first signature is found, all the datacode opens a file and writes 512 bytes to that follows is valid image datafile. The code needs to keep reading the data and addingreal problem is that it tois reopening the output file until the next signaturein 'w' mode. This means that each time a 512 byte block is found (at which point it should closeread, the existing file and openis deleted a new one.)

You need to add code to handlefile is created, and the intermediate imagecurrent 512 bytes is written out - but the previous data. There is none inlost because the code nowfile was deleted when the latest fopen is executed.

That's whatThe logic is wrong with your code, and yesmostly sound, it wouldbut the output file should only be opened once and only when a duplicate because thisnew signature is a very common problem. But that's okfound. If you weren't able to recognize For all the problemdata between signature blocks, then you wouldn't understand why it has been seen before. ;-)

You could also improve the code by incorporating the fread into the while statement. You should learn that while(1 == 1) or while(true) essentially creates an infinite loop. Infinite loopsdata blocks should almost alwayssimply be avoided, unless it is truly intendedwritten to run foreverthe already opened file. (Deliberate infinite loops are more often used in OS operations

Also, but generally not in application codethe signature check is only checking the first 3 bytes. If you're creating an infinite loop, you should be ableIt needs to clearly articulate whycheck the 4th byte too.)

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

No, your code only partially works. It is creating 50 files, but they don't contain valid images. Also, if you run ls -al *.jpg, you'll see that every file is 512 bytes long. The code is only writing the signature blocks out to the files.

After the first signature is found, all the data that follows is valid image data. The code needs to keep reading the data and adding it to the output file until the next signature block is found (at which point it should close the existing file and open a new one.)

You need to add code to handle the intermediate image data. There is none in the code now.

That's what is wrong with your code, and yes, it would be a duplicate because this is a very common problem. But that's ok. If you weren't able to recognize the problem, then you wouldn't understand why it has been seen before. ;-)

You could also improve the code by incorporating the fread into the while statement. You should learn that while(1 == 1) or while(true) essentially creates an infinite loop. Infinite loops should almost always be avoided, unless it is truly intended to run forever. (Deliberate infinite loops are more often used in OS operations, but generally not in application code. If you're creating an infinite loop, you should be able to clearly articulate why.)

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

No, your code only partially works. It is creating 50 files, but they don't contain valid images. Also, if you run ls -al *.jpg, you'll see that every file is 512 bytes long.

After the first signature is found, the code opens a file and writes 512 bytes to that file. The real problem is that it is reopening the file in 'w' mode. This means that each time a 512 byte block is read, the existing file is deleted a new file is created, and the current 512 bytes is written out - but the previous data is lost because the file was deleted when the latest fopen is executed.

The logic is mostly sound, but the output file should only be opened once and only when a new signature is found. For all the data between signature blocks, the data blocks should simply be written to the already opened file.

Also, the signature check is only checking the first 3 bytes. It needs to check the 4th byte too.

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

added 462 characters in body
Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

No, your code only partially works. It is creating 50 files, but they don't contain valid images. Also, if you run ls -al *.jpg, you'll see that every file is 512 bytes long. The code is only writing the signature blocks out to the files.

After the first signature is found, all the data that follows is valid image data. The code needs to keep reading the data and adding it to the output file until the next signature block is found (at which point it should close the existing file and open a new one.)

You need to add code to handle the intermediate image data. There is none in the code now.

That's what is wrong with your code, and yes, it would be a duplicate because this is a very common problem. But that's ok. If you weren't able to recognize the problem, then you wouldn't understand why it has been seen before. ;-)

You could also improve the code by incorporating the fread into the while statement. You should learn that while(1 == 1) or while(true) essentially creates an infinite loop. Infinite loops should almost always be avoided, unless it is truly intended to run forever. (Deliberate infinite loops are more often used in OS operations, but generally not in application code. If you're creating an infinite loop, you should be able to clearly articulate why.)

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

No, your code only partially works. It is creating 50 files, but they don't contain valid images. Also, if you run ls -al *.jpg, you'll see that every file is 512 bytes long. The code is only writing the signature blocks out to the files.

After the first signature is found, all the data that follows is valid image data. The code needs to keep reading the data and adding it to the output file until the next signature block is found (at which point it should close the existing file and open a new one.)

You need to add code to handle the intermediate image data. There is none in the code now.

That's what is wrong with your code, and yes, it would be a duplicate because this is a very common problem. But that's ok. If you weren't able to recognize the problem, then you wouldn't understand why it has been seen before. ;-)

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

No, your code only partially works. It is creating 50 files, but they don't contain valid images. Also, if you run ls -al *.jpg, you'll see that every file is 512 bytes long. The code is only writing the signature blocks out to the files.

After the first signature is found, all the data that follows is valid image data. The code needs to keep reading the data and adding it to the output file until the next signature block is found (at which point it should close the existing file and open a new one.)

You need to add code to handle the intermediate image data. There is none in the code now.

That's what is wrong with your code, and yes, it would be a duplicate because this is a very common problem. But that's ok. If you weren't able to recognize the problem, then you wouldn't understand why it has been seen before. ;-)

You could also improve the code by incorporating the fread into the while statement. You should learn that while(1 == 1) or while(true) essentially creates an infinite loop. Infinite loops should almost always be avoided, unless it is truly intended to run forever. (Deliberate infinite loops are more often used in OS operations, but generally not in application code. If you're creating an infinite loop, you should be able to clearly articulate why.)

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

Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

No, your code only partially works. It is creating 50 files, but they don't contain valid images. Also, if you run ls -al *.jpg, you'll see that every file is 512 bytes long. The code is only writing the signature blocks out to the files.

After the first signature is found, all the data that follows is valid image data. The code needs to keep reading the data and adding it to the output file until the next signature block is found (at which point it should close the existing file and open a new one.)

You need to add code to handle the intermediate image data. There is none in the code now.

That's what is wrong with your code, and yes, it would be a duplicate because this is a very common problem. But that's ok. If you weren't able to recognize the problem, then you wouldn't understand why it has been seen before. ;-)

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