1

所以我有这个文件和这个代码:

int main()
{
    FILE *file;
    file = fopen("fonts.dat", "rb");
    if (file == NULL)
        return 1;

    char readLineBuffer[200];

    if(readLineBuffer == NULL) { return 1; }
    if(readLineBuffer == 0) { return 1; }

    while (fgets(readLineBuffer, 200, file) != NULL)
    {
        readLineBuffer[strcspn(readLineBuffer, "\n")] = '\0';
        //if (readLineBuffer[0] == '\0' || readLineBuffer[0] == '#') {continue;}
        for(int i=0; readLineBuffer[i]!=00; i++)
        {
            if (readLineBuffer[i]=='#')
            {
                readLineBuffer[i] = 00;
                break;
            }
        }
        puts(readLineBuffer);
    }

    fclose(file);

    system("PAUSE");
    return 0;
}

它删除以 . 开头的注释行#

但是我怎样才能从该文件中读取 [FONT_ID] 并将其存储到一个变量中以便在其他代码中使用?

4

1 回答 1

2

这段代码中有很多内容表明您很难掌握 C/C++,或者您一直在从其他程序员/语言那里学习坏习惯。

FILE *file;
file = fopen("fonts.dat", "rb");

尽可能避免将声明和赋值分开。你说这是一个文本文件,所以你不需要以“二进制”模式打开它。读取二进制意味着您将不得不担心不同的行尾类型。以文本模式打开它,操作系统/libc 将为您进行翻译,因此行尾神奇地只是“\n”,就像它们应该的那样。

char readLineBuffer[200];

if(readLineBuffer == NULL) { return 1; }
if(readLineBuffer == 0) { return 1; }

首先,像您刚才那样使用固定大小的存储的优点之一是它永远不会评估为 NULL。使用指针时,您只需要进行空检查。虽然确实 - 在引擎盖下 - “readLineBuffer”可以用作指针,但它也是一个数组。试试下面的简单程序:

#include <stdio.h>

int main(int argc, const char* argv[])
{
    char buffer[1234];
    char* bufPointer = buffer; // yes, it looks like a pointer.
    printf("the size of buffer is %u but the size of bufPointer is %u\n",
           sizeof(buffer),
           sizeof(bufPointer));
    return 0;
}

其次,“NULL”只是一个#define

#define NULL 0

(这就是为什么在 C++11 中他们添加了特殊的“nullptr”常量)。

while (fgets(readLineBuffer, 200, file) != NULL)

手动重复事物的大小是危险的。使用“sizeof()”命令

while (fgets(readLineBuffer, sizeof(readLineBuffer), file) 

您确定文件中没有任何行的长度超过 200 字节吗?想象一下:

fgets(buffer, 20, file);

现在想象这条线:

123456789012345678#这是评论

fget 将读取“123456789012345678#”,您的代码将删除尾随的“#”并执行“puts”,将“123456789012345678\n”写入文件。然后你会读“this is the comment”,找不到注释字符,然后在文件中写一个新行“this is the comment\n”。

其次,由于无论如何您都要迭代该行,您可能需要考虑:

一个。自己迭代缓冲区 for(int i = 0; i < sizeof(readLineBuffer) && i[0] != '\0') { if(readLineBuffer[i] == '\n' || readLineBuffer[i] == '#') { readLineBuffer[i] = 0; 休息; } }

湾。使用 strpbrk char* eol = strpbrk("#\n", readLineBuffer); if ( eol != NULL ) // 找到注释或行尾 *eol = '\0';

这会将您的代码减少到以下内容。虽然这段代码可以在“C++”编译器上编译,但它几乎是纯“C”。

FILE *file = fopen("fonts.dat", "r");
if (file == NULL)
    return 1;

char readLineBuffer[200];
while (fgets(readLineBuffer, sizeof(readLineBuffer), file) != NULL)
{
    // find comment or end of line so we can truncate the line.
    char* eol = strpbrk(readLineBuffer, "#\n");
    if ( eol != NULL )
        *eol = '\0';
    puts(readLineBuffer);
}

fclose(file);

system("PAUSE");
return 0;

如果您想处理和存储正在经过的实际信息,您将需要创建变量来存储它,代码来检查/“解析?readLineBuffer 中的每一行经过时,您会想要要学习使用“sscanf”、“atoi”、“strtoul”等命令,最终您将需要创建一些微型状态机。

或者,您可能想要研究专为此类任务设计的脚本语言,如“Perl”或“Python”。

# perl version

local $/ = /\n{2,}/;  # Read the file as paragraphs.
open(file, "font.dat") || die "Can't open font.dat";
my %data = ();
while ($line = <>) {
    $line =~ s/\s+#.*$//mg;    # Get rid of all the comments.
    $line =~ s/\n\n/\n/sg;     # Fix any blank lines we introduced.

    # Each data block starts with an ini-style label, that is a
    # line starting with a "[", followed by some word-characters (a-z, A-Z, 0-9)
    # and a closing "]", maybe with some whitespace after that.

    # Try to remove a label line, capturing the label, or skip this block.
    next unless $line =~ s/^ \[ (\w+) \] \s* \n+ //sx;

    # Store the remaining text into data indexed on the label.
    my ($label) = ($1);  # the capture
    $data{$label} = $line;
}

print("FONT_ID = $data{'FONT_ID'}\n");

或者用 perlier-perl 编写

local $/ = /\n{2,}/;  # Read blocks separated by 2-or-more newlines (paragraphs)
die "Can't open file" unless open(file, "font.dat");
while (<>) {
    s/\s+#.*$//mg;
    s/\n{2,}/\n/sg;
    $data{$1} = $_ if (s/^\[(\w+)\][^\n]+\n+//s);
}
$fontId = ;
print("font_id = ", int($data{'FONT_ID'}), "\n");
于 2013-05-26T21:28:43.523 回答