首页 文章

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

提问于
浏览
0

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

我有一个较旧的程序,读取类似的文件,但格式更好 . 我需要改变它以适应我对这个项目的需求,但不知道如何 .

My code:

#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);
}

The data in the text file is in the format:

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

我只需要他们所在的演员姓名和电影 . 这是我试图阅读和存储的数据样本 .

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>

Here is my output:

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”的电视节目

1 回答

  • 0

    您只需要进行一些检查即可获得结果 . 您需要保留以前的名称,直到找到仅为“\ 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);
    

相关问题