C# creating,copying txt filesHELP ME PLEASE!
15 Replies - 2871 Views - Last Post: 18 October 2010 - 09:33 AM
#1 Guest_sisqo*
C# creating,copying txt files
Posted 18 October 2010 - 03:19 AM
so the questin is :
You need to create a new Folder and a new Text File Finish.txt, and then you have to copy all the containing text from the 3 text files
("I Need YOUR HELP") seperating the text with a line in the new created text file Finish.txt and it should look like this:
I
------------
Need
------------
YOUR
------------
HELP
Can somebody help me with this problem.
sisqo
Replies To: C# creating,copying txt files
#2
Re: C# creating,copying txt files
Posted 18 October 2010 - 03:45 AM
- Reading a text file
- Writing a text file
- Directory creation
The flow of your application appears to be:
- Create directory
- Read Example1.txt file data
- Store read data to local string
- Read Example2.txt file data
- Store to local string
- Read Example3.txt data
- Store to local string
- Write local string to Finish.txt
Creation of a directory is simple provided you use System.IO:
http://msdn.microsof...ory(VS.71).aspx
Reading a text file:
http://msdn.microsof...reamreader.aspx
Writing a text file:
http://msdn.microsof...reamwriter.aspx
You'll want to create one reader and one writer method and pass in the file name:
public class Program
{
public static string GetTextData(string path)
{
// Open path file and read contents. Return contents.
}
public static void WriteTextData(string path, string data)
{
// Open path file for write and write contents of data.
}
}
With all that, you shouldn't have much issue here.
Good luck.
#3 Guest_sisqo*
Re: C# creating,copying txt files
Posted 18 October 2010 - 04:29 AM
but i'm havin problems again it would really help me if you would write the whole code for this problem and i'll compare with my code and learn where the problem is thank you mentalfloss
#4
Re: C# creating,copying txt files
Posted 18 October 2010 - 04:38 AM
sisqo, on 18 October 2010 - 03:29 AM, said:
but i'm havin problems again it would really help me if you would write the whole code for this problem and i'll compare with my code and learn where the problem is thank you mentalfloss
Hello there,
I will gladly help you, but I cannot really just give you all the code straight up for a problem like this. It is against the policy of the site. Show me what you have, and I will try and guide you with pleasure. Make sure you let us know specifically what problems you are having too
This post has been edited by CodingSup3rnatur@l-360: 18 October 2010 - 04:39 AM
#5 Guest_sisqo*
Re: C# creating,copying txt files
Posted 18 October 2010 - 04:47 AM
using System;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Directory.CreateDirectory("c:\\ps2");
string road = "c:\\ps2\\finish.txt";
StreamWriter sw = new StreamWriter(road);
string path = "c:\\ps\\example1.txt";
TextReader read = new StreamReader(path);
read.Close();
TextWriter write = new StreamWriter(path);
write.Close();
}
}
}
MOD EDIT: When posting code...USE CODE TAGS!!!
This post has been edited by JackOfAllTrades: 18 October 2010 - 05:49 AM
#6
Re: C# creating,copying txt files
Posted 18 October 2010 - 05:02 AM
class Program
{
static string filePath1 = "Example1.txt"; //you don't necessarily need variables to hold the paths, but it just makes it clearer for you
static string filePath2 = "Example2.txt";
static string filePath3 = "Example3.txt";
static string newFilePath = "Finish.txt";
static void Main(string[] args)
{
}
private static string readFile(string filePath)
{ //everytime you want to read a file, you pass which file you want to read
//you need to use StreamReader as you have done and pass it the filepath.
//notice the method returns a string so that you can return what you read
//as your scenario is quite simple, you could get away with using the ReadToEnd(); method
//of the stream reader class as this will read everything in the file. Once you have read it,
//return it using the return 'keyword'. So just call the StreamReader ReadToEnd(); method
//to read the file and then return it.
}
private static void writeFile(params string[] text)
{
//we will deal with writing in a minute
}
}
}
Also, use using blocks when creating stream objects. This just makes things easier as you do not have to explicitly call the Close(), method. See below:
Instead of:
TextWriter streamWriter = new StreamWriter("<filePath>");
...
streamWriter.Close();
Use:
using(TextWriter streamWriter = new StreamWriter("<filePath>"){
//put all code the uses the stream object in here
}
See if you can create the read method to start with, and then post back with what you create.
This post has been edited by CodingSup3rnatur@l-360: 18 October 2010 - 07:42 AM
#7 Guest_sisqo*
Re: C# creating,copying txt files
Posted 18 October 2010 - 05:32 AM
using System;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static string filePath1 = "Example1.txt";
static string filePath2 = "Example2.txt";
static string filePath3 = "Example3.txt";
static string newFilePath = "Finish.txt";
static void Main(string[] args)
{
}
private static string readFile(string filePath)
{
string path = "c:\\ps\\Example1.txt";
TextReader read = new StreamReader(path);
return read;
{
}
}
private static void writeFile(params string[] text)
{
using(StreamWriter streamWriter = new StreamWriter("c:\\ps2\\Finish.txt"))
{
}
}
}
}
it's missing somthing !
MOD EDIT: When posting code...USE CODE TAGS!!!
This post has been edited by JackOfAllTrades: 18 October 2010 - 05:50 AM
#8
Re: C# creating,copying txt files
Posted 18 October 2010 - 05:54 AM
sisqo, on 18 October 2010 - 04:32 AM, said:
using System;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static string filePath1 = "Example1.txt";
static string filePath2 = "Example2.txt";
static string filePath3 = "Example3.txt";
static string newFilePath = "Finish.txt";
static void Main(string[] args)
{
}
private static string readFile(string filePath)
{
string path = "c:\\ps\\Example1.txt";
TextReader read = new StreamReader(path);
return read;
{
}
}
private static void writeFile(params string[] text)
{
using(StreamWriter streamWriter = new StreamWriter("c:\\ps2\\Finish.txt"))
{
}
}
}
}
it's missing somthing !
MOD EDIT: When posting code...USE CODE TAGS!!!
Ok Thanks
#9
Re: C# creating,copying txt files
Posted 18 October 2010 - 05:56 AM
Also, what I was going to say next is that you should probably ensure that the file exists before you try and read it. You can do this with the File.Exists() method. It returns true if the File exists, false if it doesn't. Below is a functioning version of readFile().
class Program
{
static string filePath1 = @"c:\ps\Example1.txt";
static string filePath2 = @"c:\ps\Example2.txt"; //you need to chenage all these to your desired file path as shown
static string filePath3 = @"c:\ps\Example3.txt";//notice the use of the '@' symbol. It basically says 'treat all character
static string newFilePath = @"c:\ps\Finish.txt";//in this string as literal characters' meaning you do not have to use escape
//characters in the string
static void Main(string[] args)
{
}
private static string readFile(string filePath)
{
if (File.Exists(filePath)) //check if the file exists
{
using (TextReader reader = new StreamReader(filePath)) //if it does exist, instantiate a TextReader object in the using statement
//so you don't have to manually Close() the Reader when you are done with it
{
return reader.ReadToEnd(); //read everything in the file and return it
}
}
else return "Couldn't find required file"; //if the file doesn't exist, return an error message (you can add something better to handle the error
// here if you wish)
}
Baically, you check see if the file exists, if it does, set up a stream and read everything in that file, and return what has been read as a string. If it doesn't exists, return an error message.
Now, for the writeFile() method, things get slightly more complicated.
I have used the 'params' keyword in the parameter list. I have done this so you can pass as many strings as you like to the method, and it will write them ALL to the file correctly.
The text parameter is therefore an array and, as such, you need to loop through and write each element to a file (you pass elements into the array when you call the method by passing the method the text you want to write to the file (i.e. the strings returned be each call to readFile()).
You could use a foreach loop to loop through the array, hhowever, in this case, a standard for loop is better as it means you can track where you are in the array at any given time.
So, your method will look something like this:
private static void writeFile(params string[] text)
{
using (TextWriter writer = new StreamWriter(newFilePath))
{
for (int i = 0; i < text.Length; i++)
{
}
}
}
In the code above, you have set up a stream so that you can write to the file, and you have a loop that loops through each element in the params array (i.e. each piece of text you want writing to the file). In the loop, you need to use the WriteLine method of the Stream Reader class (writer.WriteLine(<place the current array element here>)) to write each element to file. We use WriteLine() instead of Write() becuase we want a new line to be entered after every element. However, if you want the "------------" spearating the elements, you will have to do something more after you have written the element to the file.
Firstly, you need to check whether or not you are on the last element; if you are on the last piece of text, you do not want to place the "--------------" after it as there is no more text after it. So, you need to check (with an if statement), if you are on the last element. If you are, break out of the loop (the 'break' keyword?). If the code gets past this statement (i.e. we are not on the last piece of text), you need to write a "--------------------" string to the file using the write.WriteLine() method again.
Have a go at writing writeFile(). Just think through it logically; step by step. You just need to add appropriate logic to the loop. You should be able to do everything you need in 3 lines I think
This post has been edited by CodingSup3rnatur@l-360: 18 October 2010 - 06:04 AM
#10
Re: C# creating,copying txt files
Posted 18 October 2010 - 05:58 AM
using System;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static string filePath1 = "Example1.txt";
static string filePath2 = "Example2.txt";
static string filePath3 = "Example3.txt";
static string newFilePath = "Finish.txt";
static void Main(string[] args)
{
}
private static string readFile(string filePath)
{
string path = "c:\\ps\\example1.txt";
TextReader read = new StreamReader(path);
read.ReadToEnd();
return read;
{
}
}
private static void writeFile(params string[] text)
{
using(StreamWriter streamWriter = new StreamWriter("c:\\ps2\\finish.txt"))
{
//put all code the uses the stream object in here
}
#11
Re: C# creating,copying txt files
Posted 18 October 2010 - 06:30 AM
StreamWriter streamWriter = new StreamWriter("c:\\ps2\\finish.txt"
You use:
StreamWriter streamWriter = new StreamWriter(newFilePath) //but you MUST change the variables at the top of the file
Also, you are still returning the TextReader object in the readFile method rather than the string from the ReadToEnd() method.
Change the variables up top, actually use them in the program, add a using block in the readFile method, return the string from the ReadToEnd method instead of the TextReader object in the readFile method and try and create the for loop in the writeFile() method and add the check to ensure the file exists to the readFile method
The loop really isn't very complicated if you just think about it.
I am trying to guide you and help you, but you need listen to what I say and make a reasonable effort yourself. If there is something SPECIFIC you don't quite understand, then ask by all means.
Once you have made an reasonable attempt at the loop and implemented what I talked about in previous posts, I shall help you further
This post has been edited by CodingSup3rnatur@l-360: 18 October 2010 - 09:44 AM
#12
Re: C# creating,copying txt files
Posted 18 October 2010 - 06:36 AM
using System;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static string filePath1 = @"c:\ps\Example1.txt";
static string filePath2 = @"c:\ps\Example2.txt";
static string filePath3 = @"c:\ps\Example3.txt";
static string newFilePath = @"c:\ps\Finish.txt";
static void Main(string[] args)
{
}
private static string readFile(string filePath)
{
if (File.Exists(filePath1))
{
return reader.ReadToEnd();
}
}
else return "Couldn't find required file";
//string path = "c:\\ps\\example1.txt";
//TextReader read = new StreamReader(path);
//read.ReadToEnd();
//return read;
{
}
}
private static void writeFile(params string[] text)
{
using(StreamWriter streamWriter = new StreamWriter("c:\\ps2\\finish.txt"))
{
for (int i = 0; i < text.Length; i++)
}
}
#13
Re: C# creating,copying txt files
Posted 18 October 2010 - 06:46 AM
Also, you seem to have lost the StreamReader object in readFile(). Include it in a using statement. I have actually already given you the readFile method in a previous post.
Change this line in the writeFile method:
using(StreamWriter streamWriter = new StreamWriter("c:\\ps2\\finish.txt"))
to...
using(StreamWriter streamWriter = new StreamWriter(newFilePath)//use the variables at the top.
//They will help you separate everything in you mind even though
//they are probably unecessary for such a simple application
//(you might want to think up better, descriptive variable names at some point too,
//if you choose to keep those variables that is...) You can remove them
//once you understand what is going on fully.
Once you have the readFile method correct, and have changed the line above in the writeFile method, read what I put about the for loop. Go through it step by step and try to write each line step by step...please
This post has been edited by CodingSup3rnatur@l-360: 18 October 2010 - 08:27 AM
#14
Re: C# creating,copying txt files
Posted 18 October 2010 - 07:36 AM
Thanks!
#15
Re: C# creating,copying txt files
Posted 18 October 2010 - 07:41 AM
sisqo, I have put the code in the writeFile method in spoiler tags below. Once you have the writeFile and readFile methods set up properly, all you need to do is call the methods in the Main method of the application. I would do this by calling the writeFile method, then passing in the return values of the readFile method calls. For example,
writeFile(readFile(fileName1),readFile(fileName2),readFile(fileName)); //write to file all strings read from three other files
You seemed to be struggling a bit. Have a look through your completed code and make sure you understand every single part of it.
I think you would benefit from following a few basic tutorials also. There are some great starter tutorials on this site. Perhaps pick up a good c# book as well.
Note: You probably should add some exception handling try-catch blocks when trying to read and write from/to files, just to guard against any problems that could potentially occur. I'll leave that with you though
This post has been edited by CodingSup3rnatur@l-360: 18 October 2010 - 08:31 AM
|
|

New Topic/Question
Reply
MultiQuote










|