一下代码在 Xcode 中实现:
//
// main.m
// C_Homework_10
//
// Created by on 15/3/25.
// Copyright (c) 2015年 . All rights reserved.
//
#import <Foundation/Foundation.h>
void readFileBytes(const char *name);
int main(int argc, const char * argv[]) {
// 3. (****)有一段文本,将文本中的所有单词,存放到一个字符串数组中。 (要求占用内存最少)
char *filePath = "/Users/lanouhn/Desktop/Homwork/C_Project_11/C_Project_11/words.txt" ;
readFileBytes( filePath );
return 0;
}
void readFileBytes(const char *path)
{
FILE *fl = NULL;
fl = fopen( path, "r" );
if ( fl == NULL ) // 目的是判断 fopen() 函数是否成功执行
{
fprintf(stderr, "%s \n", strerror(errno));
exit(0);
}
fseek( fl, 0, SEEK_END );
long len = ftell( fl );
fseek( fl, 0, SEEK_SET );
// char p[256][256] = {0};
// int i = 0, j = 0;
while( len-- > 0 )
{
char c = fgetc( fl );
if( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) )
{
// p[i][j] = c ;
// j++;
printf( "%c", c );
}
else
{
// j = 0;
// i++;
printf( "\n" );
}
}
// for( int n = 0; n < i; n++ )
// printf( "%s\n", p[n]);
fclose( fl );
}