简体   繁体   English

随机排序读取文件

[英]Read file with random ordering

I need to read a file that has random ordering of the ASCII characters and do something based on whatever character I get. 我需要读取一个具有ASCII字符随机顺序的文件,并根据我得到的任何字符来执行某些操作。 I can get any of these character types "/", "*", a string, or numbers. 我可以获取任何这些字符类型“ /”,“ *”,字符串或数字。 I need to do something different with each case. 对于每种情况,我需要做一些不同的事情。 I think the best way would be to start with fgets and read line by line. 我认为最好的方法是从fgets开始并逐行阅读。 After that I would usually use sscanf, but since the files I will be reading have random ordering of the ASCII characters I'm not sure how to do this. 之后,我通常会使用sscanf,但是由于要读取的文件具有ASCII字符的随机顺序,因此我不确定如何执行此操作。 This is what I have started with. 这就是我开始的。 Any ideas would be greatly appreciated. 任何想法将不胜感激。

while(fgets(buffer, 80, fp) != NULL)
{
    /*if(/)
    {
        //do something for "/" character
    }
    if(*)
    {
        //do something for "*" character
    }
    if(string)
    {
        //do something for string
    }
    if(numbers)
    {
        //do something for numbers
    }*/
    memset(buffer, 0, 80);
}

example like this: 像这样的例子:

while(fgets(buffer, 80, fp) != NULL)
{
    char ch, cx = 0;
    double n;

    if(sscanf(buffer, "%c %c", &ch, &cx) == 1){
        if(ch == '/'){
            puts("/");
        } else if(ch == '*'){
            puts("*");
        }
    } else if(sscanf(buffer, "%lf %c", &n, &cx) == 1){
        puts("number");
    } else {//Check further limiting condition?
        puts("string");
    }
    memset(buffer, 0, 80);
}

You are thinking correctly. 您的想法正确。 The key to any of these type problems is to read the line into the buffer, as you have done. 解决所有这些类型问题的关键是,就像您所做的那样,将行读入缓冲区。 Then you have to make sense of what's in the buffer. 然后,您必须弄清楚缓冲区中的内容。 Generally, most decisions can be made on the first character in the buffer (eg +,-,/,*,... ). 通常,大多数决定都可以对缓冲区中的第一个字符(例如+,-,/,*,... )进行。

There are other situations where you will need to process the line further after you have passed your initial test (eg isdigit() , or isalpha() (which can be reduced to simple character comparisons to avoid the function calls). 在其他情况下,通过初始测试后,您将需要进一步处理该行(例如isdigit()isalpha() (可以简化为简单字符比较以避免函数调用))。

The simple way to handle both, is to simply assign a pointer to the buffer, and then work through the buffer using the pointer to test, extract, or pass, the needed element(s) to your handling function. 处理这两种情况的简单方法是,简单地将一个指针分配给缓冲区,然后使用该指针遍历缓冲区以测试,提取或传递所需的元素到处理函数。

Now the next challenge is loop control. 现在,下一个挑战是循环控制。 Some tests my need the entire string, some may only care about the first char . 有些测试需要整个字符串,有些则可能只关心第一个char An approach something like the following will allow you to handle any line in a graceful manner to get the information you need: 类似于以下内容的方法将使您能够以优美的方式处理任何行以获取所需的信息:

while(fgets(buffer, 80, fp) != NULL)
{
    char *p = buffer;
    while (*p)
    {
        if(/)
        {
            //do something for "/" character
        }
        if(*)
        {
            //do something for "*" character
        }
        if(('A' <= *p && *p <= 'Z') || ('a' <= *p && *p <= 'z'))
        {
            // do something for string
            // handle initial char
            while (*p) {
                // handle rest of string;
                p++;
            }
            if (!*p)
                goto nextline;
        }
        if('1' <= *p && '9' <= *p)
        {
            // do something for numbers
            convert numbers with atoi() or strtol(), ....
            if (endofline)
                goto nextline;
        }
        p++;
    }
    nextline:;
    //memset(buffer, 0, 80); /* not needed */
}

There are more varied and different ways to do this than you can swing a dead cat at. 除了挥舞死猫,还有更多不同的方式可以做到这一点。 It's up to you to choose the method of program control that fits your data. 由您选择适合您数据的程序控制方法。 Let me know if you have questions. 如果您有任何问题,请告诉我。

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

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