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

时间:2018-12-09 23:32:53

标签: c file struct

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

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

我的代码:

#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

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

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”

1 个答案:

答案 0 :(得分: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);