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:

{
        try
        {   
            using(FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)
            {
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                {
                    String line;
                
                    while ((line = sr.ReadLine()) != null) 
                
                    {
                        Console.WriteLine(line);

                        using (StreamWriter sw = new StreamWriter("TestFile.txt")) 
                        {
            
                            sw.Write("\n");
                            sw.WriteLine("line\n");
                            sw.WriteLine("test");

                        }
                    }
                }
            }
       
        catch (Exception e) 
        {
             // Let the user know what went wrong.
                        Console.WriteLine("The file could not be read:");
                        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
                        Console.WriteLine("File: " + e.Name);
                        Console.WriteLine("Folder:" + Path.GetDirectoryName(e.FullPath));
                        Console.WriteLine(d.Message);
                        continue;
                }
            }
        }
        }

THANKS FOR ANY HELP!

Recommended Answers

All 3 Replies

Hi,
Please use for all the code you post here. regarding solution for your problem, just put '}' for every '{' and same for '(' and ')'.[code=language] for all the code you post here.
regarding solution for your problem, just put '}' for every '{' and same for '(' and ')'.

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

Here is my code

try
        {
            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader("TestFile.txt"))
                {
                    String line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);

                        using (StreamWriter sw = new StreamWriter("TestFile.txt"))
                        {

                            sw.Write("\n");
                            sw.WriteLine("line\n");
                            sw.WriteLine("test");

                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
            Console.WriteLine("File: " + e.Name);
            Console.WriteLine("Folder:" + Path.GetDirectoryName(e.FullPath));
            Console.WriteLine(d.Message);
            continue;
        }

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 "

string str = "this is a string";

"

This will display as

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

commented: Good explanation. +7

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]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.