簡體   English   中英

C程序讀取大文本文件並將信息存儲在struct中

[英]C program reading large text file and storing information in 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”

您只需要一點點檢查就可以達到目的。 您需要保留以前的名稱,直到找到僅一行“ \\ 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM