###前提・実現したいこと
プログラミング学習中です。参考書でつまづきましたが、問題が解決しないので..
標準入力から読み込んだテキストファイルを1行ごとにtextdata(charへのポインタのポインタ)に与え、1行ずつ表示していくプログラムで、正しく実行させたいです。
###発生している問題・エラーメッセージ
コンソールから例えばabc(Enter)efg(Enter)hij(Enter)[Ctrl+z]と入力してもprintf関数内の改行文字しか吐かない。
実行結果:
(改行)
(改行)
(改行)
欲しい実行結果:
abc
efg
hij
###該当のソースコード
###read_file.c
c
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <assert.h> 5#define ALLOC_SIZE (256) 6 7static char *st_line_buffer = NULL; 8 9static int st_current_buffer_size = 0; 10 11static int st_current_used_size = 0; 12 13static void add_character(int ch) 14{ 15 assert(st_current_buffer_size >= st_current_used_size ); 16 17 if( st_current_buffer_size == st_current_used_size ){ 18 st_line_buffer = realloc(st_line_buffer,(st_current_buffer_size + ALLOC_SIZE) 19 * sizeof(char)); 20 st_current_buffer_size += ALLOC_SIZE; 21 } 22 23 st_line_buffer[st_current_used_size] = ch; 24 st_current_used_size++; 25 26} 27 28char *read_line(FILE *fp) 29{ 30 int ch; 31 char *ret; 32 33 st_current_used_size = 0; 34 35 while((ch = getc(fp))!=EOF){ 36 if( ch == '\n') { 37 add_character('\0'); 38 break; 39 } 40 add_character('\0'); 41 } 42 43 if( ch == EOF ){ 44 if(st_current_used_size > 0){ 45 add_character('\0'); //最後の行に改行がなかった場合 46 }else{ 47 return NULL; 48 } 49 } 50 51 ret = malloc(sizeof(char) * st_current_used_size); 52 strcpy(ret,st_line_buffer); 53 54 return ret; 55} 56 57void free_buffer(void) 58{ 59 free(st_line_buffer); 60 st_line_buffer = NULL; 61 st_current_buffer_size = 0; 62 st_current_used_size = 0; 63}
###read_line.h
C
1#ifndef READ_LINE_H_INCLUDED 2#define READ_LINE_H_INCLUDED 3 4#include <stdio.h> 5 6 char *read_line(FILE *fp); 7 void free_buffer(void); 8 9#endif /* READ_LINE_H_INCLUDED */
###read_file.c
C
1#include<stdio.h> 2#include<stdlib.h> 3#include<assert.h> 4 5#define ALLOC_SIZE (256) 6 7#include "read_line.h" 8 9char **add_line(char **text_data, char *line, int *line_alloc_num, int *line_num) 10{ 11 assert(*line_alloc_num >= *line_num ); 12 if(*line_alloc_num == *line_num ){ 13 text_data = realloc( text_data,(*line_alloc_num + ALLOC_SIZE) * sizeof(char *)); 14 *line_alloc_num += ALLOC_SIZE; 15 } 16 text_data[*line_num] = line; 17 (*line_num)++; 18 19 return text_data; 20} 21 22char **read_file(FILE *fp, int *line_num_p) 23{ 24 char **text_data = NULL; 25 int line_num = 0; 26 int line_alloc_num = 0; 27 char *line; 28 29 while(( line = read_line(fp) ) != NULL ){ 30 text_data = add_line(text_data, line, &line_alloc_num,&line_num); 31 } 32 /* text_dataを、本当に必要なサイズまで縮める。 */ 33 text_data = realloc( text_data, line_num * sizeof (char *)); 34 *line_num_p = line_num; 35 36 return text_data; 37} 38 39int main(void) 40{ 41 char **text_data; 42 int line_num; 43 int i; 44 45 text_data = read_file(stdin, &line_num); 46 47 for( i = 0; i< line_num; i++ ){ 48 printf("%s\n", text_data[i]); 49 } 50 return 0; 51}
###試したこと
ソースをじっくりと見直し、調べたが分からなかった
###補足情報(言語/FW/ツール等のバージョンなど)
・C言語
・Eclipse4.5
(Cプロジェクト内で開発-read_line.c,read_line.h,read_file.cは同一プロジェクト)
・MinGW GCC
回答1件
あなたの回答
tips
プレビュー