C程序读取大文本文件并将信息存储在struct中

标签 c file struct

我正在开发一个 C 程序,它需要我读取一个相当大的文本文件并将信息存储在结构中。该文件包含 Actor 姓名和他们出演过的电影。我已经搜索了我的教科书和其他在线资源,但仍然不知道如何继续。

我有一个旧程序,可以读取类似但格式更好的文件。我需要对其进行更改以满足我对该项目的需求,但不知道如何进行。

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
#define START 239
#define END 374

//method to find the index of a char c in a string
int indexOf(char c, char *string){
    ///iterating through char array, checking if any character matches c
    for(int i=0;string[i]!='\0';i++){
        if(string[i]==c){
        //found
        return i;
        }
    }
    //not found
    return -1;
}

//method to find the substring of a string between indices from and to
//and store the result in result

void substring(char *string, int from, int to, char *result){
    int index=0;
    //storing characters between from and to to result
    for(int i=from;i<to;i++){
        result[index]=string[i];
        index++;
    }
    //null terminating the array
    result[index]='\0';
}


//a structure to represent an actor

struct Actor{
    char lastName[20];
    char firstName[20];
    char movie[20];
};

//method to print name and movie of an actor in separate lines

void print(struct Actor actor) {
    printf("First name: %s\n",actor.firstName);
    printf("Last name: %s\n",actor.lastName);
    printf("Movie: %s\n\n",actor.movie);
}

int main(){

    //creating a file pointer, asking user for the file name
    FILE *fp;
    //opening file in read mode
    fp = fopen("./actors.txt","r");

    if(fp == NULL){
        //file can not be opened
        printf("File not found!\n");
        return 0;
    }

    //creating a char array to store each line, one at a time
    char buffer[100];
    //creating an Actor structure object
    struct Actor actor;
    //needed variables
    int index1 = 0, index2 = 0,index3 = 0, index4 = 0;
    //reading all lines one by one
    int i = 0;
    while(fgets(buffer, 100, fp)){
        i++;
        if(i > START && i < END ){
            getLen(buffer);
            ///finding index of comma (,)
            index1 = indexOf(',',buffer);
            //cutting the string between indices 0 and index1
            //and storing as actor's lastname
            substring(buffer,0,index1,actor.lastName);
            ///finding index of tab (\t)
            index2=indexOf('\t',buffer);
            //storing string between indices index1 and index2 in firstname
            substring(buffer,index1,index2,actor.firstName);
            ///finding year parentheses
            index3=indexOf('(', buffer);
            ///fetching movie title

            substring(buffer,index2,index3-1,actor.movie);
            //printing actor
            print(actor);
        }
    }
        //closing file
    fclose(fp);
}

文本文件中的数据格式为:

lastname, firstname\t\tMovie (year) [role]
\t\t\tmore movies

我只需要 Actor 姓名和他们出演过的电影。这是我尝试读取和存储的数据示例。

Parr, Brian (I)     Blue Ice (1992)  [Stallholder]  <20>
        Eskimo Day (1996) (TV)  [Second cabbie]  <22>
        Summer in the Suburbs (2000) (TV)  [Neighbor #2]  <22>
        The fairy queen (La reine des fées) (1989) (TV)  [Snug]  <12>

Rogers, Marcus (II)     .357 (2005)  [Joshua]
        Streets (2004)  [Man in car]
        Summer in the Suburbs (2000) (TV)  [Bobby]  <16>
        "15 Storeys High" (2002) {The Sofa (#1.1)}  [Lawyer]  <5>

这是我的输出:

First name: , Brian (I)
Last name: Parr
Movie:

First name:
Last name:
Movie:                   Eskimo Day

First name:
Last name:
Movie:                   Summer in the SubrnSw

First name: b
Last name:
Movie:                   The fairy queen

First name: b
Last name:
Movie:

First name: , Marcus (II)
Last name: Rogers
Movie:

First name: b
Last name:
Movie:                   Streets

First name: b
Last name:
Movie:                   Summer in the SubrnSw

First name: b
Last name:
Movie:                   "15 Storeys High"rnSw

如何读取这些文件并将它们存储在我的结构中,以便它们不会使用额外的制表符和字符进行打印?此外,该结构需要一系列电影,因此我试图让它打印如下:

Actor Name
Movies
Movies
Movies
Movies

我尝试添加一个循环来执行此操作,但没有成功。我对 C 还很陌生,而且我的教科书很棒。我在网上搜索了其他资源,但似乎找不到任何东西。请问,我该如何解决这个问题,以便我只读取和存储名称和电影?

另一方面,我不关心带有双引号“show”的电视节目

最佳答案

您只需要进行一些检查即可获得结果。 您需要保留以前的名称,直到找到仅包含“\n”的行。 也无需重新定义 C 标准库中已存在的函数(但如果需要,您可以重新实现它们):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
#define START 0
#define END 374

//a structure to represent an actor

struct Actor{
    char lastName[20];
    char firstName[20];
    char movie[50];
};

//method to print name and movie of an actor in separate lines

void print(struct Actor actor) {
    printf("First name: %s\n",actor.firstName);
    printf("Last name: %s\n",actor.lastName);
    printf("Movie: %s\n\n",actor.movie);
}

int main(){

    //creating a file pointer, asking user for the file name
    FILE *fp;
    //opening file in read mode
    fp = fopen("./actors.txt","r");

    if(fp == NULL){
        //file can not be opened
        printf("File not found!\n");
        return 0;
    }

    //creating a char array to store each line, one at a time
    char buffer[100];
    //creating an Actor structure object
    struct Actor actor;
    //reading all lines one by one
    int i = 0;
    int check=0;
    char *ptr;
    while(fgets(buffer, 100, fp)){
        i++;
        int len;
        if(i > START && i < END ){
            if ( strcmp( buffer, "\n") == 0)
            {
                check = 0;
                continue;
            }
            if( !check)
            {
                len = strchr(buffer, ',') - buffer - 1;
                strncpy( actor.lastName, buffer, len);
                actor.lastName[len] = '\0';
                if( (ptr = strchr(buffer, ',')))
                {
                    len = strchr(buffer, '\t') - ptr -1;
                    strncpy( actor.firstName, ptr+1, len);
                    actor.firstName[len] = '\0';
                }
                check = 1;
            }
            if( (ptr = strchr(buffer, '\t')))
            {
                len = strchr( ptr, '(') - ptr-2;
                strncpy( actor.movie, ptr+2, len);
                actor.movie[len] = '\0';
            }
            //printing actor
            print(actor);
        }
    }
        //closing file
    fclose(fp);
}

输出

First name:  Brian (I)
Last name: Par
Movie: Blue Ice 

First name:  Brian (I)
Last name: Par
Movie: Eskimo Day 

First name:  Brian (I)
Last name: Par
Movie: Summer in the Suburbs 

First name:  Brian (I)
Last name: Par
Movie: The fairy queen 

First name:  Marcus (II)
Last name: Roger
Movie: .357 

First name:  Marcus (II)
Last name: Roger
Movie: Streets 

First name:  Marcus (II)
Last name: Roger
Movie: Summer in the Suburbs 

First name:  Marcus (II)
Last name: Roger
Movie: "15 Storeys High"  

如果您愿意,您还可以通过创建类似的函数来概括这一点

void parse( char * dest, char * string, char delim, int offset1, int offset2)
{
    int len = strchr(string, delim) - string - 1 - offset1;
    strncpy( dest, string + 1 + offset2, len);
    dest[len] = '\0';
}

这将使代码看起来像这样:

        if( !check)
        {
            parse(actor.lastName, buffer, ',', 0, -1);
            if( (ptr = strchr(buffer, ',')))
                parse( actor.firstName, ptr, '\t', 0,0);
            check = 1;
        }
        if( (ptr = strchr(buffer, '\t')))
            parse( actor.movie, ptr, '(', 1, 1);
        //printing actor
        print(actor);

关于C程序读取大文本文件并将信息存储在struct中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53697776/

相关文章:

c++ - C/C++ 结构包装不起作用

c - 使用 getline 后出现段错误

c - 如何使用GDI将RGB位图绘制到窗口?

c++ - 第 3 方库冲突定义/重新定义

Java 导出 - 文本文件

c - malloc 结构指针数组与结构数组

c - Aarch64 程序集中的系统调用调用

python - 使用python提取zip文件

c - 尝试使用字符串搜索随机访问文件

c - 我应该返回函数中的指针以便我可以使用它们吗?例如,我应该返回 table2_entry 吗?