本文介绍了关于获取线功能需要一些建议。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我正在尝试编写一个函数,它将从 指定文件中读取一行并返回该行。它目前工作正常。 但如果有人指出一些改进点或代码中的错误,那对我来说会非常有帮助。 问候, Somenath char * get_line(FILE * fp) { int len = 2; / *第一个内存分配为2个字节1个用于一个 字符另一个用于''\ 0''* / int pos = 0; char * buff; char * temp_buff; char c; buff = malloc(len); if(fp == NULL || buff == NULL) { return(NULL); } while((c = fgetc(fp))!= EOF) { buff [pos ++] = c; 如果(c ==''\ n'') { / * pos减1,因为,目前buff [pos] 在pos 的 位置包含换行符但是我们需要在换行时将换行符设为buf [pos];所以 返回我们在换行的地方分配''\0'',即 ''\ n''由''\ 0''重新排列。* / pos = pos-1; / *我们到达了结尾行* / 休息; } if(pos == len) { len + = 2; temp_buff = realloc(buff,len); 如果(temp_buff == NULL) { / *内存重新分配失败* / 返回NULL ; } 其他 { buff = temp_buff; } } } buff [pos] =''\ 0''; return buff =(c == EOF?NULL:buff); } 解决方案 somenath写道: 大家好, 我试图写一个函数,它将从一个读取一行 指定文件并返回该行。它目前工作正常。 但如果有人指出一些改进点或代码中的错误,那对我来说会非常有帮助。 问候, Somenath char * get_line(FILE * fp) { int len = 2; / *第一个内存分配为2个字节1个用于一个 字符另一个用于''\ 0''* / int pos = 0; char * buff; char * temp_buff; char c; buff = malloc(len); if(fp == NULL || buff == NULL) { return(NULL); } while((c = fgetc(fp))!= EOF) { buff [pos ++] = c; 如果(c ==''\ n'') { / * pos减1,因为,目前buff [pos] 在pos 的 位置包含换行符但是我们需要在换行时将换行符设为buf [pos];所以 返回我们在换行的地方分配''\0'',即 ''\ n''由''重复' \0''。* / pos = pos-1; / *我们到达了行尾* / 休息; } 1) 我认为它会更容易阅读以进行测试首先是 ''\ n'',如果是这样的话就加入缓冲区 if(c ==''\ n '') 休息; buf [pos ++] = c; 2) 在Windows下,您有\r \ n的新行。如果你想在windows下运行你应该 测试\ r和你正在阅读的文件 是以二进制模式打开的。 if(pos == len) { len + = 2; 重新分配比 只需2个字符更有效率,以避免过多调用 realloc。 temp_buff = realloc(buff,len); if(temp_buff == NULL) { / *内存重新分配失败* / 这里有内存泄漏。你返回时没有释放 旧的缓冲区...... 返回NULL; } else { buff = temp_buff; } } } buff [pos] =''\'''; 返回buff =(c == EOF?NULL:buff); 如果你点击EOF,为什么会返回NULL? 这意味着你有一个不会结束的文件 换行符。那不是一个大罪。你可以返回 到目前为止所读到的内容。 } - jacob navia jacob at jacob point remcomp point fr logiciels / informatique http://www.cs.virginia.edu/~lcc-win32 somenath写道: > 大家好, 我试图编写一个函数,它将从 指定文件中读取一行并返回该行。它目前工作正常。 但如果有人指出一些改进点或代码中的错误,那对我来说会非常有帮助。 问候, char c; while((c = fgetc(fp))!= EOF) 马上,(c)应该输入int。 - pete jacob navia写道: > somenath写道: 大家好, 我试图编写一个函数,它将从 指定文件中读取一行并返回该行。它目前工作正常。 但如果有人指出一些改进点或代码中的错误,那对我来说会非常有帮助。 问候, Somenath char * get_line(FILE * fp) { int len = 2; / *第一个内存分配为2个字节1个用于一个 字符另一个用于''\ 0''* / int pos = 0; 这些int应该是size_t。 char * buff; char * temp_buff; char c; buff = malloc(len); if(fp == NULL || buff == NULL) { return(NULL); } while((c = fgetc(fp))!= EOF) { buff [pos ++] = c; if( c ==''\ n'') { / * pos减1,因为目前buff [pos] 在pos 的 位置包含换行符但是我们需要在换行符时将换行符设为buf [pos];所以 pos递减 返回之前我们在换行的地方分配''\ 0'',即 '' \\'''由''\ 0''重新排列。* / pos = pos-1; / *我们到达了行尾* / 休息; } 1) 我认为它会更容易阅读以测试第一个 ''\ n'',然后如果是这样的话加入缓冲区 if (c ==''\ n'') 休息; buf [pos ++] = c; 我也这么认为。 > 2) 在Windows下,你有了\r \ n的新行。如果你想在windows下运行你应该 测试\ r和你正在阅读的文件 是以二进制模式打开的。 line的整个概念如在get_line中那样 适用于以文本模式打开的流。 > if(pos == len) { len + = 2; 重新分配比 只需2个字符更有效率,以避免过多调用 realloc。 还要多少? 如何防止(len)中的最终溢出? > temp_buff = realloc(buff,len); if(temp_buff == NULL) { / *内存重新分配失败* / 这里有内存泄漏。你返回时没有释放 旧的缓冲区...... 返回NULL; } else { buff = temp_buff; } } } buff [pos] =''\'''; 返回buff =(c == EOF?NULL:buff); 在返回语句中为(buff)赋值( )是没有意义的。 如果你点击EOF,为什么会返回NULL? 这意味着你有一个文件没有结束 换行。 No. 这也可能意味着get_line函数 已读取文件的最后一个字符,上次调用get_line时的。 get_line应该有一些方法来表示 文件的结尾已经到了。 返回NULL就可以了。 这不是一个大罪。你可以返回 到目前为止你所读到的内容。 - pete Hi All,I was trying to write a function which will read one line from aspecified file and return the line. It is currently working fine.But it would be very much helpful for me if some one point out someimprovement points or some error in the code.Regards,Somenathchar* get_line(FILE *fp){int len = 2; /*First memory is allocated for 2 bytes 1 for onecharacter another for ''\0''*/int pos = 0;char *buff;char *temp_buff;char c;buff = malloc(len);if (fp == NULL || buff == NULL){return (NULL);}while ( (c = fgetc(fp)) != EOF){buff[pos++] = c;if (c == ''\n''){/*pos is decremented by one because, currently buff[pos]contains newline at theposition of posbut we need to put newline at the end od buf[pos] ;Sopos is decremented by one andat the end beforereturn we are assignning ''\0'' at the place of newline i.e''\n'' is replcaed by ''\0''.*/pos = pos-1;/*We reached the end of the line*/break;}if (pos == len){len += 2;temp_buff = realloc(buff,len);if (temp_buff == NULL){/*Reallocation of memory fails */return NULL;}else{buff = temp_buff;}}}buff[pos] = ''\0'';return buff = (c==EOF?NULL:buff);} 解决方案 somenath wrote:Hi All,I was trying to write a function which will read one line from aspecified file and return the line. It is currently working fine.But it would be very much helpful for me if some one point out someimprovement points or some error in the code.Regards,Somenathchar* get_line(FILE *fp){ int len = 2; /*First memory is allocated for 2 bytes 1 for onecharacter another for ''\0''*/ int pos = 0; char *buff; char *temp_buff; char c; buff = malloc(len); if (fp == NULL || buff == NULL) { return (NULL); } while ( (c = fgetc(fp)) != EOF) { buff[pos++] = c; if (c == ''\n'') { /*pos is decremented by one because, currently buff[pos]contains newline at the position of pos but we need to put newline at the end od buf[pos] ;Sopos is decremented by one and at the end before return we are assignning ''\0'' at the place of newline i.e''\n'' is replcaed by ''\0''.*/ pos = pos-1; /*We reached the end of the line*/ break; }1)I think itr would be easier to read to test FIRST for''\n'', THEN add to the buffer if that is the caseif (c == ''\n'')break;buf[pos++] = c;2)Under windows, you have new lines with \r \n. You shouldtest for \r if you want to run under windows and the fileyou are reading is opened in binary mode. if (pos == len) { len += 2;It would be more efficient to reallocate much more thanjust 2 characters, to avoid too many calls torealloc. temp_buff = realloc(buff,len); if (temp_buff == NULL) { /*Reallocation of memory fails */You have a memory leak here. You return without freeingthe old buffer... return NULL; } else { buff = temp_buff; } } } buff[pos] = ''\0''; return buff = (c==EOF?NULL:buff);Why return NULL if you hit EOF?That means that you got a file that doesn''t end in anewline. That is not a BIG SIN. You could just returnwhat you have read so far.}--jacob naviajacob at jacob point remcomp point frlogiciels/informatique http://www.cs.virginia.edu/~lcc-win32somenath wrote:>Hi All,I was trying to write a function which will read one line from aspecified file and return the line. It is currently working fine.But it would be very much helpful for me if some one point out someimprovement points or some error in the code.Regards, char c; while ( (c = fgetc(fp)) != EOF)Right off the bat, (c) should be type int.--petejacob navia wrote:>somenath wrote: Hi All, I was trying to write a function which will read one line from a specified file and return the line. It is currently working fine. But it would be very much helpful for me if some one point out some improvement points or some error in the code. Regards, Somenath char* get_line(FILE *fp) { int len = 2; /*First memory is allocated for 2 bytes 1 for one character another for ''\0''*/ int pos = 0;Those int should be size_t. char *buff; char *temp_buff; char c; buff = malloc(len); if (fp == NULL || buff == NULL) { return (NULL); } while ( (c = fgetc(fp)) != EOF) { buff[pos++] = c; if (c == ''\n'') { /*pos is decremented by one because, currently buff[pos] contains newline at the position of pos but we need to put newline at the end od buf[pos] ;So pos is decremented by one and at the end before return we are assignning ''\0'' at the place of newline i.e ''\n'' is replcaed by ''\0''.*/ pos = pos-1; /*We reached the end of the line*/ break; }1)I think itr would be easier to read to test FIRST for''\n'', THEN add to the buffer if that is the case if (c == ''\n'') break; buf[pos++] = c;I think so too.>2)Under windows, you have new lines with \r \n. You shouldtest for \r if you want to run under windows and the fileyou are reading is opened in binary mode.The whole concept of "line" as in "get_line"applies to streams opened in text mode.> if (pos == len) { len += 2;It would be more efficient to reallocate much more thanjust 2 characters, to avoid too many calls torealloc.How much more?How do you prevent eventual overflow in (len)?> temp_buff = realloc(buff,len); if (temp_buff == NULL) { /*Reallocation of memory fails */You have a memory leak here. You return without freeingthe old buffer... return NULL; } else { buff = temp_buff; } } } buff[pos] = ''\0''; return buff = (c==EOF?NULL:buff);There''s no point in assigning a value to (buff) either,in the return statement.Why return NULL if you hit EOF?That means that you got a file that doesn''t end in anewline.No.It could also mean that the get_line functionhad read the last character of the file,on the previous invocation of get_line.get_line should have some way to indicatethat the end of file has been reached.Returning NULL is OK.That is not a BIG SIN. You could just returnwhat you have read so far.--pete 这篇关于关于获取线功能需要一些建议。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 23:20
查看更多