c语言,把文件的所有内容读入到字符串中

http://www.oschina.net/code/snippet_166355_9332

任何的C编译器,任何平台

[1].[代码] 复制粘贴即可运行 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdio.h"
#include "string"
#include "stdlib.h"
 
using namespace std;
 
 
int main( void )
{
     FILE *fp;
     fp = fopen ( "05.爱,九把刀系列之那些年,我们一起追的女孩.txt" , "r" );
     fseek ( fp , 0 , SEEK_END );
     int file_size;
     file_size = ftell ( fp );
     printf ( "%d" , file_size );
     char *tmp;
     fseek ( fp , 0 , SEEK_SET);
     tmp =  ( char *) malloc ( file_size * sizeof ( char ) );
     fread ( tmp , file_size , sizeof ( char ) , fp);
     printf ( "%s" , tmp );
     return 0;
}

[2].[代码] 复制粘贴即可运行 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdio.h"
#include "string"
#include "stdlib.h"
 
using namespace std;
 
 
int main( void )
{
     FILE *fp;
     fp = fopen ( "05.爱,九把刀系列之那些年,我们一起追的女孩.txt" , "r" );
     fseek ( fp , 0 , SEEK_END );
     int file_size;
     file_size = ftell ( fp );
     printf ( "%d" , file_size );
     char *tmp;
     fseek ( fp , 0 , SEEK_SET);
     tmp =  ( char *) malloc ( file_size * sizeof ( char ) );
     fread ( tmp , file_size , sizeof ( char ) , fp);
     printf ( "%s" , tmp );
     return 0;
}

[3].[代码] 复制粘贴即可运行 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdio.h"
#include "string"
#include "stdlib.h"
 
using namespace std;
 
 
int main( void )
{
     FILE *fp;
     fp = fopen ( "05.爱,九把刀系列之那些年,我们一起追的女孩.txt" , "r" );
     fseek ( fp , 0 , SEEK_END );
     int file_size;
     file_size = ftell ( fp );
     printf ( "%d" , file_size );
     char *tmp;
     fseek ( fp , 0 , SEEK_SET);
     tmp =  ( char *) malloc ( file_size * sizeof ( char ) );
     fread ( tmp , file_size , sizeof ( char ) , fp);
     printf ( "%s" , tmp );
     return 0;
}

[4].[代码] 复制粘贴即可运行 跳至 [1] [2] [3] [4]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdio.h"
#include "string"
#include "stdlib.h"
 
using namespace std;
 
 
int main( void )
{
     FILE *fp;
     fp = fopen ( "05.爱,九把刀系列之那些年,我们一起追的女孩.txt" , "r" );
     fseek ( fp , 0 , SEEK_END );
     int file_size;
     file_size = ftell ( fp );
     printf ( "%d" , file_size );
     char *tmp;
     fseek ( fp , 0 , SEEK_SET);
     tmp =  ( char *) malloc ( file_size * sizeof ( char ) );
     fread ( tmp , file_size , sizeof ( char ) , fp);
     printf ( "%s" , tmp );
     return 0;
}


你可能感兴趣的:(C语言)