读书人

c fgets()函数 读取文件时位置会自动移

发布时间: 2012-04-07 17:31:51 作者: rapoo

c fgets()函数 读取文件时位置会自动移动下一行吗?
文件内容:
name1=xiaoming
name2=andylu
name3=kaka
在读取第一行name1=xiaoming后,读取位置会移到下一行吗?
我写了个测试程序,好像是可以,但是在网上查的资料,没有说可以会自动移动位置,这是怎么回事啊?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define pFile "file.cfg"
#define MAXLINE 1024
#define TRUE 1

static int get_section_info(char *RdBuff, char *argName, char *argValue)
{
char *current;
char *tail;
char *value;

if( !RdBuff )
return -1;
current = RdBuff;

while (*current == ' ' || *current == '\t')
current++;

if (*current == ';' || *current == '#')
return -1;

if (*current == '\n' || *current == '\0')
return -1;

tail = current;
while (*tail != '=' && *tail != '\n' && *tail != ';' && *tail != '#' && *tail != '\0')
tail++;

value = tail + 1;
if (*tail != '=')
*value = '\0';

*tail-- = '\0';
while (*tail == ' ' || *tail == '\t')
{
*tail = '\0';
tail--;
}

tail = value;
while (*tail != '\n' && *tail != '\0')
tail++;
*tail = '\0';

if (argName)
strcpy(argName, current);
if (argValue)
strcpy(argValue, value);

return 0;
}

int main()
{
int ret;
FILE *fp;
char name[16];
char value[16];
char szBuff[MAXLINE];


if( !(fp = fopen(pFile, "r")) )
{
printf("The file open failur!\n");
return -1;
}

while( fgets(szBuff, MAXLINE, fp) )

ret = get_section_info(szBuff, name, value);
if(ret < 0)
continue;
printf("*****%s = %s*****\n", name, value);
sleep(1);
}

return 1;
}


[解决办法]
因为fgets()读取的时候,想要读取的字节数是MAXLINE,超过了文件中一行实际的字节数,那么他就会自动移动到下一行,你fgets(szBuff, 2, fp)这样肯定就不会。。。
[解决办法]

C/C++ code
#include <stdio.h>#include <string.h>#define MAXLEN 1000char ln[MAXLEN];FILE *f;int i,z;int b,n,L;int main(int argc,char **argv) {    if (argc<2) {        printf("Usage:%s fullpathfilename.ext\nget total blank/non-blank/total linenumbers.\n",argv[0]);        return 1;    }    f=fopen(argv[1],"r");    if (NULL==f) {        printf("Can not open file [%s]!\n",argv[1]);        return 2;    }    z=0;    b=0;    n=0;    L=0;    while (1) {        if (NULL==fgets(ln,MAXLEN,f)) break;        L=strlen(ln);        if ('\n'==ln[L-1]) {            if (0==z) {                for (i=0;i<L-1;i++) {                    if (!(' '==ln[i] || '\t'==ln[i])) break;                }                if (i<L-1) z=1;//当前行不是空行            }            if (0==z) b++; else n++;            z=0;        } else {            if (0==z) {                for (i=0;i<L;i++) {                    if (!(' '==ln[i] || '\t'==ln[i])) break;                }                if (i<L) z=1;//当前行不是空行            }        }    }    fclose(f);    if (L>0 && '\n'!=ln[L-1]) {        if (0==z) b++; else n++;//最后一行末尾无'\n'也计算    }    printf("File:[%s] total blank/non-blank/total linenumbers is %d/%d/%d\n",argv[1],b,n,b+n);    return 0;} 


[解决办法]
fgets() reads in at most one less then size characters from stream and
stores them into the buf pointed to by s,
Reading stops after an EOF or a NEWLINE.
A '\0' is stored after the last character in the buffer.
[解决办法]

C/C++ code
函数名: fgets功  能: 从流中读取一字符串用  法: char *fgets(char *string, int n, FILE *stream);程序例:#include <string.h>#include <stdio.h>int main(void){   FILE *stream;   char string[] = "This is a test";   char msg[20];   /* open a file for update */   stream = fopen("DUMMY.FIL", "w+");   /* write a string into the file */   fwrite(string, strlen(string), 1, stream);   /* seek to the start of the file */   fseek(stream, 0, SEEK_SET);   /* read a string from the file */   fgets(msg, strlen(string)+1, stream);   /* display the string */   printf("%s", msg);   fclose(stream);   return 0;} 

读书人网 >C语言

热点推荐