file updation in vc++

Started by
7 comments, last by mikfig 13 years, 11 months ago
hii everyone1.. how can i update a text file i,e only one particular record in a file i'm using openGL with vc++ ... my sample code to write into a text file when a store button is clicked is over here..after every 8sec i have to update only x_quad and y_quad how can that be done..can any one help me out?? case STORE_BUTTON: fptr=fopen ("target.txt", "w"); fclose(fptr); fptr=fopen("target.txt","a+"); if(radiogroup_item_id==0) strng="incomming"; else if(radiogroup_item_id==1) strng="outgoing"; compute(az,sr,er,strng); if(no>=1) { no=no-1; if(tid <= 10) { fprintf(fptr, "Target id=%d\n", tid); fprintf(fptr, "%s\n", strng); fprintf(fptr, "speed=%f\n", speed); fprintf(fptr, "Azimuth=%d\n", az); fprintf(fptr, "Heading=%d\n", hh); fprintf(fptr, "Start range=%d\n", sr); fprintf(fptr, "End Range=%d\n",er); fprintf(fptr, "Point: %f, %f\n\n", X_quad,y_quad); } else if(no==0 || tid>10) fclose(fptr); } tid++; break;
Advertisement
if that is the ONLY line you need to edit, then store the position (ftell) before you write out that line.
When you need to update that line, fseek to the stored location, then fprintf the new data.
Just note, you will want to use the format flags for the floats, or write out some extra spaces, since the update may not take up the same room in the file.
If there is any data after that "Point " line for x_quad,y_quad it would be erased using this method. You'd have to move everything down manually.

Also, you can't use the "a+" mode for this. Append mode forces all write to append to the end of the file. You'd have to use the "w" mode.
Can you give us a high level overview of what you are doing? It sounds possibly overcomplicated.
What exactly do you want to do, and why?
Are you trying to do something like update the data, user config, or maybe in game player status that changes on-the-fly?
Hi every1...
Actualy I'm simulating a Radar diplay using openGL.It has user interface part, in the i'm inputing the target data in contols..and once if i click store button all the target data is stored in a file!!

Here first i'll calculate the first position of the target(x_quad and y_quad) and store it n a file.. and once when i call the compute function it calculates the next position int same variables x_quad and y_quad..now i have to replace the previous value of x_quad and y_quad value what is stored in file..
how can that be done can any1 help out??

I tried doing what kuleran has said..but couldn't replace that line.. instead it writes the new value n the new line as below..

........................
1
incomming
50.000000
30
0
60
0.000000 0.000000

12.000000 20.784567
.........................

my sample code is over here.........hlp me out plz...........

case STORE_BUTTON:
fptr=fopen ("target.txt", "w");
fclose(fptr);

fptr=fopen("target.txt","a+");
if(radiogroup_item_id==0)
strng="incomming";
else if(radiogroup_item_id==1)
strng="outgoing";
if(no>=1)
{
no=no-1;
if(tid <= 10)
{


printf("STORE Button clicked....Stored to a file!\n");
fprintf(fptr, "%d\n", tid); //target ID
fprintf(fptr, "%s\n", strng);//Type of target
fprintf(fptr, "%f\n", speed);//speed
fprintf(fptr, "%d\n", az);//angle
fprintf(fptr, "%d\n", sr);//start range
fprintf(fptr, "%d\n",er);//end range
int y = ftell(fptr);
//Current position of the target x_quad and y_quad stored in file
fprintf(fptr, "%f %f\n\n", x_quad, y_quad);
//compues the next position of target
compute(speed,az,sr,er,strng);
}
fscanf(fptr,"%f%f",&x_quad,&y_quad);
fseek(fptr, y, 0);
fprintf(fptr,"%f %f\n\n",x_quad,y_quad);
if(no==0 || tid>10)
fclose(fptr);
}
tid++;
break;
}
}



It looks like its because the mode in which you opened the file. You specified 'a+' as the file open mode and that means "open for reading and writing (append if file exists)". So when you write to the file it will add to it rather than allow you to change existing lines. So I think the mode you want to use is w+ which is "open for reading and writing (overwrite file)".

So to change that then you have to change your fopen function call from:

fptr=fopen("target.txt","a+");

to:

fptr=fopen("target.txt","w+");
"WARNING: Excessive exposure to politicians and other bureaucrats has been linked to aggressive behavior." - Henk Hopla
hmm..ya i got it..but,i don wan to rewrite the whole file..only the last line i have to update every time..can u plz tell m how can that be done??
I think you want the r+ mode then.

r+ - open for reading and writing, start at beginning
"WARNING: Excessive exposure to politicians and other bureaucrats has been linked to aggressive behavior." - Henk Hopla

This topic is closed to new replies.

Advertisement