Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] a little direction

csharp

  • This topic is locked This topic is locked
5 replies to this topic

#1 Jeff.

Jeff.

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 15 September 2012 - 11:49 AM

Hello all.
Well is what im wanting to do is attempt to write a program in C# that take a .txt doc and add your desired string or character at the beginning or end or both. I have no idea where to even start so I was hoping someone could shed some light on this for me.

Also while im at it, is it possible to take 2 separate .txt docs..lets say one is filled with the names of documents and the other is filled with links to the document, can you join them together, while placing lets say html or css(doesn't matter just string in general) code/tags at the beggining of the line, in between the title's and links of the documents and at the end also?

thanks in advance for any inpu
  • 0

#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 15 September 2012 - 11:08 PM

Yes jeff, working with text files in c# is easy,
you need to start by including this :
using System.IO;
in order to use StreamReader and StreamWritter,

with both those you can read a text file line by line, or the whole file in one read (provided it is not a huge file)

my aproacho would be to use something like this :

String[] email = ReadToEnd().Split( Environment.NewLine );

take it as pseudocode ;), but doing that way you read the whole text at once and at the same time you make an array of it where each row will be a line of the file.

if you do the same with both files you will have two arrays, so, if rows are one on one you can just iterate and write your final file.
or search for values if that is how you need to locate the links

give it a try and come back with more questions ;)


  • 1

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 15 September 2012 - 11:37 PM

BlackRabbit said all for you. However, another way to read the whole context of a text file is using System.IO.File.ReadAllText method which opens a text file, reads all lines of the file, and then closes the file. As example,
String fileContent = System.IO.File.ReadAllText("textfilename.txt")
, the above code will read all the content of file textfilename.txt.

To write text to a file you can use System.IO.File.WriteAllText method which creates a new file, writes the specified string to the file, and then closes the file (If the target file already exists, it is overwritten).

So, to merge the two file's content and write the content to a new file, you can code like this --
String file1Content = System.IO.File.ReadAllText("textfile1name.txt");
String file2Content = System.IO.File.ReadAllText("textfile2name.txt");
System.IO.File.WriteAllText("newfilename.txt", file1Content + file2Content);
. The above code reads all text from two files (textfile1name.txt and textfile2name.txt) and merge them (textfile2name.txt file's content following textfile1name.txt content) together into a new file name newfilename.txt.

Note that, there are other variants as well of the ReadAllText and WriteAllText methodd (like providing an encoding as argument, Read/Write AllLines, AllBytes and others) which you can use depending on your needs.

In case of long/large file, you can perform those operations in thread.
  • 1

#4 Jeff.

Jeff.

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 16 September 2012 - 10:03 AM

I appreciate both of you guys, thank you
  • 0

#5 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 16 September 2012 - 03:57 PM

You are welcome! :)
  • 0

#6 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 16 September 2012 - 06:35 PM

This topic has been marked as SOLVED. If you have a similar question or topic, you can go back to the subforum and start a new topic to continue discussions.
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download