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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-09-2004, 01:05 AM
|
#1
|
Member
Registered: Nov 2003
Posts: 49
Rep:
|
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?
|
|
|
07-09-2004, 08:54 AM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
You have the right idea: It's a headache. You have to shift text around after the insertion/deletion point.
|
|
|
07-09-2004, 10:13 AM
|
#3
|
Member
Registered: May 2002
Posts: 964
Rep:
|
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
|
|
|
07-09-2004, 01:49 PM
|
#4
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552
|
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
|
|
|
07-09-2004, 10:45 PM
|
#5
|
Member
Registered: Nov 2003
Location: New Hampshire
Distribution: Slack 9.2
Posts: 39
Rep:
|
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.
|
|
|
07-09-2004, 10:57 PM
|
#6
|
Member
Registered: Nov 2003
Location: New Hampshire
Distribution: Slack 9.2
Posts: 39
Rep:
|
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
|
|
|
07-11-2004, 11:37 PM
|
#7
|
Member
Registered: Nov 2003
Posts: 49
Original Poster
Rep:
|
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.
|
|
|
07-13-2004, 11:40 PM
|
#8
|
Member
Registered: Nov 2003
Location: New Hampshire
Distribution: Slack 9.2
Posts: 39
Rep:
|
They are both C++. But, if you need any pointers, give email me..
|
|
|
All times are GMT -5. The time now is 02:57 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|