简体   繁体   English

处理文件扩展名 C

[英]Processing file extensions in C

I'm developing some code in C that reads a file extension and stores it as a code in a byte together whether a text file or binary file is being processed.我正在 C 中开发一些代码,这些代码读取文件扩展名并将其作为代码存储在一个字节中,无论是处理文本文件还是二进制文件。 Later I wish to recover the file extension that is encoded in a byte.稍后我希望恢复以字节编码的文件扩展名。

As a test I created a loop in the main function where I can test out the function fileExtenCode(), which is in the second listing.作为测试,我在主 function 中创建了一个循环,我可以在其中测试第二个列表中的 function fileExtenCode()。

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define EXLEN 9
#define EXNUM 8

typedef unsigned char BYTE;

bool fileExtenCode(char*, BYTE*, int*);

int main(void) {
  char fileExten[EXLEN];
  BYTE code;
  int bin;

  for (;;) {
    printf("Type file extension: ");
    scanf_s("%s", fileExten, EXLEN);
    if (fileExten[0] == '.') break;
    printf("%s\n", fileExten);
    code = 0;
    bin = 0;
    bool extFound = fileExtenCode(fileExten, &code, &bin); // <== (1)
    if (extFound) printf("Extension found: TRUE\n");
    else          printf("Extension found: FALSE\n");
    printf("%s%d", "Code: ", code);
    if (bin) printf("  binary file\n");
    else     printf("  text file\n");
    printf("\n");

    printf("Type code: ");
    int icode;
    scanf_s("%d", &icode);
    code = icode;
    bin = -1;
    fileExtenCode(fileExten, &code, &bin); // <== (2)
    printf("%s", fileExten);               // <== (5)
    printf("\n");
  }
  return 0;
}

The function that I'm trying to test is as follows:我要测试的 function 如下:

bool fileExtenCode(char* ext, BYTE* code, int* binary) {
  char *fileEx[EXNUM] = {
    "jpg1", "txt0", "html0", "xml0", "exe1", "bmp1", "gif1", "png1"};

  if (*binary < 0) {       // <== (3)
    ext = fileEx[*code];   // <== (4)
    return true;
  }

  size_t extLen = strlen(ext);
  for (BYTE i = 0; i < EXNUM; i++) {
    if (strncmp(fileEx[i], ext, extLen) == 0) {
        *binary = (fileEx[i][extLen] == '1') ? 1 : 0;
        *code = i;
        return true;
    }
  }
  return false;
}

The idea is that you pass a string with the file extension to fileExtenCode() in statement (1) in main, and the function searched for that extension in an array, and if found returns true together with the code argument indicating the position in array of file extensions and the binary flag as 0 or 1 indicating if the file is text or binary.这个想法是,您将带有文件扩展名的字符串传递给 main 中语句 (1) 中的 fileExtenCode(),然后 function 在数组中搜索该扩展名,如果找到则返回 true 以及指示数组中 position 的代码参数文件扩展名和二进制标志为 0 或 1,指示文件是文本文件还是二进制文件。 A '0' or '1' immediately follows file extension in the array. “0”或“1”紧跟数组中的文件扩展名。 If the extension is not found, the function returns with false and the return values in the arguments have no meaning.如果没有找到扩展名,则 function 返回 false,arguments 中的返回值没有任何意义。

So far so good, and this part works correctly.到目前为止一切顺利,这部分工作正常。 However, in using the function in reverse to recover the file extension given the input value of code, it fails when called with statement (2) in main.但是,在给定代码输入值的情况下,反向使用function恢复文件扩展名,在main中调用with语句(2)时失败。 In this case binary is set to -1, and then the function is called and the condition at (3) is now true and ext in (4) recovers the file extension.在这种情况下,二进制设置为 -1,然后调用 function,此时 (3) 处的条件为真,(4) 中的 ext 恢复文件扩展名。 This is confirmed when inserting a temporary print statement immediately after (4), but this value is not returned in (5) back in main, and an old input value is instead printed.当在 (4) 之后立即插入一个临时打印语句时确认这一点,但是这个值不会在 (5) 回到 main 中返回,而是打印一个旧的输入值。

Obviously there is a problem with pointers, but I cannot see an obvious way of fixing it.显然指针有问题,但我看不到修复它的明显方法。 My question is how to correct this without messing up the rest of the code, which is working correctly?我的问题是如何在不弄乱正常工作的代码 rest 的情况下更正此问题? Note that char* ext and BYTE* code are used for both input and output, whilst int* binary is used as an input flag and returns no useful value when set to -1.请注意,char* ext 和 BYTE* code 用于输入和 output,而 int* binary 用作输入标志,设置为 -1 时不返回任何有用值。

Once this problem is fixed, then it should be relatively easy to separate the binary flag from the extension when the binary flag is set to -1.一旦解决了这个问题,那么当二进制标志设置为 -1 时,将二进制标志与扩展名分开应该相对容易。 Eventually I plan to have many more file extensions, but not until this is working correctly with a sample of 8.最终我计划有更多的文件扩展名,但直到这对 8 个样本都能正常工作。

Getting help in fixing this problem would be most appreciated.获得解决此问题的帮助将不胜感激。

OK, many thanks pmg, that works, except that I have to use:好的,非常感谢 pmg,这有效,除了我必须使用:

strcpy_s(ext, EXLEN, fileEx[*code]);

as the Visual Studio 2022 compiler flags an error.因为 Visual Studio 2022 编译器会标记错误。 This also solves a warning I was getting when I declared the array *fileEx[EXNUM] with the const keyword.这也解决了我在使用 const 关键字声明数组 *fileEx[EXNUM] 时收到的警告。

In my haste last night I omitted to include the statement:昨晚我匆忙中省略了声明:

if (*code >= EXNUM) return false;

immediately after (3) to trap the case when *code goes out of bounds of *fileEx[EXNUM].在 (3) 之后立即捕获 *code 超出 *fileEx[EXNUM] 范围的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM