简体   繁体   English

如何从文件中读取数据以进行编程

[英]How can I read data from a file to program

I need to get my data from the file that looks like this:我需要从如下所示的文件中获取数据:

在此处输入图像描述

and then print it on my screen.然后打印在我的屏幕上。

I cant figure how to print it because of dashes (-).由于破折号(-),我无法弄清楚如何打印它。

I tried this code:我试过这段代码:

typedef struct studenti {
    char index[10];
    char ime[20];
    char prezime[20];
    int kviz[10];
} studenti;

void init_load() {
    int i;
    studenti studenti;

    FILE *fp;
    fp = fopen("studenti_2022.txt", "r");
    if(fp == NULL){
        printf("Doslo je do greske");
        return 0;
    }
    while(fread(&studenti, sizeof(studenti), 1, fp)){
        printf("%s", studenti.index);
        printf("%s", studenti.ime);
        printf("%s", studenti.prezime);
        printf("%d", studenti.kviz);
    }
    fclose(fp);
}

fread is generally intended for reading binary data, and is usually the wrong function to use for a text formatted file. fread通常用于读取二进制数据,通常是错误的 function 用于文本格式文件。 Unless you have written your structures directly to a file using fwrite , you will need something else.除非您使用fwrite将结构直接写入文件,否则您将需要其他内容。

In this case, you can use functions like fgets , fgetc , and fscanf to treat the contents of the file as text , and parse a known format.在这种情况下,您可以使用fgetsfgetcfscanf等函数将文件的内容视为文本,并解析已知格式。

strtok can be used to further tokenize a string, and strtol / sscanf can be used to read integers from those tokens. strtok可用于进一步标记字符串,而strtol / sscanf可用于从这些标记中读取整数。

Here is a rough example:这是一个粗略的例子:

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

typedef struct {
    char index[10];
    char ime[20];
    char prezime[20];
    int kviz[10];
} student_info;

int get_next_entry(student_info *si, FILE *f) {
    if (3 != fscanf(f, "%9s%19s%19s", si->index, si->ime, si->prezime))
        return 0;

    char buffer[256];

    if (fgetc(f) != '\n' || !fgets(buffer, sizeof buffer, f))
        return 0;

    size_t i = 0;
    char *tok = strtok(buffer, "|");

    if (tok) do {
        si->kviz[i++] = (int) strtol(tok, NULL, 10);
    } while (i < 10 && (tok = strtok(NULL, "|")));

    /* make sure we read enough ints, and our delimiting line is there. */
    if (i != 10 || !fgets(buffer, sizeof buffer, f) ||
            0 != strncmp(buffer, "----------", 10))
        return 0;

    return 1;
}

int main(void) {
    FILE *file = fopen("data.txt", "r");
    student_info info;

    if (file) while (get_next_entry(&info, file)) {
        printf("%s\n%s\n%s\n", info.index, info.ime, info.prezime);

        for (size_t i = 0; i < 10; i++)
            printf("%d ", info.kviz[i]);
        putchar('\n');
    }

    fclose(file);
}

data.txt : data.txt

I-0123-45
John
Doe
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
--------------------------------------
I-0042-99
Jane
Doe
1 | 4 | 3 | 2 | 14 | 6 | 3 | 8 | 1 | 11 |
---------------------------------------

Output: Output:

I-0123-45
John
Doe
1 2 3 4 5 6 7 8 9 10
I-0042-99
Jane
Doe
1 4 3 2 14 6 3 8 1 11

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

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