Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

editing with text file c#

hi,

I want to read text file (which has some value written in one line for example "55")  in c#

now after reading, I want to check the value if its equal to 55 I want to clear this value and store new value (for example "66") and save the file with new value (where 55 is replaces with 66)

0
Ameerh24
Asked:
Ameerh24
2 Solutions
 
zadeveloperCommented:

string fileValue;
            using (var sr = new StreamReader("c:\\text.txt"))
            {
                fileValue = sr.ReadLine();
                sr.Close();
            }

            if (fileValue.Equals("55"))
                fileValue = "66";

            using (var sw = new StreamWriter("c:\\text.txt", false))
            {
                sw.Write(fileValue);
                sw.Close();
            }

Open in new window

0
 
gsx1022Commented:
Hi,

you will have to use the StreamReader and StreamWriter classes. Check them out on MSDN.

gsx1022
0
 
tigin44Commented:
something like this may help you...
public static void ReadTextFile()
        {
            string sFrom = "", sTo = "";
            string sBuffer = "";
            try
            {
                Console.WriteLine("Enter name of teh file with full path");
                sFrom = Console.ReadLine();
                Console.WriteLine("Enter the name of the file for the output");
                sTo = Console.ReadLine();

                StreamReader reader = new StreamReader(sFrom);
                StreamWriter writer = new StreamWriter(sTo);

                sBuffer = reader.ReadLine();

                if (int.Parse(sBuffer) == 55)
                {
                    writer.Write(66);
                
                }
            }
            catch (FileNotFoundException fnf)
            {
                Console.WriteLine("File not found, Try again. {0}", fnf);
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occured. {0}", e);
            }
        }

Open in new window

0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
magicdlfCommented:
Just want to mention that if you happened to use VS2005, change this line:
using (var sw = new StreamWriter("c:\\text.txt", false))
to this:
using (StreamWriter sw = new StreamWriter("c:\\text.txt", false))

You can also get the int value by this:
int value = int.TryParse(sr.ReadLine());

I think zadeveloper did a good job helping you. Happy Coding!
0
 
käµfm³d 👽Commented:
All of these solutions assume that your text file only contains one line of text. Is that the case, or does your file have multiple lines of text?
0
 
SameerJagdaleCommented:
read all the text from your file with File.ReadAllText method, loop into all lines of file and just call string.replace("55","66").
HTH
0
 
zadeveloperCommented:
Or , using the StreamReader you can call ReadToEnd()
0
 
käµfm³d 👽Commented:
And of course, that would depend on how big your file is. I imagine you'd have a tough time processing a 4 GB text file if you called ReadToEnd().

;)
0
 
zadeveloperCommented:
This is true, but from the description of the problem give it would seem like a 1 line file. I would not use any suggestion on this page to open a 4gb text file. :)
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now