打印从给定字母开始的整条线

发布于 2025-02-05 19:53:28 字数 452 浏览 1 评论 0原文

我试图打印从给定字母到第二文件的线条,但仅打印1行,然后停止,即使有更多的行以给​​定字母开头。如何修复它?

#include <stdio.h>
#include <string.h>
int main()
{
    FILE* f = fopen("story.txt","r");
    char usr;
    printf("enter letter: ");
    scanf(" %c",&usr);

    FILE* f2=fopen("letter.txt","a+");

    char buffer[255];
       while(fscanf(f, "%[^\n]s", buffer)==1)
        if (*buffer == usr)
          fprintf(f2, "%s\n", buffer);

    return 0;
}

I'm trying to print lines which starts from given letter to second file but it prints only 1 line and then stops, even if there are more lines which starts with given letter. How to fix it ?

#include <stdio.h>
#include <string.h>
int main()
{
    FILE* f = fopen("story.txt","r");
    char usr;
    printf("enter letter: ");
    scanf(" %c",&usr);

    FILE* f2=fopen("letter.txt","a+");

    char buffer[255];
       while(fscanf(f, "%[^\n]s", buffer)==1)
        if (*buffer == usr)
          fprintf(f2, "%s\n", buffer);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻了热茶 2025-02-12 19:53:29

第二次通过循环,fscanf(f,“%[^\ n] s”,buffer)无法扫描任何内容在缓冲区中。这无法超越。

使用fgets()而不是fscanf()读取整行。

    char buffer[255];
    while(fgets(buffer, sizeof buffer, f))
        if (buffer[0] == usr)
            fprintf(f2, "%s", buffer);

The second time through the loop, fscanf(f, "%[^\n]s", buffer) fails to scan anything because the previous call left the \n character in the buffer. This can't get past that.

Use fgets() instead of fscanf() to read a whole line.

    char buffer[255];
    while(fgets(buffer, sizeof buffer, f))
        if (buffer[0] == usr)
            fprintf(f2, "%s", buffer);
情丝乱 2025-02-12 19:53:29

我不会使用fscanf读取文本行。

//fi - input file
//fo - output file
int copyLines(FILE *fi, FILE *fo, char c)
{
    char line[256];
    if(fi && fo)
    {
        while(fgets(line, sizeof(line), fi))
        {
            if(*line = c)
                if(fputs(line, fo) == EOF) return EOF;
        }
    }
    return 0;
}

I would not use fscanf to read line of text.

//fi - input file
//fo - output file
int copyLines(FILE *fi, FILE *fo, char c)
{
    char line[256];
    if(fi && fo)
    {
        while(fgets(line, sizeof(line), fi))
        {
            if(*line = c)
                if(fputs(line, fo) == EOF) return EOF;
        }
    }
    return 0;
}
感悟人生的甜 2025-02-12 19:53:29

对于初学者,fscanf的呼叫中的格式字符串

while(fscanf(f, "%[^\n]s", buffer)==1)
                      ^^^

至少不正确,您应该删除字母s

另一个问题是,在fscanf的呼叫之后,新行字符'\ n'未读取。因此,fscanf的下一个呼叫读取一个空字符串。

最好使用另一个C函数fgtes。但是您需要谨慎使用它。

通常,在输入文件中存储的字符串大于数组buffer的大小。

这意味着您需要使用fgets的多个呼叫来读取输入文件中的某些字符串。否则,输出文件将被错误地形成。

循环可以看以下方式

int success = 1;

do
{
    success = fgets( buffer, sizeof( buffer ), f ) != NULL;
    if ( success )
    {
        int target = *buffer == usr;

        if ( target ) fprintf( f2, "%s", buffer );
        while ( success && !strchr( buffer, '\n' ) )
        { 
            success = fgets( buffer, sizeof( buffer ), f ) != NULL;
            if ( success && target ) fprintf( f2, "%s", buffer );
        }
    }
}  while ( success );

For starters the format string in the call of fscanf is incorrect

while(fscanf(f, "%[^\n]s", buffer)==1)
                      ^^^

At least you should remove the letter s.

Another problem is that after such a call of fscanf the new line character '\n' is not read. So the next call of fscanf reads an empty string.

It is better to use another C function fgtes. But you need to use it with a caution.

In general a string stored in the input file can be greater than the size of the array buffer.

That means that you need to read some strings in the input file using more than one call of fgets. Otherwise the output file will be formed incorrectly.

The loop can look the following way

int success = 1;

do
{
    success = fgets( buffer, sizeof( buffer ), f ) != NULL;
    if ( success )
    {
        int target = *buffer == usr;

        if ( target ) fprintf( f2, "%s", buffer );
        while ( success && !strchr( buffer, '\n' ) )
        { 
            success = fgets( buffer, sizeof( buffer ), f ) != NULL;
            if ( success && target ) fprintf( f2, "%s", buffer );
        }
    }
}  while ( success );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文