我想控制文件指针的位置 [英] I want to control file pointer location
问题描述
我想找到重复的整数并将其位置替换为空白.这是我的功课.但是我没有解决这个问题. 我的代码是
I want to find the repeated integers and replace their positions with empty space. This is my homework. But I didn't solve this problem. My code is
int main(){
int control_integer;
int temp_integer;
int flag = 0;
int location;
char space = ' ';
char control_end_of_file;
FILE *fp,*tp;
fp = fopen("file.txt","r+");
while(!feof(fp))
{
fscanf(fp,"%d",&control_integer);
printf("control_integer: %d\n",control_integer );
while(!feof(fp)){
fscanf(fp,"%d",&temp_integer);
printf("temp_integer = %d\n",temp_integer );
if (temp_integer == control_integer)
{
flag = 1;
printf("girdi integer: %d\n",temp_integer );
fprintf(fp, " %c ",space );
}
}
}
return 0;
}
我按以下方式计划了我的职能.首先,我在文件中添加一个元素,然后转到新的循环并查看下一个元素.如果相等,我想用一个空格代替它.但是问题就在这里. * fp
,当执行读取操作时,下一个元素被传递,但是我想防止它并向该位置写入其他内容.因此,我想在同一位置进行写入和删除操作.我尝试了一些东西,但是做不到.
I planned my function as follows. First I take an element in the file, then I go to a new loop and look at the next elements. If it is equal, I want to replace it with a space. But the problem arises right here. * fp
, when the reading operation is performed, the next element is passed but I want to prevent it and write something else to that location. So I want to do both write and delete in the same place. I tried something, but I couldn't do it.
推荐答案
首先,我在文件中添加一个元素,...
First I take an element in the file,...
我认为在这种情况下int x; x = getc()
就足够了.例如:
I think in this case int x; x = getc()
is enough. E.g.:
while((x = getc(fp)) != EOF){printf("do something");};
这篇关于我想控制文件指针的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!