FILE IN OUT probelem

W+ dose not mean i can read and write the file in the meanwhile?i use printf("hi") to detect whether the file has been read,but the result is not.Therefore,can someone tell me how to read the file in w+ version?thank a lot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include<stdio.h>
addnum(){
	FILE *fp;
	int n[3],i=0;
	fp=fopen("sam.txt","w+");
	if(fp==NULL){
		printf("The file cannot be opened");
	}
	else{
		while(i<3){
		scanf("%d",&n[i]);	
		i++;
		}
		i=0;
		while(i<3){
		fprintf(fp,"%d\n",n[i]);	
		i++; 
		}
		
		fclose(fp);
	}
}
readnum(){
	FILE *fp;
		int n[3],s,i=0;
		fp=fopen("sam.txt","w+");
	if(fp==NULL){
		printf("The file cannot be opened");
	}
	else{
		while((s=fscanf(fp,"%d",&n[i]))==1){
			printf("hi");
			i++;
		}
		fclose(fp);
	}
}
int main(){
	addnum();
	readnum();
}
When you open the file with "w+" the old file content will get deleted. You propably want to use "r" or "r+" in readnum().
sorry,but if i use r+,how can i delete the data of the txt file
You want readnum() to delete the file data?
yes,bro,if i use w/w+,the whole txt file will be over write,but if i use r/r+,it just add some data in the txt file,it cannot remove the data form the txt file.
In that case I don't understand the problem. Sorry.
Topic archived. No new replies allowed.