//Hello there, these commented lines are not executing. help me out here. when i run 1st time, a file is created. when i run it second time, it dsnt get //overwritten. plz help me out in this....
namespace File_Experiment
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw;
StreamReader sr;
FileStream fs;
char c;
string pth;
pth = Directory.GetCurrentDirectory();
Console.WriteLine(pth);
pth = pth + "\\" + "textfile.txt";
if ((File.Exists(pth)))
{
Console.WriteLine("The file already exists. Append? y or n");
c = Convert.ToChar( Console.Read());
switch (c)
{
case 'y':
FileInfo fs2;
StreamWriter sw2;
fs2 = new FileInfo(pth);
sw2 = new StreamWriter(pth);
Console.WriteLine("Enter the text to be written in the file");
string line = "hello chaman";
line = Console.ReadLine(); //Why isnt this statement executing??
sw2.WriteLine(line); //Why isnt this statement executing??
Console.WriteLine("The text has been written");
break;
case 'n': break;
default: Console.WriteLine("No identifiable key inserted. The program will now exit");
break;
}
}
else
{
fs = new FileStream(pth, FileMode.Create);
sw = new StreamWriter(fs);
Console.WriteLine("Enter the text to be written in the file");
string line = Console.ReadLine();
sw.WriteLine(line);
Console.WriteLine("The text has been written");
sw.Close(); fs.Close();
}
Console.WriteLine("Thanks for using my program :)");
}
}
}
Loading
Kirtan PatelPosted Oct 19, 2009, 10:38 AM
Here is Corrected Code
dont forget to mark "Do you like this Answer" please :)
problem was you were not reading character correctly and major thing was that you have not use curly braces { } in switch case :)
thank you :) if still problem arise let me know :)
static void Main(string[] args)
{
StreamWriter sw;
StreamReader sr;
FileStream fs;
char c;
string pth;
pth = Directory.GetCurrentDirectory();
Console.WriteLine(pth);
pth = pth + "\\" + "textfile.txt";
if ((File.Exists(pth)))
{
Console.WriteLine("The file already exists. Append? y or n");
// Get the char Value From Console
ConsoleKeyInfo k = Console.ReadKey();
c = k.KeyChar;
switch (c)
{
case 'y':
{
FileInfo fs2;
StreamWriter sw2;
fs2 = new FileInfo(pth);
//Appendby passing second parameter true
sw2 = new StreamWriter(pth,true);
Console.WriteLine("Enter the text to be written in the file");
string line = "hello chaman";
line = Console.ReadLine();
sw2.WriteLine(line);
sw2.Close();
Console.WriteLine("The text has been written");
break;
}
case 'n':
{
break;
}
default:
{
Console.WriteLine("No identifiable key inserted. The program will now exit");
break;
}
}
}
else
{
fs = new FileStream(pth, FileMode.Create);
sw = new StreamWriter(fs);
Console.WriteLine("Enter the text to be written in the file");
string line = Console.ReadLine();
sw.WriteLine(line);
Console.WriteLine("The text has been written");
sw.Close();
fs.Close();
}
Console.WriteLine("Thanks for using my program :)");
}
}
Vivek DattaPosted Oct 19, 2009, 3:25 PM
Kirtan PatelPosted Oct 19, 2009, 3:15 PM
so that carriage return \r is used by Console.ReadLine() so it will not ask you for input as it thinks Enter Key( \r)
pressed and takes "" as input :) so it skiped that line without asking input to you.
normally Console.Read() is used to Parsing File Character By Character
so its better to use ReadLine() then Just Read();
Now When you Used ReadKey() info it directly Returned the Key that was pressed so it will not leave carriage return
and Code Worked Fine in that case :)
Vivek DattaPosted Oct 19, 2009, 2:44 PM
1) //this was mine in which program wasnt overwriting or appending, it wasnt an error, but the program was not just executing the lines even after compiling them (I used the "step in" in vs2008 to check it, the codelines were compiled)
Console.WriteLine("The file already exists. Append(or overwrite for that matter)? y or n");
c = Convert.ToChar( Console.Read());
now in second run, wen a file is alrdy created, i press y, it compiles these two lines but dsnt executes them
sw2 = new StreamWriter(pth);
Console.WriteLine("Enter the text to be written in the file");
line = Console.ReadLine(); //Why isnt this statement executing??
sw2.WriteLine(line); //Why isnt this statement executing??
Console.WriteLine("The text has been written");
sw2.Close();
2) But when i replace
c = Convert.ToChar( Console.Read());
with your code
ConsoleKeyInfo k = Console.ReadKey();
c = k.KeyChar;
the program starts working fine. can u please tell me wat was happening and wats d difference btwn these two lines?? i mean both are doing the same thing na?
Kirtan PatelPosted Oct 19, 2009, 1:11 PM
1st problem was you have not used curly braces { } in switch case code(cause of that code skipping ) .
2nd problem was you have not closed the streamWriter (to successfully write into file we have to close the stream writer)
3rd the cause of not appending file was you have passed only one parameter to the streamWriter that is file path for
specifying FileIn Append Mode we need to specify path and second perimeter true if we want to append the file and
false if we dont want to append the file and want to ovver right the file :) like below code
StreamWriter sw = new StreamWriter(path,true)
here true specifies that you are opening file in append mode
thats it friend Hope you understand :)
Thank you :)
if you have any question let me know i will help you :)
finally HAPPY NEW YEAR :)
Vivek DattaPosted Oct 19, 2009, 1:02 PM
can u plz further clarify wat was the problem? i mean i used both ur and mine method for getting a char in a seprate program and both printed out same. then where was the problem???