简体   繁体   中英

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. 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. 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. 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).

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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