用C语言一次读一行
在C语言中,哪种方法可以一次读取一行文件 我正在使用fgets功能,但它不起作用。 它只读取空格分隔的标记用C语言一次读一行,c,file,C,File,在C语言中,哪种方法可以一次读取一行文件 我正在使用fgets功能,但它不起作用。 它只读取空格分隔的标记 怎么办 fgets()应该是正确的选择…函数将从文件或num字符中读取一行,其中num是传递给fgets的第二个参数。你通过的号码够大吗 比如说 // Reads 500 characters or 1 line, whichever is shorter char c[500]; fgets(c, 500, pFile); Vs 使用fgets如果您知道您的行可以放入缓冲区,或者使用f
怎么办
fgets()
应该是正确的选择…函数将从文件或num
字符中读取一行,其中num
是传递给fgets
的第二个参数。你通过的号码够大吗
比如说
// Reads 500 characters or 1 line, whichever is shorter
char c[500];
fgets(c, 500, pFile);
Vs
使用
fgets
如果您知道您的行可以放入缓冲区,或者使用fgetc
对读取进行更多控制如果您为具有GNU C库的平台编码,您可以使用getline()
这与其说是一个完整的答案,不如说是一个评论,但我没有足够的要点来评论。:) 以下是fgets()的函数原型:
它将读取n-1个字节或最多一个换行符或eof。有关更多信息,请参见当您因某种原因无法使用
fgets()
时,此功能应该可以使用
int readline(FILE *f, char *buffer, size_t len)
{
char c;
int i;
memset(buffer, 0, len);
for (i = 0; i < len; i++)
{
int c = fgetc(f);
if (!feof(f))
{
if (c == '\r')
buffer[i] = 0;
else if (c == '\n')
{
buffer[i] = 0;
return i+1;
}
else
buffer[i] = c;
}
else
{
//fprintf(stderr, "read_line(): recv returned %d\n", c);
return -1;
}
}
return -1;
}
int readline(文件*f,字符*buffer,大小长度)
{
字符c;
int i;
memset(缓冲区,0,len);
对于(i=0;i
使用fgets
从行中读取,然后使用getc(…)
咀嚼换行符或行尾以继续读取…下面是一个永远读取行的示例
// Reads 500 characters or 1 line, whichever is shorter
char c[500], chewup;
while (true){
fgets(c, sizeof(c), pFile);
if (!feof(pFile)){
chewup = getc(pFile); // To chew up the newline terminator
// Do something with C
}else{
break; // End of File reached...
}
}
//读取500个字符或1行,以较短者为准
char c[500],咀嚼;
while(true){
fgets(c,sizeof(c),pFile);
如果(!feof(pFile)){
chewup=getc(pFile);//检查换行符终止符
//用C做点什么
}否则{
break;//已到达文件结尾。。。
}
}
使用以下程序从文件中逐行获取数据
#include <stdio.h>
int main ( void )
{
char filename[] = "file.txt";
FILE *file = fopen ( filename, "r" );
if (file != NULL) {
char line [1000];
while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */ {
fprintf(stdout,"%s",line); //print the file contents on stdout.
}
fclose(file);
}
else {
perror(filename); //print the error message on stderr.
}
return 0;
}
#包括
内部主(空)
{
char filename[]=“file.txt”;
FILE*FILE=fopen(文件名,“r”);
如果(文件!=NULL){
字符行[1000];
while(fgets(line,sizeof line,file)!=NULL)/*从文件中读取一行*/{
fprintf(标准输出,“%s”,行);//在标准输出上打印文件内容。
}
fclose(文件);
}
否则{
perror(filename);//在stderr上打印错误消息。
}
返回0;
}
错误源:
实际上这不是我的错。。
在这种情况下。
我使用strtok函数,不小心修改了原始字符串。
因此,在打印时,我得到了错误
谢谢大家帮助我……) 您可以使用fscanf而不是fgets。因为fgets fscanf用于包含空格的字符,但在使用fscanf时,您可以单独访问保存在文件中的数据。例如,有一个文件名为class roll。现在声明一个字符串和两个整数行。只要缓冲区大,则足够汤姆:这是第二个参数的用途,以避免缓冲区溢出。或者你的意思是没有预见到足够大的缓冲区?IIRC,我的C编译器每行有512字节的限制。那是哪个C编译器?。我在想一个例子,你不知道这条线有多长,例如stdin。不想在这上面发动圣战。所以你不是真的想读一行,你想读到一个空格?或者你是说FGET停止在空格而不是换行符上?应该只停止在换行符上。这是我的输入::启动一个DC 10 B DS 20停止我得到::启动一个B停止作为我的输出..@JaredPar:酷!你修好了!:P嘿,我在从0到-1的向下投票后向上投票,然后在你纠正它时向上投票,使其返回到0,而不是跳到1…这是我的浏览器还是故障?@t0mm13b要恢复你的向下投票,再次单击向下投票按钮。向下投票,然后向上投票!=thanx buddy这是wokring,但我仍然有一个问题。。我的缓冲区正在读取和存储整行内容,但当只打印第一个空格分隔的标记时,这很奇怪,我只能使用
printf(“%s\n”,buffer)它输出整个缓冲区,这对我来说也很奇怪。因为它适用于所有以前的程序…:'(您确定您的文件不包含任何字符串终止符吗?该函数在BSD系统中也可用,仅供参考;)
// Reads 500 characters or 1 line, whichever is shorter
char c[500], chewup;
while (true){
fgets(c, sizeof(c), pFile);
if (!feof(pFile)){
chewup = getc(pFile); // To chew up the newline terminator
// Do something with C
}else{
break; // End of File reached...
}
}
#include <stdio.h>
int main ( void )
{
char filename[] = "file.txt";
FILE *file = fopen ( filename, "r" );
if (file != NULL) {
char line [1000];
while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */ {
fprintf(stdout,"%s",line); //print the file contents on stdout.
}
fclose(file);
}
else {
perror(filename); //print the error message on stderr.
}
return 0;
}