读取文本文件,以行为单位分隔

今天小康让我给他写个程序,要求读取文本文件,并以行为单位保存在一个数组里,然后用一个循环使用每行的内容。
下面两个程序只是对单位行输出,但分隔为单位行的目的达到。

/*FileName: readline.c*/
#include
#include
#include
#include
#include
#define MAX 256

char s[MAX][MAX];

int read_line(int fd,char *buf)
{
char ch;
int i = 0,j;

if ((j = read(fd,&ch,1)) == 0) {
return EOF;
}
while (ch != ‘n’ && j != 0 ){
buf[i++] = ch;
j = read(fd,&ch,1);
}

buf[i++] = ”;
return 1;
}

int string()
{
char buf[MAX];
int fd=open(“readline.c”, O_RDONLY);
int j=0;
int i=read_line(fd, (char *)buf);
while (i != EOF ){
if (i == 1){
strcpy(s[j++], buf);
fflush(stdout);
}
i = read_line(fd,buf);
}
return j;
}

int main(void)
{
int i;
int j=string();
for(i=0; i<j; i++)
printf(“%sn”, s[i]);
return 0;
}

#!/bin/bash
#FileName: readline.sh
L=`cat readline.c|wc -l`
i=1
while [ $i -le $L ]
do
p=’p’
IP=$i$p
sed -n `echo $IP` readline.c
let i=$i+1
done

2 thoughts on “读取文本文件,以行为单位分隔

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.