读取文件的多行 [英] read multiple lines of a file

查看:72
本文介绍了读取文件的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取文件的行并将每行读入数组?

例如;

array [0] = line1

array [1] = line2

...

How can i read lines of a file and place each line read in an array?
for exemple;
array[0]=line1
array[1]=line2
...

推荐答案

5月30日星期一2005 16:50:49 +0000(UTC),
ch ***** *@gmail-dot-com.no-spam.inva lid(Clunixchit)写道:
On Mon, 30 May 2005 16:50:49 +0000 (UTC),
ch******@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:
如何读取文件的行并将每行读入数组?
例如;
数组[0] =第1行
数组[1] =第2行
How can i read lines of a file and place each line read in an array?
for exemple;
array[0]=line1
array[1]=line2




您使用的单词line表示文本文件。 fgets()经常是选择的工具。


如果文件不是文本文件,那么fread()可能会更好。


<<删除电子邮件的del>>



Your use of the word line implies a text file. fgets() is frequently
the tool of choice.

If the file is not a text file, then fread() would probably be better.

<<Remove the del for email>>


谢谢你用了fgets

Thanks ive used fgets


我有*** glibc检测到***损坏的双链表:

0x0804b错误。

这里是我的代码:


typedef struct new_phrase {

int N; //字数

char ** T; //存储在数组中的单词

} new_phrase;


new_phrase file_array(new_phrase短语){

短语.N = 0 ;

FILE * stream;

char tmp [LINE_LENGTH];


/ *打开文件。如果返回NULL则出现错误* /

if(!(stream = fopen(MYAIRC,r,

))){

perror(" fopen");

_exit(EXIT_FAILURE);

}

(void)fseek(stream,0,SEEK_SET );


if(!(phrase.T = malloc(sizeof(phrase.T)

))){

perror(" malloc");

_exit(EXIT_FAILURE);

}


//创建数组>短语.T

while(fgets(tmp,LINE_LENGTH-1,stream)!= NULL

){

size_t l = strlen(tmp );

if(l> 0&& tmp [l-1] ==''\ n'')

tmp [l-1] =''\ 0'';

if(!(phrase.T [phrase.N] = malloc(sizeof

(phrase.T [phrase.N] )))){

perror(malloc);

_exit(EXIT_FAILURE);

}

phrase.T [phrase.N] = tmp;

printf(" phrase.T%s

%d \ n",phrase.T [phrase。 N],短语.N);

短语.N ++;

}


fclose(流);


if(phrase.N == 0){

printf("%s is empty\\\
,MYAIRC);

_exit(EXIT_FAILURE);

}

返回短语;

}


已经使用了fgets上面提到过。

此代码允许填充数组但返回错误

*** glibc检测到***损坏的双链表:0x0804b168 ***


有什么建议吗?还是改进?
im having a *** glibc detected *** corrupted double-linked list:
0x0804b error.
here is my code :

typedef struct new_phrase {
int N; // number of words
char **T; // words stored in an array
} new_phrase;

new_phrase file_array (new_phrase phrase) {
phrase.N=0;
FILE *stream;
char tmp[LINE_LENGTH];

/* Open the file. If NULL is returned there was an error */
if ( !(stream = fopen( MYAIRC , "r"
))) {
perror("fopen");
_exit(EXIT_FAILURE);
}
(void) fseek(stream,0,SEEK_SET);

if ( !(phrase.T = malloc ( sizeof (phrase.T)
))) {
perror("malloc");
_exit(EXIT_FAILURE);
}

// creating array > phrase.T
while ( fgets ( tmp , LINE_LENGTH-1 , stream ) != NULL
) {
size_t l = strlen(tmp);
if (l > 0 && tmp[l-1] == ''\n'')
tmp[l-1] = ''\0'';
if ( !(phrase.T[phrase.N] = malloc ( sizeof
(phrase.T[phrase.N]) ))) {
perror("malloc");
_exit(EXIT_FAILURE);
}
phrase.T[phrase.N]=tmp;
printf("phrase.T %s
%d\n",phrase.T[phrase.N],phrase.N);
phrase.N++;
}

fclose (stream);

if ( phrase.N == 0 ) {
printf("%s is empty\n",MYAIRC);
_exit(EXIT_FAILURE);
}
return phrase;
}

ive used the fgets mentioned above.
this code allows to fill the array but return the error
*** glibc detected *** corrupted double-linked list: 0x0804b168 ***

any suggestions? or improvements?

phrase.T xlogo 0
phrase.T ls -i 1
phrase.T ls -l 2
phrase.T xload 3
*** glibc检测到***损坏的双链表:0x0804b168 ***

phrase.T xlogo 0
phrase.T ls -i 1
phrase.T ls -l 2
phrase.T xload 3
*** glibc detected *** corrupted double-linked list: 0x0804b168 ***






这篇关于读取文件的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆