943,699 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 444
  • C# RSS
Jul 27th, 2009
0

Formatting Problems

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 12
Solved Threads: 0
Newbie Poster
si01 is offline Offline
7 posts
since Jul 2009
Jul 27th, 2009
0

Re: Formatting Problems

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 ')'.
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Jul 27th, 2009
1

Re: Formatting Problems

Here is your solution:
Just replace your try catch block with my code and it will work:

Here is my code
C# Syntax (Toggle Plain Text)
  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 "
C# Syntax (Toggle Plain Text)
  1. string str = "this is a string";
"

This will display as
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 35
Solved Threads: 6
Junior Poster
nccsbim071 is offline Offline
128 posts
since Jul 2009
Jul 27th, 2009
0

Re: Formatting Problems

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]
Reputation Points: 35
Solved Threads: 6
Junior Poster
nccsbim071 is offline Offline
128 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How to Edit a Gridview ??
Next Thread in C# Forum Timeline: C# problem showing a form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC