#include
#include
#include
#include
#include
#include
int readline(int fd,char* line,size_t len);
void print_empty_lines(char*);
void print(char*);
void uppercase(char*);
int main(int argc,char** argv)
{
int fd = g_open("./002.c",O_RDONLY,S_IRUSR);
if(fd == -1){
g_print("%s\n",strerror(errno));
return -1;
}
char line[1024];
while(readline(fd,line,1024) != -1){
print(line);
print_empty_lines(line);
//uppercase(line);
}
close(fd);
return 0;
}
//read a line of text from file
int readline(int fd,char* line,size_t len){
memset(line,0,len);
int i = 0;
while( i < len - 1){
int ret = read(fd,line + i,1);
if(ret == -1 && ret == EINTR) continue;
if(ret == 0) break;
if(ret == -1) return -1;
i++;
if(line[i - 1] == '\n'){
line[i - 1] = 0;
return i;
}
}
return -1;
}
typedef void (*line_func)(char*);
//print each line of text
void print(char* line){
g_print("%s\n",line);
}
void print_empty_lines(char* line){
if(strlen(line) <= 0) g_print("This is a blank line\n");
}
void uppercase(char* line){
GString* lower = g_string_new(line);
g_string_ascii_up(lower);
g_print("%s\n",lower);
g_string_free(lower,TRUE);
}
阅读(1129) | 评论(0) | 转发(0) |