用fgets()时有点问题,请大侠指点??(在线给分)

laonao0531 2002-08-01 10:54:39
我fgets(char *,fp,64)从文件里读取一行时,是否在可见字符后面还有其他的?
我用uedit显示看见有一个0d(回车符)和一个0a(还行符),是否平时我们编写文件的回车将包括一个回车符和一个换行符号????
若是这样,一般应该如何处理??
...全文
951 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zheng_can 2002-08-02
  • 打赏
  • 举报
回复
二进制文件不会做这种转换
而文本文件就会
各位可以修改修改自己打开文件的方式
cd_ragon 2002-08-02
  • 打赏
  • 举报
回复
其实是两种不同格式的文本文件。

windows 下面的文本文件里,按一下回车键,
会产生两个字符: 0x0d 和 0x0a

而 unix/linux 下面,按一下回车键,只会
产生一个字符:0x0d

所以,在 unix/linux 下产生的文本文件,再
拿到 windows 下,一般显示的时候,就会
结构很乱。

当然,ultraedit 之类的编辑软件,可以自动
转换这两种格式。(unix/linux 下也可以直接
产生 windows 格式的文本文件)

(BTW,0x0d = '\n',0x0a = '\r')
liukai10 2002-08-02
  • 打赏
  • 举报
回复
平时我们输入回车时都有‘\n’,'\r',为回车(回到行首),换行(倒下一行相同位置),注意到平时的输入中“\n”,明显两个都执行了。
liuns 2002-08-02
  • 打赏
  • 举报
回复
同意 zheng_can(nothrow)说的,fgets这个函数的用法可以看看msdn
fgets, fgetws
Get a string from a stream.

char *fgets( char *string, int n, FILE *stream );

wchar_t *fgetws( wchar_t *string, int n, FILE *stream );

Function Required Header Compatibility
fgets <stdio.h> ANSI, Win 95, Win NT
fgetws <stdio.h> or <wchar.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

Each of these functions returns string. NULL is returned to indicate an error or an end-of-file condition. Use feof or ferror to determine whether an error occurred.

Parameters

string

Storage location for data

n

Maximum number of characters to read

stream

Pointer to FILE structure

Remarks

The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.

fgets is similar to the gets function; however, gets replaces the newline character with NULL. fgetws is a wide-character version of fgets.

fgetws reads the wide-character argument string as a multibyte-character string or a wide-character string according to whether stream is opened in text mode or binary mode, respectively. For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_fgetts fgets fgets fgetws


Example

/* FGETS.C: This program uses fgets to display
* a line from a file on the screen.
*/

#include <stdio.h>

void main( void )
{
FILE *stream;
char line[100];

if( (stream = fopen( "fgets.c", "r" )) != NULL )
{
if( fgets( line, 100, stream ) == NULL)
printf( "fgets error\n" );
else
printf( "%s", line);
fclose( stream );
}
}


Output

/* FGETS.C: This program uses fgets to display



liushmh 2002-08-01
  • 打赏
  • 举报
回复
我们编写文件的回车将包括一个回车符和一个换行符号????
是的。
应该如何处理??
一般用getline()函数来处理
jiang_nan 2002-08-01
  • 打赏
  • 举报
回复
其实包含的就是'\t'这个字符,只要读出来后再处理一下,将'\t'转化为'\0'就成了合法的字符串了。
shornmao 2002-08-01
  • 打赏
  • 举报
回复
fgets获取的输入已经去处了\n。

70,039

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧