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

how to remove and replace some char in a file(in c++)?

6
i want to remove some character in my file. too i want to replace some char to another. i know the easiest way for this job is we write final text in file. i mean we first produce sightly text then write it in file .i don't want use of temporary string.what do i do for solving this problem ?
Jun 3 '14 #1
3 2225
weaknessforcats
9,208 Expert Mod 8TB
This is not as easy as it looks.

The common method is to read the file and write it to a temporary file skipping the data to be deleted. Then copying the temporary file back on top of the original.

Depending upon the format of the file, that is, if it is a non-compressed non-encrypted text file you can locate the start and end of the data to be deleted and then copy from the end of the deleted data to the start of the deleted data shifting the last portion of the file forward. You will have bytes allocated to the file at the end that are not part of the shortened file so you will need to reposition the end of file marker. This is not a beginner problem.
Jun 3 '14 #2
mmag
6
i don't understand your way.if possible you write code.are you sure this a very hard problem?i thought a lot but for per algorithm i needed to temporary string or file.this is assistant's homework.it can't very hard.
Jun 3 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
The thing with removing data from a file is that the file gets shorter. That means the excess after the data needs to be removed. So removing the data just adds bytes to the end of the file that aren't part of the file. Ugly.

If you change the data to be deleted to blanks, the data is still there. Now it's blanks and nothing was deleted.

If you change the data t be deleted to some non-data value like 003, then the 003 are there and now every function that reads the file has to smart enough to know that 003 is not a valid data value. Plus now you can't have value 003 in your file. Bad choice.

By far the simplest approach is to read the file and write the data to a temporary file skipping the data to deleted and then copying the temporary back over the original. I know you prefer not to use a temporary but that is the easiest way to go.
Jun 3 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Marc Robitaille | last post by:
Hello, I think it is a easy question but I don't have any clue how to do it...Is it possible to replace the char that a user type in a textbox with an other one in the OnKeyPress methode? My...
4
by: శ్రీనివాస | last post by:
Hai friends, Can any one tell me how can i remove a character from a unocode text. కల్‌&హార is a Telugu word in Unicode. Here i want to remove '&' but not replace with a zero width...
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
7
by: pbd22 | last post by:
Hi. How Do I "UPDATE" a previously created string? I have a problem where an XML string created in an event handler fails because the string doesn't "UPDATE" each time the event hanlder fires,...
4
by: whatnameisnottaken | last post by:
right now i have gotten all the classes to print out using helpdbg. Now i want to remove the duplicate class names out of an array so i can just print one of each. This is the output i have. ...
4
by: viba | last post by:
Hi all, I am trying a strncpy (tempstr,srcstrng,sizeof(tempstr)); where in char tempstr; char srcstng; in this case in the destination array many free mem is present & when i retrive...
2
by: anii | last post by:
Hi there, I've got this piece of code, but I'm having trouble getting it to write the changes into the file. What I'm trying to do is: Read text file Find user specified character (eg. what...
6
maylortaylor
by: maylortaylor | last post by:
int index = -1; string NewStr = null; char lower = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.