如何从该文件读取值

How to read values from this file

本文关键字:读取 文件      更新时间:2023-10-16

我有这个文件和代码:

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")] = '';
        //if (readLineBuffer[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]这样的文件中读取并将其存储到一个变量中以便在其他代码中使用呢?

这段代码中有很多东西表明你很难掌握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。只有在处理指针时才需要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 %un",
           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#这是评论

fgets将读取"123456789012345678#",您的代码将删除后面的"#"并执行"puts"操作,这将向文件写入"123456789012345678n"。然后你会读到"this is the comment",而不是找到一个注释字符,并在文件中写一个新的行"this is the commentn"。

其次,由于无论如何都要迭代这一行,您可能需要考虑以下两种:

。自己迭代缓冲区For (int I = 0;我& lt;sizeof (readLineBuffer),,I [0] != ''){如果(readLineBuffer[我]= = ' n ' | | readLineBuffer[我]= = ' # '){readLineBuffer[i] = 0;打破;}}

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

这将把您的代码简化为以下内容。虽然这段代码可以在"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 = '';
    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/nn/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");