inserting/deleting characters into a text file
hi,
i have spent days trying trying to write a C program that inserts a character/characters in the middle of text in a file. for ex, there is this line "this is a line". i want to change it to "this is a single line". is it possible? i mean if i want to insert a single character, i must shift the rest to the right, which is a lot of headache. also, is there any way to deleting character/characters from a text file? :confused: |
You have the right idea: It's a headache. You have to shift text around after the insertion/deletion point.
|
Do what stream editors do use two files:
read a line of text if it needs changing, change it write the line of text to another output file When done reading, close files, rename the output file to the input file |
It exists more easy ways to do text changements with perl
perl -pi -e 's/this is a line/this is a single line/' file.txt |
Here is a similar program I had to write for class this past semester. You could use it as a point of reference.
Quote:
I believe i have another program that will actually do more of what you want. |
This one may be a little better:
Quote:
Quote:
Hmm, i think everything is good to go. But you may need to change the type to char since it is set to double at the top. PM me if you need more help. Good luck |
thanks everybody for all the replies.
yeah, with perl its a breeze. perl is superb for such stuff. th3_doc, the linked list idea is great. but in the other program you have used c++, which i know only slightly. but with your explanation, i am beginning to understand. thanks once again. |
They are both C++. But, if you need any pointers, give email me..
|
All times are GMT -5. The time now is 12:52 AM. |