Editing a File in C

S

Sanchit

I want to know thta how can i edit a file in C

For Example my file is

Mr XyZ FFFFFF 65


And now i want go change this number 65 to 87.... how can i Do
this..... I dont want to make a new file but want to edit this file
only.
 
J

jacob navia

Sanchit said:
I want to know thta how can i edit a file in C

For Example my file is

Mr XyZ FFFFFF 65


And now i want go change this number 65 to 87.... how can i Do
this..... I dont want to make a new file but want to edit this file
only.
0: Open the file.
1: Go to the place where the character '6' is stored.
2: Write the character '8'
3: Go to the place where the character '5' is stoed.
4: Write a '7' in there.
 
S

Sanchit

int main()
{
FILE * fp;
char test[] = "XYZ", test1[] = "5", test2[]="8";
fp = fopen("test.gs","a+");
fprintf(fp,"%-15s",test);
fprintf(fp,"%s",test1);
fseek(fp, -1L*(sizeof(test1)),SEEK_CUR);
fprintf(fp,"%s",test2);
fclose(fp);
return 0;
}

I have tried to overwrite like this. But its not overwriting.
 
D

Default User

Sanchit said:
int main()
{
FILE * fp;
char test[] = "XYZ", test1[] = "5", test2[]="8";
fp = fopen("test.gs","a+");
fprintf(fp,"%-15s",test);
fprintf(fp,"%s",test1);
fseek(fp, -1L*(sizeof(test1)),SEEK_CUR);
fprintf(fp,"%s",test2);
fclose(fp);
return 0;
}

I have tried to overwrite like this. But its not overwriting.

The "a+" flag says that all writes will be appends. Try "r+".



Brian
 
J

jacob navia

Default said:
Sanchit said:
int main()
{
FILE * fp;
char test[] = "XYZ", test1[] = "5", test2[]="8";
fp = fopen("test.gs","a+");
fprintf(fp,"%-15s",test);
fprintf(fp,"%s",test1);
fseek(fp, -1L*(sizeof(test1)),SEEK_CUR);
fprintf(fp,"%s",test2);
fclose(fp);
return 0;
}

I have tried to overwrite like this. But its not overwriting.

The "a+" flag says that all writes will be appends. Try "r+".



Brian


AND OPEN THE FILE WITH THE BINARY FLAG ON, like
"rb+"

!!!!!!!!!!!

jacob
 
S

Sanchit

Default said:
Sanchit wrote:
int main()
{
FILE * fp;
char test[] = "XYZ", test1[] = "5", test2[]="8";
fp = fopen("test.gs","a+");
fprintf(fp,"%-15s",test);
fprintf(fp,"%s",test1);
fseek(fp, -1L*(sizeof(test1)),SEEK_CUR);
fprintf(fp,"%s",test2);
fclose(fp);
return 0;
}
I have tried to overwrite like this. But its not overwriting.
The "a+" flag says that all writes will be appends. Try "r+".

AND OPEN THE FILE WITH THE BINARY FLAG ON, like
"rb+"

!!!!!!!!!!!

jacob

Hey thanks its working now......
But why should i open it in binary mode???????
 
K

Keith Thompson

Sanchit said:
I want to know thta how can i edit a file in C

For Example my file is

Mr XyZ FFFFFF 65


And now i want go change this number 65 to 87.... how can i Do
this..... I dont want to make a new file but want to edit this file
only.

It rarely makes sense to do this for text files. For example, if you
wanted to change "XyZ" to "wXyZ", you'd have to rewrite everything in
the file after that point; you can modify parts of a file in place,
but there's no defined operation for inserting or deleting pieces of a
file.

Text editors, for example, will typically write to a new file, then
rename it to the original name -- or rename the orginal file before
writing a new copy.

If you know that your modifications will never change the size,
though, you should be able to do it.
 
O

osmium

Sanchit said:
On Jul 4, 12:17 pm, jacob navia <[email protected]> wrote:
Hey thanks its working now......
But why should i open it in binary mode???????

Read the following link. There are also several discussions on google
groups about this subject. Even though you have it working, doing what you
want to do is a hazardous undertaking. Most programmers would want a really
good justification before they modified a file in place.

http://en.wikipedia.org/wiki/C_file_input/output
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,174
Latest member
RonnyLoren

Latest Threads

Top