当前位置: 技术问答>linux和unix
C语言,有没有从文件中读取一行的函数
来源: 互联网 发布时间:2015-06-10
本文导语: Linux 下的C语言有没有读取一行的函数 原来TC下fgets()好像是读取一行的,但在linux下该函数的参数改变了, 为读取制定长度的字符串。 望高手指教 | 还是用fgets呀 gets, fgets -- get a strin...
Linux 下的C语言有没有读取一行的函数
原来TC下fgets()好像是读取一行的,但在linux下该函数的参数改变了,
为读取制定长度的字符串。
望高手指教
原来TC下fgets()好像是读取一行的,但在linux下该函数的参数改变了,
为读取制定长度的字符串。
望高手指教
|
还是用fgets呀
gets, fgets -- get a string from a stream
Synopsis
#include
char * gets (char * s);
char * fgets (char * s, int n, FILE * stream);
Description
gets reads characters from the standard input stream (see intro(3)),
stdin, into the array pointed to by s, until a newline character is
read or an end-of-file condition is encountered. The newline character
is discarded and the string is terminated with a null character.
fgets reads characters from the stream into the array pointed to by s,
until n -1 characters are read, or a newline character is read and
transferred to s, or an end-of-file condition is encountered. The
string is then terminated with a null character.
When using gets, if the length of an input line exceeds the size of s,
indeterminate behavior may result. For this reason, it is strongly
recommended that gets be avoided in favor of fgets.
Errors
If end-of-file is encountered and no characters have been read, no
characters are transferred to s and a null pointer is returned. If a
read operation was attempted, and an error occurs, such as trying to
use these functions on a file that has not been opened for reading, a
null pointer is returned. If end-of-file is encountered, the EOF
indicator for the stream is set. Otherwise s is returned.
The functions gets and fgets fail when the file is a regular file and
an attempt was made to read at or beyond the offset maximum associated
with the corresponding stream. There is no data transfer.
函数一直读到换行或者n个数据才停止,所以当n指定很大的时候,肯定读取1行了
gets, fgets -- get a string from a stream
Synopsis
#include
char * gets (char * s);
char * fgets (char * s, int n, FILE * stream);
Description
gets reads characters from the standard input stream (see intro(3)),
stdin, into the array pointed to by s, until a newline character is
read or an end-of-file condition is encountered. The newline character
is discarded and the string is terminated with a null character.
fgets reads characters from the stream into the array pointed to by s,
until n -1 characters are read, or a newline character is read and
transferred to s, or an end-of-file condition is encountered. The
string is then terminated with a null character.
When using gets, if the length of an input line exceeds the size of s,
indeterminate behavior may result. For this reason, it is strongly
recommended that gets be avoided in favor of fgets.
Errors
If end-of-file is encountered and no characters have been read, no
characters are transferred to s and a null pointer is returned. If a
read operation was attempted, and an error occurs, such as trying to
use these functions on a file that has not been opened for reading, a
null pointer is returned. If end-of-file is encountered, the EOF
indicator for the stream is set. Otherwise s is returned.
The functions gets and fgets fail when the file is a regular file and
an attempt was made to read at or beyond the offset maximum associated
with the corresponding stream. There is no data transfer.
函数一直读到换行或者n个数据才停止,所以当n指定很大的时候,肯定读取1行了
|
用fscanf标准读入
for example
fscanf(file,"%d %s",id,name)
完全是标准读入 一一对应
for example
fscanf(file,"%d %s",id,name)
完全是标准读入 一一对应
|
标准io 是fgets gets 无缓存io read 你要设置终端模式为行缓存 为 文件一行的长度。
推荐标准io.
推荐标准io.
|
int main(int argc, char *argv[]){
FILE *in, *out;
char buffer[256];
if((in = fopen(argv[1], "r")) != NULL && (out = fopen(argv[2]), "w")) != NULL){
while(fgets(buffer, sizeof(buffer), in)){
fputs(buffer, out);
}
}
fclose(in);
fclose(out);
}
FILE *in, *out;
char buffer[256];
if((in = fopen(argv[1], "r")) != NULL && (out = fopen(argv[2]), "w")) != NULL){
while(fgets(buffer, sizeof(buffer), in)){
fputs(buffer, out);
}
}
fclose(in);
fclose(out);
}
|
楼上说的都对!
随便一本c语言书上都有的
随便一本c语言书上都有的
|
嗯,谁来总结一下读文件的各种方式?
|
fgets