LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-09-2004, 01:05 AM   #1
ananthbv
Member
 
Registered: Nov 2003
Posts: 49

Rep: Reputation: 15
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?
 
Old 07-09-2004, 08:54 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 56
You have the right idea: It's a headache. You have to shift text around after the insertion/deletion point.
 
Old 07-09-2004, 10:13 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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
 
Old 07-09-2004, 01:49 PM   #4
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

Rep: Reputation: 66
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
 
Old 07-09-2004, 10:45 PM   #5
th3_d0c
Member
 
Registered: Nov 2003
Location: New Hampshire
Distribution: Slack 9.2
Posts: 39

Rep: Reputation: 15
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.
 
Old 07-09-2004, 10:57 PM   #6
th3_d0c
Member
 
Registered: Nov 2003
Location: New Hampshire
Distribution: Slack 9.2
Posts: 39

Rep: Reputation: 15
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
 
Old 07-11-2004, 11:37 PM   #7
ananthbv
Member
 
Registered: Nov 2003
Posts: 49

Original Poster
Rep: Reputation: 15
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.
 
Old 07-13-2004, 11:40 PM   #8
th3_d0c
Member
 
Registered: Nov 2003
Location: New Hampshire
Distribution: Slack 9.2
Posts: 39

Rep: Reputation: 15
They are both C++. But, if you need any pointers, give email me..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting empty line at end of text file in BASH human2.0 Linux - General 8 04-01-2009 02:44 AM
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 03:28 AM
Convert special characters in text file nyk Linux - Software 1 01-05-2005 03:20 PM
Deleting single space characters from a file NeoDaNeenjah Linux - Newbie 2 03-16-2004 02:27 AM
inserting text into a file DavidPhillips Programming 5 08-15-2003 04:53 PM


All times are GMT -5. The time now is 05:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration