c语言如何写入文件 编辑file写入不能正常执行问题

查看:1399|回复:4
第一个文件main()如下:复制内容到剪贴板代码:#include &stdio.h&
#include &hotel.h&
int main (void)
double hotel_
while ((code = menu ()) != QUIT)
switch (code)
case 1:hotel_rate = HOTEL1;
case 2:hotel_rate = HOTEL2;
case 3:hotel_rate = HOTEL3;
case 4:hotel_rate = HOTEL4;
default:hotel_rate = 0.0;
printf(&Oops!\n&);
nights = getnights ();
showprice (hotel_rate,nights);
printf(&Thank you and goodbye.&);
}第二个函数模块为:复制内容到剪贴板代码:#include &stdio.h&
#include &hotel.h&
int menu (void)
int code ,
printf(&\n%s%s\n&,STARS,STARS);
printf(&Enter the number of the desired hotel: \n&);
printf(&1) Fairfield Arms 2) Hotel Olympic\n&);
printf(&3) Chertworthy Plaze 4) The Stockton\n&);
printf(&5) quit\n&);
printf(&%s%s\n&,STARS,STARS);
while ((status = scanf(&%d&,&code)) != 1 ||
(code & 1 || code & 5))
if (status != 1)
scanf(&%*s&);
printf(&Enter an integer from 1 to 5,please.\n&);
int getnights (void)
printf(&How many nights are needed? &);
while (scanf(&%d&,&nights) != 1)
scanf(&%*s&);
printf(&Please enter an integer,such as 2.\n&);
void showprice (double rate,int nights)
double total = 0.0;
double factor = 1.0;
for (n = 1;n &=n++,factor *= DISCOUNT)
total += rate *
printf(&The total cost will be $%0.2f.\n&,total);
}第三个头文件hotel.h为:复制内容到剪贴板代码:#define QUIT 5
#define HOTEL1 80.00
#define HOTEL2 125.00
#define HOTEL3 155.00
#define HOTEL4 200.00
#define DISCOUNT 0.95
#define STARS &************************************&
int menu (void);
int getnights (void);
void showprice (double, int);代码在codeblocks编译结果为:
(21.33 KB)
版规,发帖可获2无忧币
本帖最后由 wwwcs59 于
12:56 编辑
你的截图显示编译无问题,至于运行错误,自己调试跟踪,找找吧.
我用vs2008运行没问题啊,少年!
版规,回帖可获2无忧币
引用:原帖由 zsy012 于
13:39 发表
我用vs2008运行没问题啊,少年! 有一点问题的!当时我传的时候没把运行结果传上去!第二个函数模块的menu里面最后那个
return 0;改成&&return code;就完美了!!!
都是怪我当初传的时候没注意!
版规,回帖可获2无忧币
引用:原帖由 wd 于
21:44 发表
有一点问题的!当时我传的时候没把运行结果传上去!第二个函数模块的menu里面最后那个
return 0;改成&&return code;就完美了!!!
都是怪我当初传的时候没注意! ... 不过,谢谢了!!!
版规,回帖可获2无忧币C语言 编辑file写入不能正常执行问题_百度知道
提问者采纳
输入的是c:\encrypted.txt
c:\key.txt,再输入过程中不需要两个反斜杠,如果是在代码中则需要反斜杠,还有问题的话请提问,能否把代码上传?方便调试。
您好,提问字数有限制,我只能用图片信息上传,并且追问字数也有限制,不能复制给您,能否通过别的方式给您,谢谢您在这么晚的时间回复我,按照您的方式,依然不能解决问题,问题依然如原题。
你能否加我,我名字,然后把文件发我。
。。。知道用的不多, 不知道怎么加人,,我的qq,现在方便加吗
提问者评价
好人一生平安
来自团队:
其他类似问题
为您推荐:
c语言的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁vs2010 上编写的c语言生成不了对应的exe文件?
[问题点数:20分,结帖人rjzou2006]
vs2010 上编写的c语言生成不了对应的exe文件?
[问题点数:20分,结帖人rjzou2006]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2010年12月 C/C++大版内专家分月排行榜第三
2013年 总版技术专家分年内排行榜第三
2012年 总版技术专家分年内排行榜第七
本帖子已过去太久远了,不再提供回复功能。C++编译环境下 运行C语言 要头文件怎么写 记不清么_百度知道
C++编译环境下 运行C语言 要头文件怎么写 记不清么
提问者采纳
一般c与c++兼容啊
改头文件就行
c++#include&iostream.h&c
#include&stdio.h&
其他的基本一样
再程序里面
把输入输出语句都改 了
其他类似问题
为您推荐:
头文件的相关知识
其他4条回答
比如 c里的&stdio.h& 在C++里写成&cstdio&在标准C++里的头文件都是没有.h的,在C++中使用C头文件只需在前面加个c,同时去掉.h
系统自带的 用尖括号&&自己写的 用双引号 “”当然都用双引号“”也可以都用尖括号&&好像不行C++的 一般不写.h使用C头文件只需在前面加个c,同时去掉.h
#include&iostream.h&
extern &C&{#include &cExample.h&}
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁1969人阅读
《C语言入门经典 第四版 霍顿》 P470
// 从文本文件中读出一个字符串
#include &stdio.h&
#include &stdlib.h&
// #include &stdbool.h&
// 编译器报错,没有这个文件 据说是bool 类型变量定义的头文件
const int LENGTH = 80;
int main(void)
char *proverbs[]=
&Many a mickle makes a muckle.\n&,
&Too many cooks spoil the broth.\n&,
&He who laughs last didn't get the joke in the first place.\n&
char more[LENGTH];
FILE *pfile = NULL;
char *filename = &c:\\myfile.txt&;
// if(!(pfile = fopen(filename,&w&))) //
因为没有stdbool.h头文件,所以要这句拆开写
pfile = fopen(filename,&w&);
if(pfile==NULL)
printf(&Error opening %s for writing. Program terminated.&,filename);
int count = sizeof proverbs/sizeof proverbs[0];
// 难道proverbs每个元素都等长?显然不是,难道他们的分配的空间做了对齐处理了?
for(int i=0;i&i++)
fputs(proverbs[i],pfile);
// 从pfile 指向的文件重读取字符串 到 proverbs
fclose(pfile);
if(!(pfile = fopen(filename,&a&))) // 以追加的方式打开
pfile = fopen(filename,&a&);
if(pfile==NULL)
printf(&Error opening %s for writing. Program terminated.&,filename);
printf(&Enter proverbs of less than 80 characters or press Enter to end:\n&);
while(true)
fgets(more,LENGTH,stdin); //读取一条谚语从 键盘 到 more
if(more[0]=='\n')
// 如果是空行,则结束读取操作。
fputs(more,pfile); // 输出more 到 pfile
fclose(pfile);
// if(!(pfile = fopen(filename,&r&))) // 以只读的方式打开文件
pfile = fopen(filename,&r&);
if(pfile==NULL)
printf(&Error opening %s for reading. Program terminated.&,filename);
printf(&The proverbs in the file are :\n\n&);
while(fgets(more,LENGTH,pfile))// 读取一条谚语:从pfile 到 more,(没有遇到EOF ?)
printf(&%s&,more);
fclose(pfile);
// remove(filename);
char readstring[LENGTH];
pfile = fopen(filename,&r&);
fgets(readstring,LENGTH,pfile);
// 读取文件的第一行
printf(&%s&,readstring);
fgets(readstring,LENGTH,pfile); // 读取文件的第二行
printf(&%s&,readstring);
fgets(readstring,LENGTH,pfile); // 读取文件的第三行
printf(&%s&,readstring);
fgets(readstring,LENGTH,pfile);
// 读取文件的第四行 ,文件只有四行,读完了
printf(&%s&,readstring);
fgets(readstring,LENGTH,pfile); // 读取文件的第五行 ,没有第五行了,测试表明还是读取的第四行
printf(&%s&,readstring);
fgets(readstring,LENGTH,pfile); // 读取文件的第六行 ,没有第六行了,测试表明还是读取的第四行
printf(&%s&,readstring);
fclose(pfile);
// 关闭文件指针
输出结果:
Enter proverbs of less than 80 characters or press Enter to end:
The proverbs in the file are :
Many a mickle makes a muckle.
Too many cooks spoil the broth.
He who laughs last didn't get the joke in the first place.
test test test
Many a mickle makes a muckle.
Too many cooks spoil the broth.
He who laughs last didn't get the joke in the first place.
test test test
test test test
test test test
&1) fgets(readstring,LENGTH,pfile); 每执行一次,刚好读取一行,问题是被读取的文件在被关闭之前,是怎么知道移到下一行的?
&&&&&&&& 难道是文件指针移动了?
2) 文件读了4行,已经读完了,再次执行fgets(readstring,LENGTH,pfile);都是输出文件的最后一行,可能文件指针在被关闭前,
&&&&&&& 指针总之指在每行的开始?
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:175358次
积分:3169
积分:3169
排名:第5290名
原创:137篇
转载:84篇
评论:107条
(1)(30)(40)(21)(7)(2)(1)(3)(6)(12)(37)(13)(1)(1)(16)(5)(14)(2)(7)(2)}

我要回帖

更多关于 c语言将数组写入文件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信