ページ 11

文字列の逆順での表示

Posted: 2011年6月11日(土) 11:23
by タマ
test.txt内のすべての文字を、行ごとに取得し、それを行単位で逆順に表示するプログラムを作ろうとしています。

コード:

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

    char *lines[100];
    int i=0,j;

char *getline1(void){
    FILE *fp;
    char *string;
	if( !( string = (char *) malloc(sizeof(char)*100))){
		printf("Sorry, the malloc was aborted!\n");
		exit(1);
	}
    if((fp=fopen("test.txt","r"))==NULL){
        printf("ファイルが開けません。\n");
        exit (2);
    }
    while((fgets(string,100,fp))!=NULL){
        lines[i]=string;
        i++;
    }
	for(j=0;j<i;j++){
    printf("%s\n",lines[i-j-1]);
    }
}

int main(void){
    *getline1();
    return 0;
}
ここまで作ったのですが、printfで表示しようとすると空白になってしまいます。
test.txtは、↓の通りです。

コード:

An Introduction to Computers Qn: Why does a bee hum? Ans: Because it 
doesn't know the words! One way of describing a computer is as an 
electric box which humms. This, whilst technically correct, can lead to 
significant amounts of confusion, particularly amongst those who then 
try to program a fridge. A better way is to describe it as:
A device which processes information according to instructions it has 
been given. 
This general definition rules out fridges but is not exhaustive. However 
for our purposes it will do. The instructions you give to the computer 
are often called a program. The business of using a computer is often 
called programming. This is not what most people do with computers. Most 
users do not write programs, instead they talk to programs written by 
other people. We must therefore make a distinction between users and 
programmers. A user has a job which he or she finds easier to do on a 
computer running the appropriate program. A programmer has a masochistic 
desire to tinker with the innards of the machine. One of the golden 
rules is that you never write your own program if there is already one 
available, i.e. a keen desire to process words with a computer should 
not result in you writing a word processor! 

However, because you will often want to do things with computers which 
have not been done before, and further because there are people willing 
to pay you to do it, we are going to learn how to program as well as use 
a computer. 
Before we can look at the fun packed business of programming though it 
is worth looking at some computer terminology:Hardware and Software
If you ever buy a computer you are not just getting a box which humms. 
The box, to be useful, must also have sufficient built in intelligence 
to understand simple commands to do things. At this point we must draw a 
distinction between the software of a computer system and the hardware. 
Hardware is the physical side of the system. Essentially if you can kick 
it, and it stops working when immersed in a bucket of water, it is 
hardware. Hardware is the impressive pile of lights and switches in the 
corner.... 

Re: 文字列の逆順での表示

Posted: 2011年6月11日(土) 11:34
by box
タマ さんが書きました:

コード:

    *getline1();
先頭の * には、どういう意味があるのでしょうか?

Re: 文字列の逆順での表示

Posted: 2011年6月11日(土) 13:17
by タマ

コード:

    *getline1();
プログラムを作る際に、この書き方をするように指定されました。

Re: 文字列の逆順での表示

Posted: 2011年6月11日(土) 16:43
by non
>それを行単位で逆順に表示する

これは、最後の行から出力したいということでしょうか?
それなら、
if( !( string = (char *) malloc(sizeof(char)*100))){
printf("Sorry, the malloc was aborted!\n");
exit(1);
}
をwhileの中のi++;の後にも、入れてみましょう。

Re: 文字列の逆順での表示

Posted: 2011年6月11日(土) 16:47
by non
タマ さんが書きました:

コード:

    *getline1();
プログラムを作る際に、この書き方をするように指定されました。
恐らく、聞き違いでしょう。
プロトタイプの説明として
char *getline(void);
と説明があったのではないでしょうか?
でも、そうすると、この関数名やプロトタイプで、この課題はおかしいと思います。
逆順にするのは1行の中ではないですか?

Re: 文字列の逆順での表示

Posted: 2011年6月11日(土) 18:35
by box
タマ さんが書きました:test.txt内のすべての文字を、行ごとに取得し、それを行単位で逆順に表示するプログラムを作ろうとしています。
stringは1回malloc()しただけですので、固定値です。
それをlines[]に代入していますので、lines[]の要素はすべて同じ値です。
それらを出力すると、ファイルの行数だけ同じ内容を出力します。
空白だけを出力するというのは、テキストファイルの最後に空白行があるためではないでしょうか。