Formatting Problems

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 7
Reputation: si01 is an unknown quantity at this point 
Solved Threads: 0
si01 si01 is offline Offline
Newbie Poster

Formatting Problems

 
0
  #1
Jul 27th, 2009
Hey I'm really new to C# - I don't understand how to properly sort out the code based on the messages that I am getting.

The messages are:

} expected
{ expected
) expected
Invalid expression term '}'

When I simply enter these characters into the place that the debugger tells me to, more errors pop up - I am using Visual Studio 2008. This is the section of code I believe I am having difficulty with:
  1. {
  2. try
  3. {
  4. using(FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)
  5. {
  6. using (StreamReader sr = new StreamReader("TestFile.txt"))
  7. {
  8. String line;
  9.  
  10. while ((line = sr.ReadLine()) != null)
  11.  
  12. {
  13. Console.WriteLine(line);
  14.  
  15. using (StreamWriter sw = new StreamWriter("TestFile.txt"))
  16. {
  17.  
  18. sw.Write("\n");
  19. sw.WriteLine("line\n");
  20. sw.WriteLine("test");
  21.  
  22. }
  23. }
  24. }
  25. }
  26.  
  27. catch (Exception e)
  28. {
  29. // Let the user know what went wrong.
  30. Console.WriteLine("The file could not be read:");
  31. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
  32. Console.WriteLine("File: " + e.Name);
  33. Console.WriteLine("Folder:" + Path.GetDirectoryName(e.FullPath));
  34. Console.WriteLine(d.Message);
  35. continue;
  36. }
  37. }
  38. }
  39. }
THANKS FOR ANY HELP!
Last edited by Ancient Dragon; Jul 27th, 2009 at 9:17 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 59
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: Formatting Problems

 
0
  #2
Jul 27th, 2009
Hi,
Please use [code=language] for all the code you post here.
regarding solution for your problem, just put '}' for every '{' and same for '(' and ')'.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 94
Reputation: nccsbim071 is an unknown quantity at this point 
Solved Threads: 6
nccsbim071's Avatar
nccsbim071 nccsbim071 is offline Offline
Junior Poster in Training

Re: Formatting Problems

 
1
  #3
Jul 27th, 2009
Here is your solution:
Just replace your try catch block with my code and it will work:

Here is my code
  1. try
  2. {
  3. using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
  4. {
  5. using (StreamReader sr = new StreamReader("TestFile.txt"))
  6. {
  7. String line;
  8.  
  9. while ((line = sr.ReadLine()) != null)
  10. {
  11. Console.WriteLine(line);
  12.  
  13. using (StreamWriter sw = new StreamWriter("TestFile.txt"))
  14. {
  15.  
  16. sw.Write("\n");
  17. sw.WriteLine("line\n");
  18. sw.WriteLine("test");
  19.  
  20. }
  21. }
  22. }
  23. }
  24. }
  25. catch (Exception e)
  26. {
  27. // Let the user know what went wrong.
  28. Console.WriteLine("The file could not be read:");
  29. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
  30. Console.WriteLine("File: " + e.Name);
  31. Console.WriteLine("Folder:" + Path.GetDirectoryName(e.FullPath));
  32. Console.WriteLine(d.Message);
  33. continue;
  34. }

And I have few suggestions for you:
1. While writing code every time you open a bracket immediately place it's corresponding closing bracket and then go back inside the bracket and then type. This rule applies not only to brackets but for all the codes which has corresponding opening and closing code. For example doublequotes(") and single quotes(') and all of them.

2. When you post your code on Daniweb always enclose the code in code tags like this "
  1. string str = "this is a string";
"

This will display as
  1. string str = "this is a string";

Well goodluck
If you have any problem send messages directly at
info@bizpersonnel.com


www.bizpersonnel.com
A Software development Company
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 94
Reputation: nccsbim071 is an unknown quantity at this point 
Solved Threads: 6
nccsbim071's Avatar
nccsbim071 nccsbim071 is offline Offline
Junior Poster in Training

Re: Formatting Problems

 
0
  #4
Jul 27th, 2009
Sorry about enclosing tags in code blocks, i was not able to explain it properly:
Here follow this to enclose your code in the code blocks:

[code=C#]
String str = "This is a string.";
[/code]
My Personnel Blog:dotnetworker.blogspot.com
My Company:http://www.bizpersonnel.com
Blog:http://bizpersonnel.wordpress.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC