如何打开递增的单个文件?

huangapple go评论200阅读模式
英文:

How do I open individual files that increment up?

问题

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. const int BLOCK_SIZE = 512;
  4. int buffer[BLOCK_SIZE];
  5. unsigned int file_num = 0;
  6. int main()
  7. {
  8. // Open memory card
  9. FILE *pFile = fopen("card.raw", "rb");
  10. // Open and create JPEG files
  11. while (fread(buffer, 1, BLOCK_SIZE, pFile) == BLOCK_SIZE)
  12. {
  13. if (some condition)
  14. {
  15. // Create JPEG file
  16. char filename[10];
  17. sprintf(filename, "%.3d.jpg", file_num++);
  18. // Open new JPEG files to write data (this is the corrected part)
  19. FILE *pImg = fopen(filename, "wb");
  20. // Write data to the new JPEG file
  21. // ...
  22. // Close the new JPEG file when you're done writing
  23. fclose(pImg);
  24. }
  25. }
  26. // Close the memory card file when you're done
  27. fclose(pFile);
  28. return 0;
  29. }

我已经帮您修正了代码中的问题,现在代码应该能够正确地打开新创建的JPEG文件以写入数据。如果您有任何其他问题,请随时提问。

英文:

So I'm doing this homework where I have to go through a memory card and recover some images by putting data into a buffer, creating new JPEG files from that buffer data and then writing that data into those new files to get the images. Right now, I'm stuck on how to open the newly created files using the fopen function in C in order to write that data, and each file is named, "###.jpg", incrementing up from 000.jpg to 050.jpg. I could just open and write the files over and over I guess, but that's obviously instinctively way wrong.

Below is a rough snippet of code to explain my problem. Not really worried about memory allocation or segmentation faults/errors just yet while writing this, I'm just trying to convey my idea so you all understand my problem. I tried putting file_num at the end parameter of fopen after "w", but that just got an error.

  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdlib.h&gt;
  3. const BLOCK_SIZE = 512;
  4. int buffer[BLOCK_SIZE];
  5. unsigned int file_num = 0;
  6. int main()
  7. {
  8. //Open memory card
  9. FILE *pFile = fopen(card.raw, &quot;r&quot;);
  10. //Open and create JPEG files
  11. while(fread(buffer, 1, BLOCK_SIZE, pFile) == BLOCK_SIZE)
  12. {
  13. if(some condition)
  14. {
  15. //Create JPEG file
  16. sprintf(pFile, &quot;%.03i.jpg&quot;, file_num++);
  17. //Open new JPEG files to write data(this is my problem right here)
  18. FILE *pImg = fopen(###.jpg, &quot;w&quot;);
  19. ....
  20. }
  21. }
  22. }

答案1

得分: 2

需要一个字符缓冲区来存储sprintf创建的string。这个字符串被用作fopen的第一个参数。

printf用于unsigned int的格式说明符是%u,而不是%i

以下是一个简单的示例,创建文件000.jpg050.jpg,并将每个文件的名称写入相应的文件中(用&lt; &gt;括起来)。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void)
  4. {
  5. char file_name[64];
  6. for (unsigned n = 0; n <= 50; n++) {
  7. sprintf(file_name, "%03u.jpg", n);
  8. FILE *current_file = fopen(file_name, "w");
  9. if (current_file) {
  10. fprintf(current_file, "<%s>\n", file_name);
  11. fclose(current_file);
  12. }
  13. }
  14. }

使用示例:

  1. $ ./create_files
  2. $ ls
  3. 000.jpg 007.jpg 014.jpg 021.jpg 028.jpg 035.jpg 042.jpg 049.jpg
  4. 001.jpg 008.jpg 015.jpg 022.jpg 029.jpg 036.jpg 043.jpg 050.jpg
  5. 002.jpg 009.jpg 016.jpg 023.jpg 030.jpg 037.jpg 044.jpg create_files
  6. 003.jpg 010.jpg 017.jpg 024.jpg 031.jpg 038.jpg 045.jpg create_files.c
  7. 004.jpg 011.jpg 018.jpg 025.jpg 032.jpg 039.jpg 046.jpg
  8. 005.jpg 012.jpg 019.jpg 026.jpg 033.jpg 040.jpg 047.jpg
  9. 006.jpg 013.jpg 020.jpg 027.jpg 034.jpg 041.jpg 048.jpg
  10. $ cat 000.jpg 040.jpg
  11. <000.jpg>
  12. <040.jpg>
  13. $ rm {000..050}.jpg
英文:

You need a character buffer to store the string that sprintf creates. This string is used as the first argument to fopen.

The printf format specifier for unsigned int is %u, not %i.

Here is a simple example that creates the files 000.jpg up to, and including, 050.jpg.

It also writes the name of each file to the respective file (enclosed in &lt; &gt;).

  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdlib.h&gt;
  3. int main(void)
  4. {
  5. char file_name[64];
  6. for (unsigned n = 0; n &lt;= 50; n++) {
  7. sprintf(file_name, &quot;%03u.jpg&quot;, n);
  8. FILE *current_file = fopen(file_name, &quot;w&quot;);
  9. if (current_file) {
  10. fprintf(current_file, &quot;&lt;%s&gt;\n&quot;, file_name);
  11. fclose(current_file);
  12. }
  13. }
  14. }

In use:

  1. $ ./create_files
  2. $ ls
  3. 000.jpg 007.jpg 014.jpg 021.jpg 028.jpg 035.jpg 042.jpg 049.jpg
  4. 001.jpg 008.jpg 015.jpg 022.jpg 029.jpg 036.jpg 043.jpg 050.jpg
  5. 002.jpg 009.jpg 016.jpg 023.jpg 030.jpg 037.jpg 044.jpg create_files
  6. 003.jpg 010.jpg 017.jpg 024.jpg 031.jpg 038.jpg 045.jpg create_files.c
  7. 004.jpg 011.jpg 018.jpg 025.jpg 032.jpg 039.jpg 046.jpg
  8. 005.jpg 012.jpg 019.jpg 026.jpg 033.jpg 040.jpg 047.jpg
  9. 006.jpg 013.jpg 020.jpg 027.jpg 034.jpg 041.jpg 048.jpg
  10. $ cat 000.jpg 040.jpg
  11. &lt;000.jpg&gt;
  12. &lt;040.jpg&gt;
  13. $ rm {000..050}.jpg

huangapple
  • 本文由 发表于 2023年7月31日 20:38:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定