C语言 文件程序设计题

文件程序设计题:请调用fputs函数,把10个字符串输出到文件中;再从此文件中读入这10个字符串放在一个字符串数组中;最后把字符串数组中的字符串输出到终端屏幕,以检验所有... 文件程序设计题:
请调用fputs函数,把10个字符串输出到文件中;再从此文件中读入这10个字符串放在一个字符串数组中;最后把字符串数组中的字符串输出到终端屏幕,以检验所有操作是否正确。
展开
 我来答
passion_wu128
2011-01-04 · TA获得超过1168个赞
知道小有建树答主
回答量:370
采纳率:0%
帮助的人:561万
展开全部
#include<stdio.h>
#include<stdlib.h>
void main()
{
char* sample[10]={"sampleStr1","sampleStr2","sampleStr3","sampleStr4","sampleStr5",
"sampleStr6","sampleStr7","sampleStr8","sampleStr9","sampleStr10",};
char readStr[10][20];
FILE *fp;
int i;
if((fp=fopen("test.txt","w"))==NULL)
{
printf("canot open the file!");
exit(0);
}
//写入字符串
for(i=0;i<10;i++)
{
if(fputs(sample[i],fp)==EOF)
{
printf("fputs error!\n");
return;
}
fputs("\n",fp);
}
fclose(fp);
if((fp=fopen("test.txt","r"))==NULL)
{
printf("canot open the file!");
exit(1);
}
//读出字符串
for(i=0;i<10;i++)
{
if(fgets(readStr[i],20,fp)==NULL)
{
printf( "fgets error\n" );
return;
}
}
for(i=0;i<10;i++)
printf("%s",readStr[i]);
fclose(fp);
}
regret4hxp2
2011-01-04 · TA获得超过682个赞
知道小有建树答主
回答量:479
采纳率:100%
帮助的人:259万
展开全部
#include "stdio.h"
#include "string.h"
int main()
{
FILE* fp = fopen("temp.txt","w"); //以写方式创建文件
char tmp[11] = "abcdefghij";
char tmp2[11] = {0};

fputs( tmp, fp ); //输出到文件
fclose(fp);

FILE* fp2 = fopen("temp.txt","r");//以读方式打开之前的文件
fgets( tmp2, 12,fp2); //从文件中读入
fclose(fp2);

printf("%s", tmp2); //输出到屏幕终端
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
pdmsbolt
2011-01-05 · 超过10用户采纳过TA的回答
知道答主
回答量:20
采纳率:0%
帮助的人:0
展开全部
#include<stdio.h>
#include<stdlib.h>
#include <string.h>

#define BUFFERSIZE 256

char filename[BUFFERSIZE] = "d:\\test.txt"; //文件名
int main()
{
int strlength = 0;
printf("输入字符串数:");
scanf("%d",&strlength);
char **strArray = NULL;
strArray = (char **)malloc(strlength * sizeof(char *)); //分配空间
if (strArray == NULL)
{
printf("array error!\n");
return 0;
}
for (int i = 0; i < strlength; ++i)
{
strArray[i] = NULL;
strArray[i] = (char*)malloc(BUFFERSIZE * sizeof(char));
if (strArray == NULL)
{
printf("array error!\n");
return 0;
}
}

int i = 0;
while(i < strlength) //输入字符串
{
scanf("%s",strArray[i]);
++i;
}

FILE *pfile = NULL;
pfile = fopen(filename,"w+");
if (pfile == NULL)
{
printf("can not open the file!\n");
return 0;
}
for (int i = 0; i < strlength; ++i) //写入到文件
{
fputs(strArray[i],pfile);
fputs("\n",pfile);
}
fclose(pfile);
pfile = NULL;
for (int i = 0; i < strlength; ++i)
{
memset(strArray[i],0,BUFFERSIZE);
}
pfile = fopen(filename,"r+");
if (pfile == NULL)
{
printf("can not open the file!\n");
return 0;
}
for (int i = 0; i < strlength; ++i)
{
fgets(strArray[i],BUFFERSIZE,pfile);
printf("%2d\t%20s\n",i,strArray[i]);
}

for (int i = 0; i < strlength; ++i) //释放空间
{
free(strArray[i]);
}
free(strArray);

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
never715
2011-01-04 · TA获得超过942个赞
知道小有建树答主
回答量:1010
采纳率:84%
帮助的人:484万
展开全部
建议你还是先看看书,再试着自己写一个,字符串改由用户输入,其实文件操作也是很重要的...
这是我写的(选择读和写):
#include <stdafx.h>
#include <stdlib.h>
#define N 10
void write_file();
void read_file();

int main(int argc,char *argv[])
{
int choice=0;
do
{
printf("1-----写入文件\n");
printf("2-----读取文件\n请选择(输入0退出):");
scanf("%d",&choice);
if(choice==1)
write_file();
else if(choice==2)
read_file();
}while(choice);
return 0;
}

void write_file()
{
FILE *fp;
char ch[N][15]={"VISUAL C++","C语言"};//十个字符串,每串不超过15个字符
int n;
if((fp=fopen("c:\\test.txt","w"))==NULL)//打开文件失败
{
printf("打开文件失败\n");
getchar();
exit(0);
}
for(n=0;n<N;n++)
{
fputs(ch[n],fp);
fputs("\n",fp);
}
printf("文件写入成功!\n");
fclose(fp);
}

void read_file()
{
FILE *fp;
char ch[N][15];
int n;
if((fp=fopen("c:\\test.txt","r"))==NULL)//打开文件失败
{
printf("打开文件失败\n");
getchar();
exit(0);
}
for(n=0;n<N;n++)
{
fgets(ch[n],15,fp);
printf("%s",ch[n]);
}
fclose(fp);
}
希望对你有所帮助!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
听不清啊
高粉答主

推荐于2017-11-01 · 说的都是干货,快来关注
知道顶级答主
回答量:7.8万
采纳率:89%
帮助的人:2亿
展开全部
#include <stdio.h>
main()
{int i;
 char s[81],st[10][81];
 FILE *fp;
 fp=fopen("d:\\0.txt","w");
 for(i=0;i<10;i++)
 {gets(s);
  fputs(s,fp);
  fputc('\n',fp);
 }
 fclose(fp);
 fp=fopen("d:\\0.txt","r");
 for(i=0;i<10;i++)
 {fgets(st[i],81,fp);
  printf("%s",st[i]);
  }
 fclose(fp);
 return 0;
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式