473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Editing a File in C

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.

Jul 4 '07 #1
7 1386
Sanchit wrote:
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.
Jul 4 '07 #2
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.

Jul 4 '07 #3
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+".

Brian

--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Jul 4 '07 #4
Default User wrote:
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+".

Brian

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

!!!!!!!!!!!

jacob
Jul 4 '07 #5
On Jul 4, 12:17 pm, jacob navia <j...@jacob.remcomp.frwrote:
Default User wrote:
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+".
Brian

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???????

Jul 4 '07 #6
Sanchit <sa************@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 4 '07 #7
"Sanchit" wrote:
On Jul 4, 12:17 pm, jacob navia <j...@jacob.remcomp.frwrote:
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
Jul 4 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sam.collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8"...
8
by: Sam Collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8"...
12
by: Ron Weldy | last post by:
I have a test server runinng 2003/IIS 6 with a mixture of asp and asp.net files. On my workstation I have a share set up to the folder where the web files reside. I am just doing quick and dirty...
0
by: Vanga Sasidhar | last post by:
Hi All, Please help me in the following problem. I am having some files with AVI extension. I want to make two programs in Visual Basic .NET which will work with these AVI Files. One program...
12
by: Thomas Bartkus | last post by:
Does anyone use emacs together with both WordStar key bindings and python mode? I'm afraid that Wordstar editing key commands are burned R/O into my knuckles! I would like to play with emacs...
8
by: D | last post by:
Hi, I currently have a Python app with a Tkinter GUI frontend that I use for system administration. Everytime it launches, it reads a text file which contains info about each host I wish to...
2
by: ritesh | last post by:
Hi, I'm facing a problem in which I need to edit an already created file, and the editing needs to be done at the start of the file rather then appending to the file. OS - Linux,Solaris ...
0
by: Frnak McKenney | last post by:
Can I use a bound ComboBox for both browsing and editing? I'm working on a small, standalone database application using Visual C#.NET 2003 and an Access data file. In order to keep the number...
5
by: Zytan | last post by:
I cannot stand being unable to change the code while the debugger is running. Is there a way to do this? thanks, Zytan
10
by: PhilSorum | last post by:
Hi all. I've to edit a txt file, deleting a given word. My approach is to open the file and read it char by char, copying that to a second file. I use a buffer to identify the given word, so if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.