473,395 Members | 1,692 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,395 software developers and data experts.

How to delete a line or a character?

Hi!
I am a beginner of C.

I want to delete some characters or a whole line in a text file. I
have tried the "fprintf", "fputs", "fwrite", but no one of them can
run rightly.

In the below codes, the "str1" is one text line , in fact. I want
replace "str1" (the whole line) with the "str3". In my case, "str3" is
shooter than "str1", But I will get some strange code that can not be
read.

Thanks in advance!

if ((cfPtr = fopen("client.dat","r+")) == NULL)
printf ("File counld not be opened. \n");
else
{
while (!feof(cfPtr))
{fgets(str1, 200, cfPtr);
printf ("OK!");
if (strstr(str1, str2))
printf ("NO!");
{fseek (cfPtr, ftell(cfPtr)-strlen(str1)-1,
SEEK_SET);
fwrite (str3, strlen(str1), 1, cfPtr);
exit();
}
}
fclose(cfPtr);
}

--
Dong Ge
2004-10-04
Nov 14 '05 #1
8 4746
Dong Ge wrote:
Hi!
I am a beginner of C.

I want to delete some characters or a whole line in a text file. I
have tried the "fprintf", "fputs", "fwrite", but no one of them can
run rightly.

In the below codes, the "str1" is one text line , in fact. I want
replace "str1" (the whole line) with the "str3". In my case, "str3" is
shooter than "str1", But I will get some strange code that can not be
read.

Thanks in advance!

if ((cfPtr = fopen("client.dat","r+")) == NULL)
You use cfPts to open a file.
printf ("File counld not be opened. \n");
else
{
while (!feof(cfPtr))
{fgets(str1, 200, cfPtr);
printf ("OK!");
if (strstr(str1, str2))
printf ("NO!");
{fseek (cfPtr, ftell(cfPtr)-strlen(str1)-1,
SEEK_SET);
fwrite (str3, strlen(str1), 1, cfPtr);
You are using the same handle to write again ?
Are you sure you wanna do this ?
May be, you want to read the contents of the file to a buffer
and then write again.

You have not mentioned how you are opening the file !

exit();


After writing, you are exiting. Well - you are not closing the file.
That would imply that:

* There is no guarantee that the file contents are going to be written.
* <OT>
whatever the lock acquired by the underlying OS over the file
resource may not be necessary given back. But I think this depends on
the implementation, but nevertheless this is something you have to pay
heed to.
</OT>.

--
Karthik.
http://akktech.blogspot.com .
Nov 14 '05 #2
Hi! Karthik,

I guess your maybe not reading my codes carefully. I use the same
handle "cfPtr" to open, write and close the same file, not "cfPts". In
fact, the code can be compiled and run.

I just want to know how to delete a text line or some characters.

Thanks for your reply.
--
Dong Ge
2004-10-05

On Mon, 04 Oct 2004 14:46:52 -0700, Karthik Kumar
<ka*******************@yahoo.com> wrote:
Dong Ge wrote:
Hi!
I am a beginner of C.

I want to delete some characters or a whole line in a text file. I
have tried the "fprintf", "fputs", "fwrite", but no one of them can
run rightly.

In the below codes, the "str1" is one text line , in fact. I want
replace "str1" (the whole line) with the "str3". In my case, "str3" is
shooter than "str1", But I will get some strange code that can not be
read.

Thanks in advance!

if ((cfPtr = fopen("client.dat","r+")) == NULL)


You use cfPts to open a file.
printf ("File counld not be opened. \n");
else
{
while (!feof(cfPtr))
{fgets(str1, 200, cfPtr);
printf ("OK!");
if (strstr(str1, str2))
printf ("NO!");
{fseek (cfPtr, ftell(cfPtr)-strlen(str1)-1,
SEEK_SET);
fwrite (str3, strlen(str1), 1, cfPtr);


You are using the same handle to write again ?
Are you sure you wanna do this ?
May be, you want to read the contents of the file to a buffer
and then write again.

You have not mentioned how you are opening the file !

exit();


After writing, you are exiting. Well - you are not closing the file.
That would imply that:

* There is no guarantee that the file contents are going to be written.
* <OT>
whatever the lock acquired by the underlying OS over the file
resource may not be necessary given back. But I think this depends on
the implementation, but nevertheless this is something you have to pay
heed to.
</OT>.


Nov 14 '05 #3
Dong Ge wrote:
Hi! Karthik,

I guess your maybe not reading my codes carefully. I use the same
handle "cfPtr" to open, write and close the same file, not "cfPts". In
fact, the code can be compiled and run.
Oops - Sorry. That was a typo. But my question remains the same -
how could you use the same handle to read and write .

I just want to know how to delete a text line or some characters.

Read the contents of the file.
Close the file.

Apply the filter on the contents that you have read in step 1.
Open a file and write the contents.


Thanks for your reply.
--
Dong Ge
2004-10-05


And please don't top-post.

--
Karthik.
http://akktech.blogspot.com .
------------ And now a word from our sponsor ------------------
For a quality usenet news server, try DNEWS, easy to install,
fast, efficient and reliable. For home servers or carrier class
installations with millions of users it will allow you to grow!
---- See http://netwinsite.com/sponsor/sponsor_dnews.htm ----
Nov 14 '05 #4
Karthik Kumar wrote:
Dong Ge wrote:
Hi! Karthik,

I guess your maybe not reading my codes carefully. I use the same
handle "cfPtr" to open, write and close the same file, not "cfPts". In
fact, the code can be compiled and run.

Oops - Sorry. That was a typo. But my question remains the same - how
could you use the same handle to read and write .

I just want to know how to delete a text line or some characters.


Read the contents of the file.
Close the file.

Apply the filter on the contents that you have read in step 1.
Open a file and write the contents.


To add further,

remove the original file.
rename the new file to the original file.

--
Karthik.
http://akktech.blogspot.com .
Nov 14 '05 #5
In article <news:kt********************************@4ax.com >
Dong Ge <ha******@21cn.net> wrote:
while (!feof(cfPtr))


Bug here, virtually guaranteed. If you ever see "while (!feof(...))"
in a C program, look for the bug. There will be one, at least 99%
of the time.

Enormous hint: see the comp.lang.c FAQ, question 12.2.

For the answer to the question in the "Subject:" line, see the
comp.lang.c FAQ as well.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6
In the comp.lang.c FAQ :

Question 19.14:
How can I insert or delete a line (or record) in the middle of a file?
--------------------------------------------------------------------------------

Short of rewriting the file, you probably can't. The usual solution is
simply to rewrite the file. (Instead of deleting records, you might
consider simply marking them as deleted, to avoid rewriting.) See also
questions 12.30 and 19.13.

Question 12.30
I'm trying to update a file in place, by using fopen mode "r+",
reading a certain string, and writing back a modified string, but it's
not working.
--------------------------------------------------------------------------------

Be sure to call fseek before you write, both to seek back to the
beginning of the string you're trying to overwrite, and because an
fseek or fflush is always required between reading and writing in the
read/write "+" modes. Also, remember that you can only overwrite
characters with the same number of replacement characters; see also
question 19.14.

" ALSO, REMEMBER THAT YOU CAN ONLY OVERWRITE CHARACTERS WITH THE SAME
NUMBER OF REPLACEMENT CHARACTERS."
This means I can't write my code with the standdard lib?

----
Dong Ge
2004-10-05

On Tue, 05 Oct 2004 12:57:36 +0800, Dong Ge <ha******@21cn.net> wrote:
Hi! Karthik,

I guess your maybe not reading my codes carefully. I use the same
handle "cfPtr" to open, write and close the same file, not "cfPts". In
fact, the code can be compiled and run.

I just want to know how to delete a text line or some characters.

Thanks for your reply.


Nov 14 '05 #7
Hi! Karthik,
The orginal text file contained thousands of lines, if I always remove
the original file and rename the new file. Maybe the program will run
very slowly.

Tons of thanks for your help!
--
Dong Ge
2004-10-05

On Mon, 04 Oct 2004 22:09:01 -0700, Karthik Kumar
<ka*******************@yahoo.com> wrote:
Karthik Kumar wrote:
Dong Ge wrote:
Hi! Karthik,

I guess your maybe not reading my codes carefully. I use the same
handle "cfPtr" to open, write and close the same file, not "cfPts". In
fact, the code can be compiled and run.

Oops - Sorry. That was a typo. But my question remains the same - how
could you use the same handle to read and write .

I just want to know how to delete a text line or some characters.


Read the contents of the file.
Close the file.

Apply the filter on the contents that you have read in step 1.
Open a file and write the contents.


To add further,

remove the original file.
rename the new file to the original file.


Nov 14 '05 #8
Dong Ge wrote:
In the comp.lang.c FAQ :

Question 19.14:
How can I insert or delete a line (or record) in the middle of a file?
--------------------------------------------------------------------------------

Short of rewriting the file, you probably can't. The usual solution is
simply to rewrite the file. (Instead of deleting records, you might
consider simply marking them as deleted, to avoid rewriting.) See also
questions 12.30 and 19.13.

Question 12.30
I'm trying to update a file in place, by using fopen mode "r+",
reading a certain string, and writing back a modified string, but it's
not working.
--------------------------------------------------------------------------------

Be sure to call fseek before you write, both to seek back to the
beginning of the string you're trying to overwrite, and because an
fseek or fflush is always required between reading and writing in the
read/write "+" modes. Also, remember that you can only overwrite
characters with the same number of replacement characters; see also
question 19.14.

" ALSO, REMEMBER THAT YOU CAN ONLY OVERWRITE CHARACTERS WITH THE SAME
NUMBER OF REPLACEMENT CHARACTERS."
This means I can't write my code with the standdard lib? Of course you could try padding with spaces if that is compatible with
how you read the file again at a later time.
----
Dong Ge
2004-10-05

On Tue, 05 Oct 2004 12:57:36 +0800, Dong Ge <ha******@21cn.net> wrote:

Hi! Karthik,

I guess your maybe not reading my codes carefully. I use the same
handle "cfPtr" to open, write and close the same file, not "cfPts". In
fact, the code can be compiled and run.

I just want to know how to delete a text line or some characters.

Thanks for your reply.


Nov 14 '05 #9

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

Similar topics

6
by: Carol | last post by:
With ASP, I am generating a huge CSV file. It is to large to open in notepad. Can I use the Filesystem Scripting Object to read the very last line of the file and delete the very last...
3
by: Noah | last post by:
I have a text field in a form. I want the user to be able to click a DELETE button and have the character at the cursor position deleted. This would be just as if the user had pressed the Back...
10
by: Alex Vinokur | last post by:
GNU g++ 3.3.3, Cygwin // Stuff static char* mbuffer = NULL; // Stuff void doit()
26
by: S!mb | last post by:
Hi all, I'm currently developping a tool to convert texts files between linux, windows and mac. the end of a line is coded by 2 characters in windows, and only one in unix & mac. So I have to...
6
by: I am Sam | last post by:
I keep getting this error and I don't know why: The path is too long after being fully qualified. Make sure path is less than 260 characters. Description: An unhandled exception occurred...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. ...
1
by: jmarcrum | last post by:
Hello all! i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After...
5
by: dudeja.rajat | last post by:
Sorry : Earlier mail had a typo in Subject line which might look in-appropriate to my friends Hi, I've a list some of whose elements with character \. I want to delete this last character...
0
by: norseman | last post by:
dudeja.rajat@gmail.com wrote: ============================== depending on OS and other factors, the DirList may be more like: ABDIR\ 41 42 44 49 52 5C 0A nextDir If so endswith needs to...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.