点击(此处)折叠或打开
- [10:30:09 @cfiles]$ cat test8.c
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXLINE 100
- int main()
- {
- FILE *fp;
- char arr[MAXLINE+1];
- if ((fp = fopen ("MYFILE", "r")) == NULL)
- {
- perror ("File open error!\n");
- exit (1);
- }
- while ((fgets (arr, MAXLINE, fp)) != NULL)
- {
- fputs (arr, stdout);
- }
- return 0;
- }
- [10:30:35 @cfiles]$ gcc test8.c
- [10:30:40 @cfiles]$ ./a.out
- abcdef
- sdkpn
- love
- [10:30:52 @cfiles]$ cat MYFILE
- abcdef
- sdkpn
- love
点击(此处)折叠或打开
- [omcbo@DGSERNMC111 C]$ cat GetFileName.cpp
- #include <iostream>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- #define OFile file
- #define MAXLINE 100
- int main()
- {
- FILE *fp;
- char arr[MAXLINE+1];
- int i=0;
- int j=0;
- struct node{
- char str[MAXLINE+1];
- }data[10];
- if(access("file",F_OK) != 0)
- {
- cout<<"File do not exist"<<endl;
- exit (1);
- }
- if((fp=fopen("file","r")) == NULL )
- {
- cout<<"file can not be read"<<endl;
- exit (2);
- }
- while((fgets(arr,MAXLINE,fp))!=NULL)
- {
- if(arr[strlen(arr)-1]=='\n')
- arr[strlen(arr)-1]=0;
- if(arr[strlen(arr)-1]=='\r')
- arr[strlen(arr)-1]=0;
- strcpy(data[i].str,arr);
- i++;
- }
- for (j=0;j<=i;j++)
- {
- cout<<data[j].str<<endl;
- }
- return 0;
- }
- g++ GetFileName.cpp -o GetFileName
- [omcbo@DGSERNMC111 C]$ ./GetFileName
- PROC_ALEX_CELL_60_01
- PROC_ALEX_CELL_60_02
- PROC_ALEX_CELL_60_03
- PROC_ALEX_CELL_60_04
- PROC_ALEX_CELL_60_05
- PROC_ALEX_CELL_60_06
- PROC_ALEX_CELL_60_07
- PROC_ALEX_CELL_60_08
- PROC_ALEX_CELL_60_09
- [omcbo@DGSERNMC111 C]$ cat file
- PROC_ALEX_CELL_60_01
- PROC_ALEX_CELL_60_02
- PROC_ALEX_CELL_60_03
- PROC_ALEX_CELL_60_04
- PROC_ALEX_CELL_60_05
- PROC_ALEX_CELL_60_06
- PROC_ALEX_CELL_60_07
- PROC_ALEX_CELL_60_08
- PROC_ALEX_CELL_60_09