文件操作 [英] FILE Operations
问题描述
有人可以编写示例c代码,显示如何在文件中找到
字或char的实例(包括打开和关闭文件)?谢谢
史蒂夫在28/07/04写道:有人可以写一个示例c代码,展示如何在文件中找到一个单词或char的实例(包括打开和关闭文件)?谢谢
这太过分了。你必须自己编写代码。
提示:打开你的C书。
-
Emmanuel
C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
C是一个尖锐的工具
>有人可以写一个示例c代码,显示如何在一个文件中找到一个
word或char的实例(包括打开和关闭文件)?谢谢
Steve,
此代码可让您计算给定char的实例:
#include< stdio。 h>
#include< string.h>
int main()
{
const char fileName [] =" fileName.txt";
const char toCount =''f'';
FILE * f = fopen(fileName," r");
if(!f)
{
printf(" File not found\\\
);
退出(0);
}
char s [100];
memset(s,' '\0'',100);
int count = 0;
while(fgets(s,100,f))
{
char * q =& s [0];
for(; * q!=''\'''; q ++)
{
if(* q == toCount)
count ++;
}
printf(" ;%s",s); / *打印每组字符* /
}
memset(s,''\'',100);
printf(你有%d \%c'' \" \ n",count,toCount);
fclose(f);
返回0;
}
请不要做其他人的家庭工作......
他们不会学习任何东西,他们会告诉他们的朋友:
让comp.lang.c的人做作业!
Can someone write a sample c code showing how one can find an instance of a
word or a char in a file (including opening and closing a file)? Thanks
Steve wrote on 28/07/04 :Can someone write a sample c code showing how one can find an instance of a
word or a char in a file (including opening and closing a file)? Thanks
This is going too far. You have to write the code yourself.
Hints: Open your C book.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
> Can someone write a sample c code showing how one can find an instance of
aword or a char in a file (including opening and closing a file)? Thanks
Hi Steve,
This code lets you count the instances of a given char:
#include <stdio.h>
#include <string.h>
int main()
{
const char fileName[] = "fileName.txt";
const char toCount = ''f'';
FILE* f = fopen(fileName, "r");
if(!f)
{
printf("File NOT found\n");
exit(0);
}
char s[100];
memset(s, ''\0'', 100);
int count = 0;
while (fgets(s, 100, f))
{
char * q = &s[0];
for(; *q != ''\0''; q++)
{
if(*q == toCount)
count++;
}
printf("%s", s);/* to print each set of chars*/
}
memset(s, ''\0'', 100);
printf("You have %d \"%c''s\"\n", count, toCount);
fclose(f);
return 0;
}
Please don''t do other''s home work...
They will not learn anything and they will tell their friends:
Just let comp.lang.c people do the homework!
这篇关于文件操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!