473,809 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Editing a text file, any easier way?

1 New Member
I just want to edit a txt and save the edited version.

In this case i replaced all the i's with q's.

Is there an easier way to do this? Without all the casting?

Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args){
  2.  
  3.      string[] text = File.ReadAllLines("test.txt");
  4.      char[] help;
  5.  
  6.      for (int i = 0; i < text.Length; i++) {
  7.  
  8.          help = text[i].ToArray();
  9.  
  10.          for (int j = 0; j < help.Length; j++){
  11.  
  12.               if (help[j] == 'i')
  13.                   help[j] = 'q';
  14.          }
  15.  
  16.          text[i] = new String(help);
  17.  
  18.      }
  19.      File.WriteAllLines("test_alt.txt",text);
  20. }
  21. }
Apr 21 '12 #1
1 1325
RhysW
70 New Member
Expand|Select|Wrap|Line Numbers
  1.             List<string> text = new List<string>();
  2.             string filelocation = null;
  3.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  4.             {
  5.                 filelocation = openFileDialog1.FileName.ToString();
  6.             }
  7.             foreach (string line in File.ReadLines(filelocation))
  8.             {
  9.                 string addthis;
  10.                 if (line.Contains("i"))
  11.                 {
  12.                     addthis = line.Replace("i", "j");
  13.                 }
  14.                 else
  15.                 {
  16.                     addthis = line;
  17.                 }
  18.                 text.Add(addthis);
  19.             }
  20.             File.WriteAllLines(filelocation, text);
there is this method, you throw it once into a string, replace all i's to j's and then write the list back, there is a fiddly bit in the middle where line isnt added but addthis is, beause i couldnt set it as line = line.replace("i ","j"); or the compiler throws a wobbly because line is used for the rest of the loop, so i made the new string addthis that is always added, if the string contains an i they are turned to j's, assigned to addthis, and add this gets added, otherwise addthis is just directly equal to line, im sure there is a neater way of doing this but you asked for me to get rid of the castings so this was a quick throw together.
Apr 24 '12 #2

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

Similar topics

5
11601
by: Johnny Meredith | last post by:
I have seven huge fixed width text file that I need to import to Access. They contain headers, subtotals, etc. that are not needed. There is also some corrupt data that we know about and can correct once the data is in Access (or during the import process itself). Furthermore, the text files are poorly set up, such that some records may be "shifted" over a few characters, and therefore the fixed width nature of the file is corrupted. ...
4
1142
by: ALF | last post by:
What's easiest way to replace a particular text string in a text file by another and save the modified file? Currently I know how to open a file, read the text string, place it in a variable, and modify the string. What I'd like to know is how to save the modified string in the same file. The alternative that's I'm trying to avoid is to create a temporary auxiliary file in the process.
1
1154
by: Matt | last post by:
I want to delete the first line of a text file but it seems like a waste to read in the entire file into memory, remove the line, and then write it out again. Is there a way in VB.NET to delete the first line without reading in the entire file and recreating it?
2
2085
by: ritesh | last post by:
Hi, I'm facing a problem in which I need to edit an already created file, and the editing needs to be done at the start of the file rather then appending to the file. OS - Linux,Solaris For e.g. I have a file test.txt created and I have the path to this file in a
13
4251
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them from the html page (replace the words in the html page with blank space) I'm new to python and could use a little push in the right direction, any ideas on how to implement this? Thanks!
29
4913
by: list | last post by:
Hi folks, I am new to Googlegroups. I asked my questions at other forums, since now. I have an important question: I have to check files if they are binary(.bmp, .avi, .jpg) or text(.txt, .cpp, .h, .php, .html). How to check a file an find out if the file is binary or text? Thanks for your help.
2
2470
by: ray96 | last post by:
i want the list of functions to edit a text file in c++ plz? can i have 2 diffrent pointer to a file at time ? how can i delete the last line of my text file?
10
1392
by: Ikolder | last post by:
Hello, I hope someone can help me with this! I have a problem editing a text file my file looks like this 11 13 15 16 23
1
1474
ganesanji
by: ganesanji | last post by:
hi to all, I am new to php. I have to edit a text file using php. I saw the file system concepts modes. My problem is I want to change a particular text or word in a text file. How to find the index of the specific word. Is there any functions or methods available for change a particular word or finding index of a word... For the example, consider a text file named ganesh.txt which content is shown below.... ganesh is working ...
10
3228
by: bluemountain | last post by:
Hi there, Iam new to python forms and programming too I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2, TEXT3in entire file(file consisting of 150 lines) that is related to a particular id and get the sum of the, WRITES of TEXT1, TEXT2, TEXT3 ex: input file will be as below Windows 2000 text text text text text text text
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.