用C语言读文章。

qintian1114 2005-04-01 01:41:43
我用的是 turbo c++ 3.0

我要从文件中读一篇文章到一个字符串数组 s[100][100] 中,

例如文件的内容是 :

Before I started at university,I took my first job as a waitress in a nice restaurant.At the night before my first day.I was too excited to go to sleep and as a result,I got up late in the morning.I threw on my clothes and rushed over to the restaurant.

In a great hurry,I didn't hear clearly the head waiter's instruction that we should go into the kitchen through one door and out from the other.So when I took two plates of eggs and bacon and an orange juice out to the restaurant,I went straight towards the wrong door and collided with another waiter coming in!

Worst of all was that I wore a pair of smart shoes but with high heels! A few hours later I was in agony yet had no time to change them. By the time we finished serving dinner at about 10:30 I was completely exhausted. Nevertheless, I learnt something through my experience.



我要让文件的第1行保存在 c[0] 中,第2行保存在 c[1] 中,……

第4行是空的,我也让它保存在 c[3] 中,也就是 c[3]="";

请问具体程序应该怎么编?只能用C语言遍,注意我用的是 turbo c++ 3.0

请各位大哥告诉我,谢谢!


...全文
269 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjs__2000 2005-04-07
  • 打赏
  • 举报
回复
#include <stdio.h>
void main()
{
int i,j=0;
int FLAG=0;
char * p;
char c[20][80] ;
int maxline =0;
FILE *fp;
p=(char *)malloc(sizeof(char));
if((fp = fopen("in.txt","r"))==NULL)
{
printf("打开文件错误!\n");
}
for(i=0;i<20;i++)
{
if(1==FLAG) break; /*退出循环*/
for(j=0;j<80;j++)
{
*p=fgetc(fp);
c[i][j]=*p;
if(*p=='\n') break; /*退出循环*/
if(EOF==*p)
{
FLAG=1;
break; /*退出循环*/
}
}
}
fclose(fp);

/*下面是输出调试部分*/
for(i=0;i<20;i++)
{

for(j=0;j<80;j++)
{
printf("%c",c[i][j]);
if(c[i][j]=='\n') break;
if(c[i][j]==EOF) break;
}
if(c[i][j]==EOF) break;
}
getch();
}
richardgates 2005-04-07
  • 打赏
  • 举报
回复
我觉得用二进制流文件做好一点
qingyuan18 2005-04-07
  • 打赏
  • 举报
回复
不难啊
void main()
{
char * p;
FILE *fp;
char c[20][80] ="";
int maxline =0;
if(fp = fopen("in.txt","r")==Null)
{
printf("打开文件错误!\n");
}
while(fgets(c[maxline],80,fp)!=null)
{
p = strchr(c[maxline],'\n');
if(p) *p =0;
maxline++;
}

}
cppprogramlover 2005-04-07
  • 打赏
  • 举报
回复
竟然用起二维数组来!!佩服!!!
xjp6688 2005-04-07
  • 打赏
  • 举报
回复
用流控制最省事了!
suyun9329 2005-04-07
  • 打赏
  • 举报
回复
cppprogramlover 2005-04-07
  • 打赏
  • 举报
回复
用fcb块算了,自己解决
pcboyxhy 2005-04-01
  • 打赏
  • 举报
回复
int i=0;
char s[100][100];
FILE *fp;
fp=fopen("test.txt", "r");
if(!fp) exit(1);
while( fgets(s[i], 100, fp) ) ++i;
...............
fclose(fp);
qintian1114 2005-04-01
  • 打赏
  • 举报
回复
这个问题应该不难吧?有人知道吗?
1、清晰第一 清晰性是易于维护、易于重构的程序必需具备的特征。代码首先是给人的,好的代码应当可以像文章一样发声朗诵出来。 目前软件维护期成本占整个生命周期成本的40%~90%。根据业界经验,维护期变更代码的成本,小型系统是开发期的5倍,大型系统(100万行代码以上)可以达到100倍。业界的调查指出,开发组平均大约一半的人力用于弥补过去的错误,而不是添加新的功能来帮助公司提高竞争力。 一般情况下,代码的可阅性高于性能,只有确定性能是瓶颈时,才应该主动优化。 2、简洁为美 简洁就是易于理解并且易于实现。代码越长越难以看懂,也就越容易在修改时引入错误。写的代码越多,意味着出错的地方越多,也就意味着代码的可靠性越低。因此,我们提倡大家通过编写简洁明了的代码来提升代码可靠性。 废弃的代码(没有被调用的函数和全局变量)要及时清除,重复代码应该尽可能提炼成函数。 3、选择合适的风格,与代码原有风格保持一致 产品所有人共同分享同一种风格所带来的好处,远远超出为了统一而付出的代价。在公司已有编码规范的指导下,审慎地编排代码以使代码尽可能清晰,是一项非常重要的技能。 如果重构/ / 修改其他风格的代码时,比较明智的做法是根据 现有 代码 的 现有风格继续编写代码,或者使用格式转换工具进行转换成公司内部风格。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧