LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   inserting/deleting characters into a text file (https://www.linuxquestions.org/questions/programming-9/inserting-deleting-characters-into-a-text-file-202905/)

ananthbv 07-09-2004 01:05 AM

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:

itsme86 07-09-2004 08:54 AM

You have the right idea: It's a headache. You have to shift text around after the insertion/deletion point.

jim mcnamara 07-09-2004 10:13 AM

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

keefaz 07-09-2004 01:49 PM

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

th3_d0c 07-09-2004 10:45 PM

Here is a similar program I had to write for class this past semester. You could use it as a point of reference.

Quote:

//By: Robert
//Due: 02 April 2004

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
string paragraph, changefrom, changeto, changefrom2;
char repeat=0;
int n, m, length=99, pos;
ifstream fin;
ofstream fout;
cout<<"***IF YOU WANT TO END THE PROGRAM HIT THE KEY COMBINATION 'ctrl'+'c'\n\n";
cout<<"Opening 'Paragraph.txt' please wait a minute\n\n";

fin.open("paragraph.txt");
getline(fin, paragraph, '!');

while(repeat==0) //Sets up the repeat in for the word change
{

cout<<paragraph<<endl;
fin.close();
cout<<"What do you want to change: "; //Asks what the user wants to
cin>>changefrom; //change in the paragraph

cout<<"\nWhat do you want to change '"; //Asks what the users wants to
for(m=0; m<changefrom.length(); m++) //change the first world to
cout<< changefrom[m];
cout<<"' to: ";
cin>>changeto; //The two for loops are created to
//make a count of the number of characters
cout<<"\nChanging '"<<changefrom<<"' to '"; //the word takes up
for(n=0; n<changeto.length(); n++) //So I will be able to delete the correct
cout<<changeto[n]; //number of spaces out of the paragraph after
cout<<"'."; //after I insert the new world

do
{
pos = paragraph.find(changefrom); //Finds the position of the first world
paragraph.insert(pos, changeto); //Inserts the second word at the beginning of the first words position
paragraph.erase(pos+n, m); //Erases the first world by adding the length of the second(n) to the position
}while(paragraph.find(changefrom) != -1); //than deleting the length of the first(m) word

cout<<"\n\n"<<paragraph<<endl;

fout.open("paragraph.txt");
fout<<paragraph; //Shows the new paragraph and saves to the original file
fout.close();

cout<<endl<<endl<<endl;

}
return 0;
}
It opens and reads the text in 'paragraph.txt' and inputs it to the paragraph string and than finds changefrom in the paragraph string and replaces it in the paragraph. You will need to probably read in similarly and you could find 'a' and replace it with 'a simple'
I believe i have another program that will actually do more of what you want.

th3_d0c 07-09-2004 10:57 PM

This one may be a little better:


Quote:

#include <iostream>
#include <fstream>
using namespace std;

struct node
{
double data;
node *next_ptr;
};

void enter_list(node* &begin_ptr1, node* &begin_ptr2);
void display_list(node* begin_ptr1);
void delete_value(node* &begin_ptr, int value);
void search_value(node* &begin_ptr, int value);
void insert_value(node* &begin_ptr, int value, int where);

int main()
{
node *time, *amp;

enter_list(time, amp);
display_list(time);
display_list(amp);


return 0;
}

void enter_list(node* &begin_ptr1, node* &begin_ptr2)
{
node *current_ptr1, *previous_ptr1, *current_ptr2, *previous_ptr2;
double time, amp;
ifstream fin;
fin.open("eg131_lab03_03.txt");

fin >> time >> amp;
current_ptr1 = new node;
begin_ptr1 = current_ptr1;
current_ptr1->data = time;
current_ptr1->next_ptr = NULL;
previous_ptr1 = current_ptr1;

current_ptr2 = new node;
begin_ptr2 = current_ptr2;
current_ptr2->data = amp;
current_ptr2->next_ptr = NULL;
previous_ptr2 = current_ptr2;

while(!fin.eof())
{
fin >> time >> amp;
current_ptr1 = new node;
current_ptr1->data = time;
current_ptr1->next_ptr = NULL;
previous_ptr1 = current_ptr1;
previous_ptr1->next_ptr = current_ptr1;

current_ptr2 = new node;
current_ptr2->data = amp;
current_ptr2->next_ptr = NULL;
previous_ptr2 = current_ptr2;
previous_ptr2->next_ptr = current_ptr2;

}
}

void display_list(node *begin_ptr)
{
node *current_ptr1, *current_ptr;

current_ptr = begin_ptr;
while (current_ptr!=NULL)
{
cout << current_ptr->data << " ";
current_ptr = current_ptr->next_ptr;
}
cout << endl;
}





void delete_value(node* &begin_ptr, int value)
{
node *current_ptr, *previous_ptr;

current_ptr = begin_ptr;
while (current_ptr->data!=value && current_ptr->next_ptr!=NULL)
{
previous_ptr = current_ptr;
current_ptr = current_ptr->next_ptr;
}

if (current_ptr!=begin_ptr && current_ptr->data==value)
{
previous_ptr->next_ptr = current_ptr->next_ptr;
delete current_ptr;
}
else if (current_ptr==begin_ptr && current_ptr->data==value)
{
begin_ptr = current_ptr->next_ptr;
delete current_ptr;
}
else
cout << "value not found" << endl;
}


void search_value(node* &begin_ptr, int value)
{
node *current_ptr, *previous_ptr;

current_ptr = begin_ptr;
while (current_ptr->data!=value && current_ptr->next_ptr!=NULL)
{
previous_ptr = current_ptr;
current_ptr = current_ptr->next_ptr;
}
if (current_ptr->data==value)
{
cout<<current_ptr<<"\t"<<current_ptr->data<<endl;
}
else
cout << "value not found" << endl;
}

void insert_value(node* &begin_ptr, int value, int where)
{
node *current_ptr, *previous_ptr, *temp_ptr;

current_ptr = begin_ptr;
while (current_ptr->data!=where && current_ptr->next_ptr!=NULL)
{
previous_ptr = current_ptr;
current_ptr = current_ptr->next_ptr;
}

if (current_ptr!=begin_ptr && current_ptr->data==where)
{
temp_ptr = new node;
temp_ptr->next_ptr = previous_ptr->next_ptr;
previous_ptr->next_ptr = temp_ptr;
temp_ptr->data = value;
}
else if (current_ptr==begin_ptr && current_ptr->data==where)
{
temp_ptr = new node;
temp_ptr->next_ptr = begin_ptr->next_ptr;
begin_ptr->next_ptr = temp_ptr;
temp_ptr->data = value;
}
else
cout << "value not found" << endl;
}
Example of what it could do:
Quote:

Results
enter the number of nodes to be entred = 5
enter the 5 nodes values :
1
2
3
4
5
1 2 3 4 5
data value to be entered: 0
what value should the data be put behind: 3
1 2 0 3 4 5
Press any key to continue

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

ananthbv 07-11-2004 11:37 PM

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.

th3_d0c 07-13-2004 11:40 PM

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.