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/


原创粉丝点击