如何在 C 函数中浏览文件时使用模数
How to use modulo while browsing a file in a function in C
我不知道如何编写一个函数来浏览具有特定模数的文件。
例如,如果我有 3 作为模并且一个文件包含:
abcdefghijk
那么,我的函数应该 returns :
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;
}
我不知道如何编写一个函数来浏览具有特定模数的文件。
例如,如果我有 3 作为模并且一个文件包含:
abcdefghijk
那么,我的函数应该 returns :
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;
}