2017-02-16 3 views
0

ファイルを読み込んで構造体に格納するプログラムを作成しようとしています。操作などのための関数。しかし、私が試して、後の機能のためにこのファイルにアクセスすると、何も格納/バッファされていないようです。この問題は、 "void readFile"関数のようです。ここで私がこれまで持っているものです。ファイルを読み込んで構造体に格納して、他の関数全体で使用できるようにします。

サンプル入力は: メアリーは
メアリーは フリース雪のように白かった子羊を、持っていた子羊を、持っていました。

私のデバッグはこれを正しく出力しますが、最後の行が繰り返されます。

struct info{ 
    int numOfLines; 
    char lines[]; 
} 

int main (int argc, char * argv[]){ 

    int words, sentences, syllables; 
    int index = 0; 
    struct info lines[200]; 
    FILE* inFile = NULL; 
    inFile = findFile(argv[1]); 
    readFile(inFile,lines); 
    fclose(inFile); 
    return 0; 
} 
FILE* findFile(char fileName[]){ 

    FILE* fileIn = NULL; 
    fileIn = fopen(fileName, "r"); 

    while(fileIn == NULL){ 
    printf("That file does not exist or was unable to be found, please enter another\n"); 
    fgets(fileName,100,stdin); 
    fopen(fileName, "r"); 
    } 

    return fileIn; 
} 

void readFile(FILE* toRead, struct info lines[]){ 
    char sentence[200]; 
    int i = 0; 

    while(!feof(toRead)){ 
     fgets(sentence,300,toRead); //Would this be better than fread()? 
     strcpy(lines[i].lines,sentence); 
     printf("%s",lines[i].lines);  //*This is a debug statement. The lines are read properly except the last line is printed twice. not sure why. 
     i++; 
    } 
} 

編集:文字列配列の代わりにポインタを使用する方が良いでしょうか?

答えて

0
fgets(fileName,100,stdin); 
fopen(fileName, "r"); 

fgetsfopenを使用する前にそれを削除し、末尾の改行が含まれています

char *ptr; 
fgets(fileName,100,stdin); 
if (ptr = strchr(filename, '\n') { 
    *ptr = '\0'; 
} 

はまた、あなたが

fileIn = fopen(fileName, "r"); 

代わりの

fopen(fileName, "r"); 
+0

これに加えて、lines配列の 'struct info'の最後にスペースを割り当てません。 'struct info'の配列を宣言するのではなく、' struct info * 'の配列を宣言し、構造体のサイズを超えてスペースを割り当てて、行の文字列を格納する必要があります。例えば。 'struct info * lines [200];'と 'lines [i] = malloc(sizeof(struct info)+ 100) –

1

Sを必要とすることに注意してくださいo少し曖昧ですが質問に答えようとします。 あなたの質問に関しては、 "Info structs"でいっぱいのファイルを読み込もうとしているのか、text/binファイルから構造体に 'x'量のcharを読み込もうとしているのか分かりません。

最初に私があなたのコードで見る一般的なこと。

  • この機能FILE* findFile(char fileName[])は少し不要です。私が見る限り、ファイルが存在するかどうかを確認しようとしています。これを行うには、 "acces()"を使用します。この関数は、呼び出したプロセスがファイルパス名にアクセスするかどうかをチェックします。実装はIf(access(pathOfFile, F_OK) == 0){}のようになります。詳細はhttps://linux.die.net/man/2/accessを参照してください。
  • コードスニペットに続いてchar sentence[200];fgets(sentence,300,toRead); //Would this be better than fread()?のサイズの文字配列を宣言してから、そのファイルから読み込もうとしますが、読み込もうとする量は300です。あなたの配列。ヒント "man"ページを読むには、Linux端末 "man fread"に入力してfreadを他の関数名に置き換えます。 「アクセス」と似ています。

ここでは2つのポイントをクリアしたいと思います。 あなたの質問については、ファイルから構造体を読み取る例を挙げます。あなたの質問So I am trying to write a program that will read a file and store into a struct, such that it can be later on passed to other functions for manipulation and whatnot.は、ファイルや構造体からちょうどいくつかの文字を読み込もうとしているときに少し曖昧です。そこで、INFO構造体のファイルから構造体を配列に読み込んで操作するプログラムを書き直しました。

コードスニペットは、構造体、私は、これは、少なくとも、あなたのコードであなたの問題を解決するのに役立ちます願ってい

#include <stdio.h> 
 
#include <unistd.h> 
 

 
#define file_name "Foo_File" 
 

 
struct INFO{ 
 
    char ar[200]; 
 
} 
 

 
int main (int argc, char * argv[]){ 
 
    if(access(file_name, F_OK) != 0{ 
 
    printf("File does not exist"); 
 
    return -1;//Exit program with -1, error 
 
    } 
 
    int structs = get_Amount_Of_Structs(file_name); 
 
    INFO info_array[structs];//Make an array of info structs, same amount of structs as in file 
 
    for(int i = 0; i < structs; i++){ 
 
    readDataStructFromFile(file_name, info_array, i); 
 
    } 
 
    return 0; 
 
} 
 

 
/* 
 
    This function will calculate the total byte size of the  file by diving this through the byte size of one struct  you are able to know the amount of structs. 
 
*/ 
 
int get_Amount_Of_Structs(char* filename) 
 
{ 
 
\t FILE* fp = fopen(filename, "r"); 
 
\t if(fp == NULL){//If File pointer is null opening failed 
 
\t \t return -1; 
 
    } 
 
\t fseek(fp, 0L, SEEK_END); 
 
\t int amountOfStructs = ftell(fp)/sizeof(INFO); 
 
\t fseek(fp, 0L, SEEK_SET); 
 
\t fclose(fp); 
 
\t return amountOfStructs; 
 
} 
 

 
/* 
 
    This function reads a struct from a file at the position  "pos" by slight modification you can make it read the  whole file into a pointer that points to an array of INFO structs 
 
*/ 
 
int readDataStructFromFile(char* filename, INFO* data, int pos) 
 
{ 
 
\t FILE* fp = fopen(filename, "r"); 
 
\t if(fp == NULL) 
 
\t { 
 
\t \t fclose(fp); 
 
\t \t return -1; 
 
\t } 
 
\t fseek(fp, (pos * sizeof(INFO)), SEEK_SET); 
 
\t int rv = fread(data, sizeof(INFO), 1, fp); 
 
\t fclose(fp); 
 
\t if(rv == 1) 
 
\t { 
 
\t \t return 0; 
 
\t } 
 
\t return -1; 
 
}

の配列に構造のファイルからデータを読み込みます。 "fgets vs fread"に関する質問はもう別の人によって解決されています。 願い私はあなたの質問にもっと完全な答えを与えることができますが、そこには少し曖昧です。

p.s.コミュニティの誰かが自分のコードや説明に誤りがある場合は、編集したり、間違いを指摘してください。私は学生ですので、常にいくつかのことを学ぶことが残っています。

+0

読み込みしようとしているファイルはちょうど保育園です。本質的に、私は童謡を読んで、すべての文章や言葉を数えたいと思う –

関連する問題