简体   繁体   English

C 从文件中读取格式化文本

[英]C reading formatted text from file

i have a question on a C program that I'm doing.我对我正在做的 C 程序有疑问。 The beginning of the track ask this:曲目的开头问这个:
"Process P ask as argument the path of a file in which every line ust be 16 characters length (included the end of line), and every line must start with "WAIT" or "NOWAIT" followed by a command." “进程 P 询问文件的路径作为参数,其中每行必须为 16 个字符长度(包括行尾),并且每行必须以“WAIT”或“NOWAIT”开头,后跟一个命令。”
The example of input file is:输入文件的例子是:

WAIT ls
NOWAIT who
WAIT date

I made this code for now:我现在做了这个代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define MIN_SIZE 5
#define ROW_LEN 17
int main (int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Program usage: %s file_path.\n", argv[0]);
        exit(1);
    }
    
    int fd = open(argv[1], O_RDONLY);
    struct stat fd_info;
    
    if(fd < 0) {
        perror("Error opening file");
        exit(2);
    }
    
    
    fstat(fd, &fd_info);
    if(fd_info.st_size <= MIN_SIZE) {
        printf("Size of file '%s' is less or equal than 5 bytes.\n", argv[1]);
        exit(3);
    }
    
    char buf[ROW_LEN];
    buf[ROW_LEN - 1] = '\0';
    while ((read(fd, buf, ROW_LEN - 1)) > 0) {
        char type[ROW_LEN], cmd[ROW_LEN];
        sscanf(buf, "%s %s", type, cmd);
        printf("type=%s; command=%s;\n", type, cmd);
    }
    
    
    return 0;
}

In this way i can read good only if in the file.txt I complete every row with spaces until it reaches 15 characters for each line (else it start reading next line too).这样,只有在 file.txt 中我用空格完成每一行,直到每行达到 15 个字符(否则它也开始读取下一行)时,我才能读得很好。 But in the file that prof gave us there aren't spaces to complete the row.但是在 prof 给我们的文件中,没有空格来完成该行。 So my question is, how can I read correctly from the file?所以我的问题是,我怎样才能从文件中正确读取? I can't understand that "every line must have 16 characters included end of line".我无法理解“每行必须包含 16 个字符,包括行尾”。 Thanks to all, I hope I explained good the question!谢谢大家,我希望我能很好地解释这个问题!

Firstly with this sentence you must considerate each line as a possible input, but the input is coming from anyone so anything can append and any errors in consequences.首先,对于这句话,您必须将每一行视为可能的输入,但输入来自任何人,因此任何东西都可以 append 和任何错误后果。

You start on the good way you must consider all your file and after your line > check if your line is good您从必须考虑所有文件的好方法开始,然后在您的线路之后 > 检查您的线路是否良好

you can use getline to get your file easily, and strlen and strcmp to check if your line is conform.您可以使用 getline 轻松获取文件,并使用 strlen 和 strcmp 检查您的行是否符合要求。

Finaly the part "include end of line", that mean that all the line must have a length of 16 character with '\0', so in your file the "visible" length must be at 15 for the maximum,最后是“包括行尾”部分,这意味着所有行的长度必须为 16 个字符,并带有 '\0',因此在您的文件中,“可见”长度必须为 15 的最大值,

for example if the maximal length is 3 included end of line: "abc": incorrect because it's equal to {'a', 'b', 'c' '\0'};例如,如果最大长度为 3,则包含行尾:“abc”:不正确,因为它等于 {'a', 'b', 'c' '\0'}; "ab": correct because it's equal to {'a', 'b', '\0'}; "ab": 正确,因为它等于 {'a', 'b', '\0'};

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

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