Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 读取输出文件的行数_C_File Pointer - Fatal编程技术网

C 读取输出文件的行数

C 读取输出文件的行数,c,file-pointer,C,File Pointer,这是我的问题。输出文件有七行,如下所示: Dinda Jane is so beautiful and handsome. char c = getc(fp); while (c != EOF) { if (c == '\n') a++: c = getc(fp); } printf("Number of line is %d", (a + 1)/2); // (the +1 is for the last line) #include<stdio.h> #i

这是我的问题。输出文件有七行,如下所示:

Dinda
Jane
is
so
beautiful
and
handsome.
char c = getc(fp);
while (c != EOF) {
    if (c == '\n') a++:
    c = getc(fp);
}

 printf("Number of line is %d", (a + 1)/2); // (the +1 is for the last line)
#include<stdio.h>
#include<string.h>
main()
{
    FILE *fp;
    char filename[25];
    char buffer[256];
    int buffer_size = sizeof(buffer);
    int a=0;

    printf("Enter File Name: ");
    fgets(filename,sizeof(filename),stdin); //Always use fgets
    filename[strlen(filename)-1]='\0'; //Remove \n from the filename

    fp=fopen(filename,"r"); 
    if(fp==NULL) //Check if file pointer is NULL then return
    {
        printf("\nERROR! File Doesn't Exist!");
        return;
    }

    while (fgets(buffer, buffer_size, fp)) //Count number of lines in file
    {
        a++;
    }

    fclose(fp);

    //If number of lines are even then just divide by 2 else divide by 2 and add one
    if (a % 2 == 0) 
        a = (a / 2); 
    else        
        a = (a / 2) + 1;

    printf("The Number of lines are: %d",a);
    return 0;
}
我们被要求读取输出文件有多少行。但是两行应该只算1。因此,这个程序中的确切行数应该是四行(包括最后一行)。我怎么能一次读两行呢?这就是我目前得到的

#include<stdio.h>
main()
{
    FILE *fp;
    char filename[25];
    char c[25][25];
    int a;

    clrscr();

    printf("Enter File Name: ");
    gets(filename);

    if(filename==NULL)
    {
        printf("\nERROR! File Doesn't Exist!");
    }
    else
    {
        fp=fopen(filename,"r");

        while(!feof(fp))
        {
            fgets(&c,sizeof(c),fp);
            printf("%s",c);
            if(strlen(c))
            {
                a++;
            }
        }

        printf("The Number of Words are: %d",a);
        fclose(fp);
        getch();
    }
}
#包括
main()
{
文件*fp;
字符文件名[25];
字符c[25][25];
INTA;
clrsc();
printf(“输入文件名:”);
获取(文件名);
如果(文件名==NULL)
{
printf(“\n错误!文件不存在!”);
}
其他的
{
fp=fopen(文件名,“r”);
而(!feof(fp))
{
fgets(c&c、sizeof(c)、fp);
printf(“%s”,c);
if(strlen(c))
{
a++;
}
}
printf(“字数为:%d”,a);
fclose(fp);
getch();
}
}

尝试以下方法:

Dinda
Jane
is
so
beautiful
and
handsome.
char c = getc(fp);
while (c != EOF) {
    if (c == '\n') a++:
    c = getc(fp);
}

 printf("Number of line is %d", (a + 1)/2); // (the +1 is for the last line)
#include<stdio.h>
#include<string.h>
main()
{
    FILE *fp;
    char filename[25];
    char buffer[256];
    int buffer_size = sizeof(buffer);
    int a=0;

    printf("Enter File Name: ");
    fgets(filename,sizeof(filename),stdin); //Always use fgets
    filename[strlen(filename)-1]='\0'; //Remove \n from the filename

    fp=fopen(filename,"r"); 
    if(fp==NULL) //Check if file pointer is NULL then return
    {
        printf("\nERROR! File Doesn't Exist!");
        return;
    }

    while (fgets(buffer, buffer_size, fp)) //Count number of lines in file
    {
        a++;
    }

    fclose(fp);

    //If number of lines are even then just divide by 2 else divide by 2 and add one
    if (a % 2 == 0) 
        a = (a / 2); 
    else        
        a = (a / 2) + 1;

    printf("The Number of lines are: %d",a);
    return 0;
}
请在代码的其他部分尝试此代码

它一次获取一个字符,并比较该字符是否为换行符。如果标志变量是新行字符,则它将递增该标志变量。最后,在遍历文件中的所有字符之后。它将使标志值减半。就像你想要的一样。

当你这么做的时候,为什么要让它变得困难(读两行):

Dinda
Jane
is
so
beautiful
and
handsome.
char c = getc(fp);
while (c != EOF) {
    if (c == '\n') a++:
    c = getc(fp);
}

 printf("Number of line is %d", (a + 1)/2); // (the +1 is for the last line)
#include<stdio.h>
#include<string.h>
main()
{
    FILE *fp;
    char filename[25];
    char buffer[256];
    int buffer_size = sizeof(buffer);
    int a=0;

    printf("Enter File Name: ");
    fgets(filename,sizeof(filename),stdin); //Always use fgets
    filename[strlen(filename)-1]='\0'; //Remove \n from the filename

    fp=fopen(filename,"r"); 
    if(fp==NULL) //Check if file pointer is NULL then return
    {
        printf("\nERROR! File Doesn't Exist!");
        return;
    }

    while (fgets(buffer, buffer_size, fp)) //Count number of lines in file
    {
        a++;
    }

    fclose(fp);

    //If number of lines are even then just divide by 2 else divide by 2 and add one
    if (a % 2 == 0) 
        a = (a / 2); 
    else        
        a = (a / 2) + 1;

    printf("The Number of lines are: %d",a);
    return 0;
}
#包括
#包括
main()
{
文件*fp;
字符文件名[25];
字符缓冲区[256];
int buffer_size=sizeof(buffer);
int a=0;
printf(“输入文件名:”);
fgets(filename,sizeof(filename),stdin);//始终使用fgets
filename[strlen(filename)-1]='\0';//从文件名中删除\n
fp=fopen(文件名,“r”);
if(fp==NULL)//检查文件指针是否为NULL,然后返回
{
printf(“\n错误!文件不存在!”);
返回;
}
while(fgets(buffer,buffer_size,fp))//计算文件中的行数
{
a++;
}
fclose(fp);
//如果行数为偶数,则只需除以2,否则除以2,再加一
如果(a%2==0)
a=(a/2);
其他的
a=(a/2)+1;
printf(“行数为:%d”,a);
返回0;
}

有很多可能的解决方案。按照字面意思“一次读两行”,你会得到

char c[256];
while(fgets (c, sizeof(c), fp)))
{
   a++;
   if (!fgets(c,sizeof(c),fp))
       break;
}
它读取一行,递增行计数器,然后读取另一行而不递增计数器。即使最后一行没有以返回结束(在您的规范中缺失,但根据WhozCraig的观察),它也能正常工作


它可以处理长达254个字符的单行(不包括终止的
\n
)。您可以调整代码以允许更长的行:将两个
fgets
命令放入循环中,如果最后读取的字符是
\n
(或
fgets
返回
0
以指示文件结束或错误)。

不要使用
get
。如果(filename==NULL)测试将永远不会为真。数组不是指针。使用FGET计算行数,然后打印(n+1)/2作为结果。这是你现在想要解决的问题。这是错误的,你的假设对这一点也没有帮助。
fgets
完全没有验证检查。你从来没有在问题中指定(也可能从来没有在作业中被告知)每一行(包括最后一行)是否必须以换行符结尾才能被视为完整的“行”。一个文件的最后一行通常没有换行符就终止了,这使得计算行数的算法比大多数人第一次意识到的更为繁琐。仔细考虑后,“两行只能算1”是什么意思?如果行数为奇数,则不读取两行;你只看了一本。因此,从逻辑上讲,它不应该被计算。注意:应该使用
int c
。否则,256个不同的
char
中的1个将匹配
EOF
。次要:如果(a%2==0)块,则只匹配
a=a/2+a%2
@chux:你的建议好多了:)谢谢你的
a%2
想法虽然比
a=(a+1)/2
好一点,正如其他人针对
a==INT\u MAX
的单数情况所发表的那样,你的方法得出了正确的答案。在我看来,
int
太小,无法计算行数,因为文件可能很大把我甩了,我在修理时错过了第二部分。