-1
with header;
with linker;
package body(* some text )
vin float32=2.0;
mis float32=3.0;
raj pointtodatatype.array(.234,-.2344323343,.234555656,.2334445344)
rex float32=3*3.142345634;
procedure

我想从文本文件中读取这些数据,并将数据复制到开始package的行之间procedure

所以输出将是另一个包含以下内容的文本文件:

vin float32=2.0;
mis float32=3.0;
raj pointtodatatype.array(.234,-.2344323343,.234555656,.2334445344)
rex float32=3*3.142345634;
4

1 回答 1

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

int main(void)
{
    char line[4096];

    while (fgets(line, sizeof(line), stdin) != 0)
    {
        if (strncmp(line, "package ", sizeof("package ")-1) == 0)
        {
            while (fgets(line, sizeof(line), stdin) != 0)
            {
                if (strncmp(line, "procedure\n", sizeof("procedure\n")-1) == 0)
                    break;
                fputs(line, stdout);
            }
            break;
        }
    }
    return 0;
}

The code reads from standard input and writes to standard output. If you have data in file1 and want the results in file2, use:

./program <file1 >file2

This is one of the beauties of standard input/standard output filters and shell I/O redirection.

Tested. Assuming no trailing blanks after the procedure, the code now looks for "procedure\n". If you have to allow for possible trailing blanks or no trailing blanks, the comparison for procedure is fiddlier.

于 2013-05-24T05:47:55.587 回答