c - 如何在 C 函数中浏览文件时使用模数

标签 c function file modulo

我不知道如何编写一个使用特定模数浏览文件的函数。

例如,如果我有 3 作为模并且文件包含:

abcdefghijk 

然后,我的函数应该返回:

adgj

我的函数代码:

char f1(char* name_file, int modulo)
{
    char characters_ok[];
    unsigned char character_read;
    fileToOpen=fopen(name_file,"r");
    do
    {
        character_read=fgetc(fileToOpen);
        //and I don't know what to write...
    }
    while(!feof(fileToOpen));
    fclose(fileToOpen);
    return characters_ok;
}

最佳答案

已解决: 感谢评论中的人们,我的问题的答案是简单地计算我阅读的字符(使用递增计数器)并使用测试

compt % modulo) == 0

所以,完整的答案是:

       int* f1(char* name_file, int modulo)
{
    int* characters_ok;
    int compt=0;
    int character_read;
    fileToOpen=fopen(name_file,"r");
    if (fileToOpen == NULL)
    {
        printf("%s : ",name_file);
        errorfile();
    }
    while ((character_read = fgetc(fileToOpen)) != EOF)
    {
        if ((compt % modulo) == 0)
        {
           *(characters_ok++)=character_read;
        }
        compt++;
    }
    fclose(fileToOpen);
    return characters_ok;
}

关于c - 如何在 C 函数中浏览文件时使用模数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47982406/

相关文章:

php - 在 "Onsubmit"上调用多个函数

c++ - ...(省略号)作为函数原型(prototype)中唯一的函数参数在 C++ 中意味着什么?

c - 指针 typedef 结构在 C 中如何工作?

c - 在 C 中使用两个 fork() 时如何利用每个进程

c++ - 案例语句中的省略号是标准 C/C++

c - 问题在 C 中初始化双堆叠链表

function - Scala "def"方法声明 : Colon vs equals

java - 在 Java 中将文件从一个目录复制到另一个目录

windows - 共享 Windows 映射驱动器

spring - 从类路径加载文件