2016-04-10 11 views
0

私は現在、ファイルから文字列を読み込んで2次元配列に格納するプログラムに取り組んでいます。しかし、配列の内容を出力しようとすると、毎回ランダムな文字が得られます。私は私が間違って私の2次元配列に文字列を挿入していたためであると疑われる文字列を配列に出力するときに奇妙な文字がありますか?

#include <stddef.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main(){ 
    FILE* file_pointer; 
    char user_input[80]; 
    char line[81]; 
    char all_lines_array[100][81]; 
    int total_lines = 0; 
    while (1){ 
     printf("Please enter a command: "); 
     scanf("%s", &user_input); 
     if (strstr(user_input, "read") != NULL){ 
      file_pointer = fopen("C:\\Users\\Tyler\\Desktop\\Hello.txt","r"); 
      while (fgets(line, 100, file_pointer)) { 
       line[strlen(line)+1] = "\0"; 
       *all_lines_array[total_lines] = line; //My guess is this is wrong 
       total_lines++; 
     } 
     fclose(file_pointer); 
    } 
} 
return 0; 
} 

、私はそれが私が間違ってやっている何であるか見当がつかない:ここに私のコードです。私はファイル内に最大100行しか存在できないように番号を設定しており、各行の長さは80文字(末尾には"\0")しかありません。

John Doe 1221 Washington St. 1234567 
Jane Doe 1233 Washington St. 1234568 
Cain Doe 1234 Washington St. 1234569 

答えて

0

関数fgetsは100個の文字を読んでいるが、あなたの静的割り当てられた変数char line[81]があるだけで81文字のためにメモリを割り当てる:

は、ここに私の入力ファイルです。代わりに、エラーの一部を手伝ってくれましたかもしれない[私はいつもやってお勧めします] -Wall、でコンパイルする場合、いくつかの文がフラグを立て=オペレータの使用がありstrcpy

+0

ありがとうは、strcpyのは、働いていました。 strcpyは何をしているのでしょうか? – MistFTW

+1

私はCをプログラムしなければならなかったが、私はいつも[cppreference](http://en.cppreference.com/w/)や[cplusplus](http://www.cplusplus.com/)のstrcpyのような機能を調べた。あなたは例を含めて必要なすべての情報を手に入れます! – Maxincredible52

+1

@MistFTW 1つのC文字列/配列から別の文字列にデータをコピーします。 ''\ 0''のターミネータ文字がない場合、プロセス内のメモリを破壊する可能性があります。あなたが本当に盲目的に信頼するのではなく、答えを評価するためにドキュメントを本当に読まなければならない理由の1つです! (これは致命的な欠陥ではなく、インターネット上のランダムなコードを 'その言葉'とするのは賢明ではありません!) – user268396

2

の 。

ここでコメントで注釈が付け修正版、[無償スタイルのクリーンアップをご容赦ください]です。

#include <stddef.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int 
main() 
{ 
    FILE *file_pointer; 
    char user_input[80]; 
    char line[81]; 
    char all_lines_array[100][81]; 
    int total_lines = 0; 

    while (1) { 
     printf("Please enter a command: "); 

     // compiler flagged this with a warning 
#if 0 
     scanf("%s", &user_input); 
#else 
     scanf("%s", user_input); 
#endif 

     if (strstr(user_input, "read") != NULL) { 
#ifndef CRAIG 
      file_pointer = fopen("C:\\Users\\Tyler\\Desktop\\Hello.txt", "r"); 
#else 
      file_pointer = fopen("input.txt", "r"); 
#endif 
      if (file_pointer == NULL) { 
       printf("file not found\n"); 
       continue; 
      } 

      // NOTE: using sizeof here is better as 100 was specified but 
      // "line" was only 81 
      while (fgets(line, sizeof(line), file_pointer)) { 
       // NOTE: I presume this is to strip the newline 
       // in the original form, it would add garbage chars to the end 
       // [because of "\0" instead of '\0'] 
#if 0 
       line[strlen(line) + 1] = "\0"; 
#else 
       line[strlen(line) - 1] = 0; 
#endif 

       // NOTE: the compiler flagged this as well 
#if 0 
       *all_lines_array[total_lines] = line; // My guess is this is wrong 
#else 
       strcpy(all_lines_array[total_lines],line); 
#endif 
       total_lines++; 
      } 

      fclose(file_pointer); 
     } 
    } 

    return 0; 
} 
関連する問題