C指针原理(33)-C-文件指针
来源:互联网 发布:java打印一个倒三角形 编辑:程序博客网 时间:2024/05/22 09:50
9、文件指针
dp@dp:~/test1 % vim test2.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv){
int exit_status=EXIT_SUCCESS;
while (*++argv!=NULL)
{
//打开文件,如果出现错误,则显示错误信息
FILE *input=fopen(*argv,"r");
if (input==NULL){
perror(*argv);
exit_status=EXIT_FAILURE;
continue;
}
printf ("\n%s内容如下:\n",*argv);
int ch;
while((ch=fgetc(input))!=EOF){
printf("%c",ch);
}
if (fclose(input)!=0){
perror(*argv);
exit_status=EXIT_FAILURE;
}
}
return exit_status;
}
本博客所有内容是原创,如果转载请注明来源
http://blog.csdn.net/myhaspl/
上面程序使用fgetc读取命令行参数中的文本文件,将它们内容输出
dp@dp:~/test1 % cc test2.c -o mytest
dp@dp:~/test1 % ./mytest test1.c
test1.c内容如下:
#include <stdio.h>
int add(int a,int b);
int main(void){
int (*myfunc)(int a,int b);
myfunc=add;
int x=myfunc(12,36);
printf("%d",x);
return 1;
}
int add(int a,int b){
return a+b;
}
也可一次向命令行指定更多文本文件
dp@dp:~/test1 % ./mytest test1.c test3.c hello.txt
#include <stdio.h>
int add(int a,int b);
int main(void){
int (*myfunc)(int a,int b);
myfunc=add;
int x=myfunc(12,36);
printf("%d",x);
return 1;
}
int add(int a,int b){
return a+b;
}
test3.c内容如下:
#include <stdio.h>
int main(int argc,int **argv){
printf ("%s","abcdefgh"+2);
}
hello.txt内容如下:
你好,各位朋友,很高兴认识大家。
Google是一家美国的跨国科技企业,致力于互联网搜索、云计算、广告技术等领域,开发并提供大量基于互联网的产品与服务,其主要利润来自于AdWords等广告服务。
也可以使用fgets函数来读取
dp@dp:~/test1 % vim test2.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv){
int exit_status=EXIT_SUCCESS;
while (*++argv!=NULL)
{
//打开文件,如果出现错误,则显示错误信息
FILE *input=fopen(*argv,"r");
if (input==NULL){
perror(*argv);
exit_status=EXIT_FAILURE;
continue;
}
printf ("\n%s内容如下:\n",*argv);
char mytext[500];
while(fgets(mytext,500,input)!=NULL){
printf("%s",mytext);
}
if (fclose(input)!=0){
perror(*argv);
exit_status=EXIT_FAILURE;
}
}
return exit_status;
}
dp@dp:~/test1 % cc test2.c -o mytest
dp@dp:~/test1 % ./mytest hello.txt test3.c
hello.txt内容如下:
你好,各位朋友,很高兴认识大家。
Google是一家美国的跨国科技企业,致力于互联网搜索、云计算、广告技术等领域,开发并提供大量基于互联网的产品与服务,其主要利润来自于AdWords等广告服务。
test3.c内容如下:
#include <stdio.h>
int main(int argc,int **argv){
printf ("%s","abcdefgh"+2);
}
dp@dp:~/test1 %
下面的例子展示了从键盘输入文字增加到文本文件后面,如果输入%end%表示输入结束。
dp@dp:~/test1 % vim test5.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv){
int exit_status=EXIT_SUCCESS;
while (*++argv!=NULL)
{
//打开文件,如果出现错误,则显示错误信息
FILE *output=fopen(*argv,"a");
if (output==NULL){
perror(*argv);
exit_status=EXIT_FAILURE;
continue;
}
char mytext[500];
int ch='\n';
while (1){
printf("请输入文字:");
scanf("%s",&mytext);
if (strcmp(mytext,"%end%")!=0){
fputs(mytext,output);
//scanf函数不会读取换行符,因此加上换行符
fputc(ch,output);
}
else break;
}
if (fclose(output)!=0){
perror(*argv);
exit_status=EXIT_FAILURE;
}
}
return exit_status;
}
执行结果如下
dp@dp:~/test1 % cc test2.c -o mytest2
dp@dp:~/test1 % ./mytest2 hello.txt
hello.txt内容如下:
你好,各位朋友,很高兴认识大家。
Google是一家美国的跨国科技企业,致力于互联网搜索、云计算、广告技术等领域,开发并提供大量基于互联网的产品与服务,其主要利润来自于AdWords等广告服务。
dp@dp:~/test1 %
dp@dp:~/test1 % cc test5.c -o mytest5
dp@dp:~/test1 % ./mytest5 hello.txt
请输入文字:你好 ,今天天气如何?
请输入文字:今天天气不错!
请输入文字:谢谢!
请输入文字:%end%
dp@dp:~/test1 % ./mytest2 hello.txt
hello.txt内容如下:
你好,各位朋友,很高兴认识大家。
Google是一家美国的跨国科技企业,致力于互联网搜索、云计算、广告技术等领域,开发并提供大量基于互联网的产品与服务,其主要利润来自于AdWords等广告服务。
你好,今天天气如何?
今天天气不错!
谢谢!
dp@dp:~/test1 %
本博客所有内容是原创,如果转载请注明来源
http://blog.csdn.net/myhaspl/
- C指针原理(33)-C-文件指针
- C指针原理(33)-C指针基础
- C指针原理(1)
- C指针原理(2)
- C指针原理(3)
- C指针原理(4)
- C指针原理(5)
- C指针原理(6)
- C指针原理(8)
- C指针原理(9)
- C指针原理(10)
- C指针原理(11)
- C指针原理(12)
- c二级指针原理
- C指针原理(14)-C指针基础
- C指针原理(15)-C指针基础
- C指针原理(16)-C指针基础
- C指针原理(17)-C指针基础
- Flex外包团队—开发工具:Flex4.6新特性介绍
- JNI 详解,附以前写得一份PPT文档
- 稳压器78系列和79系列含义
- 腾讯面试题:根据上排给出的十个数,在其下排填出对应的十个数。
- 刚刚开通了博客,编程新手入住csdn了
- C指针原理(33)-C-文件指针
- 三角恒等变换
- 继电器与接触器
- 后觉的懒汉
- 你的团队能通过电梯测试吗?
- ruby_汉诺塔
- 剑指Offer之二叉树的深度
- 求一个数组的最长递减子序列
- 集成电路74系列