关于C语句的一个文件操作的程序
我写了下面这个程序
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <conio.h>
main()
{
int iFileLen=0,iStrLen=0,iCount;
char pstFilename[80];
char **pstArry;
char string[80];
char ch;
FILE *fp;
clrscr();
printf("Please enter the file name of you will sort:");
scanf("%s",pstFilename);
if((fp=fopen(pstFilename,"r"))==NULL)
{
printf("\n%s cannot open!",pstFilename);
exit(1);
}
/* compulate the number of words */
while(!feof(fp))
{
ch=fgetc(fp);
if(ch!=32&& /*not space*/
ch!='\n'&& /*not enter*/
ch!='\t') /*not table*/
iFileLen++;
while(ch!=32&&ch!='\n'&&ch!='\t'&&ch!=EOF)
ch=fgetc(fp);
}
pstArry=(char**)malloc(iFileLen*sizeof(char *));
iFileLen=0;
close(fp);
if((fp=fopen(pstFilename,"r"))==NULL)
{
printf("%s cannot open!\n");
exit(1);
}
while(!feof(fp))
{
ch=fgetc(fp);
if(ch!=32&&
ch!='\n'&&
ch!='\t'&&
ch!=EOF)
*(string+iStrLen++)=ch;
else if(ch!=EOF)
{
*(string+iStrLen)=NULL;
*(pstArry+iFileLen)=(char*)malloc(sizeof(string));
*(pstArry+iFileLen)=string; printf("%s\n",*(pstArry+iFileLen)); /*输出结果1*/
iFileLen++;
iStrLen=0;
}
}
*(pstArry+iFileLen)=NULL;
for(iCount=0;iCount<iFileLen;iCount++)
{
printf("\n%s",*(pstArry+iCount)); /*输出结果2*/
}
close(fp);
free(pstArry);
getch();
}
运行时输出:c:\string.txt
运行结果:
(输出结果1)
This
software
signed
by
Mr.
Huang
This
is
a
application
program
for
logically
sort
CET4
words!
(输出结果2)
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
words!lly
为什么两次的输出结果不同?
其中string.txt的内容如下:
This software signed by Mr. Huang
This is a application program for logically sort CET4 words!
万分感谢!
问题点数:39、回复次数:5Top
1 楼huang_yi_cn(蓝色的云)回复于 2001-11-21 21:49:25 得分 0
为何还没有人理我
Top
2 楼xilimi10(禾月轩主)回复于 2001-11-21 22:47:40 得分 39
好长的代码呀,没时间看,有空再给你改吧!Top
3 楼huang_yi_cn(蓝色的云)回复于 2001-11-23 18:45:15 得分 0
这已经解决了,这回便宜你了,楼上的同志。Top
4 楼xilimi10(禾月轩主)回复于 2001-11-23 19:52:26 得分 0
谢谢,刚想看看,可你也没给分呀!Top
5 楼huang_yi_cn(蓝色的云)回复于 2001-11-30 18:17:46 得分 0
现在给了,以前不知道什么原因,总是加不了分。Top




