简体   繁体   中英

C# StreamWriter cannot implicitly convert type to string to SYSTEMS.IO

I need to use StreamWriter to write/store information that the user is entering. I have got this working with the code below.

I now need to make it write a file with the users first name, and store the information there.

Hide Expand Copy Code

case 2: //enter new user
    //Enter new user information and add them to file
    Console.Write("Please Enter the number of users you wish to log: ");

    iUserNumber = Convert.ToInt32(Console.ReadLine());

    //open file for writing, in APPEND mode
    using (StreamWriter sw = new StreamWriter("NewUser" +sName,".txt", true))
    {
        //loop for each user
        for (iCount = 1; iCount <= iUserNumber; iCount++)
        {
            // enter details
            Console.Write("Please Enter your Name" + iCount + ": ");
            sName = Console.ReadLine();

            Console.Write("Please Enter the Name of your School: ");
            sSchoolName = Console.ReadLine();
            Console.WriteLine();

            Console.Write("Please Enter the Name of your Class: ");
            sClassName = Console.ReadLine();

            // write to file
            sw.WriteLine(sName);
            Console.WriteLine();
            sw.WriteLine(sSchoolName);
            Console.WriteLine();
            sw.WriteLine(sClassName);

            Console.WriteLine("User data entered: ");
        }
    }

I can't seem to get this to work and keep coming up with these errors:

Argument 1: cannot convert from 'string' to 'System.IO.Stream' FUNCOCO2

Error CS0165 Use of unassigned local variable 'sName' FUNCOCO2

Error CS1503 Argument 2: cannot convert from 'string' to 'System.Text.Encoding'

I am not sure how to fix this.

I changed it to this:

case 2: //enter new user
    //Enter new user information and add them to file
    Console.Write("Please Enter the number of users you wish to log: ");
    iUserNumber = Convert.ToInt32(Console.ReadLine());
    //enter first name
    Console.WriteLine("Please enter your first Name: ");
    sFirstName = Convert.ToString(Console.ReadLine());

    //open file for writing, in APPEND mode
    using (StreamWriter sw = new StreamWriter(sName))
    {
        //loop for each user
        for (iCount = 1; iCount <= iUserNumber; iCount++)
        {
            // enter details
            Console.Write("Please Enter your Name" + iCount + ": ");
            sLastName = Console.ReadLine();

            Console.Write("Please Enter the Name of your School: ");
            sSchoolName = Console.ReadLine();
            Console.WriteLine();

            Console.Write("Please Enter the Name of your Class: ");
            sClassName = Console.ReadLine();

            // write to file
            sw.WriteLine(sName);
            Console.WriteLine();
            sw.WriteLine(sSchoolName);
            Console.WriteLine();
            sw.WriteLine(sClassName);

            Console.WriteLine("User data entered: ");
        }
    }

And that doesn't seem to work either.

For the first section of code, you are trying to open a streamwriter before you assigned a value to sName. The using keyword just makes sure the object is properly disposed off once the code exits the section, which means you are not leaving files open all over the place in this case. What you should be doing is opening a file within the for loop. The correct code would be:

   case 2: //enter new user
            //Enter new user information and add them to file
            Console.Write("Please Enter the number of users you wish to log: ");
            iUserNumber = Convert.ToInt32(Console.ReadLine());



                //loop for each user
                for (iCount = 1; iCount <= iUserNumber; iCount++)
                {
                    // enter details
                    Console.Write("Please Enter your Name" + iCount + ": ");
                    sName = Console.ReadLine();

                    Console.Write("Please Enter the Name of your School: ");
                    sSchoolName = Console.ReadLine();
                    Console.WriteLine();

                    Console.Write("Please Enter the Name of your Class: ");
                    sClassName = Console.ReadLine();

                    // write to file
                    //open file for writing, in APPEND mode
                    using (StreamWriter sw = new StreamWriter("NewUser" +sName +".txt", true))
                    {
                        sw.WriteLine(sName);
                        Console.WriteLine();
                        sw.WriteLine(sSchoolName);
                        Console.WriteLine();
                        sw.WriteLine(sClassName);
                    }


                    Console.WriteLine("User data entered: ");

                }

            }

Edit: plus, the whole ',' instead of '+' in the opening of the streamwriter. I missed that in favour of the obvious.

using (StreamWriter sw = new StreamWriter("NewUser" +sName,".txt", true))

There seems to be an extra comma right after +sName. That comma should be a + to concatenate the 3 string parts together

Like such using (StreamWriter sw = new StreamWriter("NewUser" + sName + ".txt", true))

Argument 1: cannot convert from 'string' to 'System.IO.Stream' FUNCOCO2

Error CS0165 Use of unassigned local variable 'sName' FUNCOCO2

Error CS1503 Argument 2: cannot convert from 'string' to 'System.Text.Encoding'

Error 1 because ""NewUser" +sName" is not a valid file path, therefore cant be a stream

Error 2 because... dunno since I don't see where you declared sName

Error 3 because... the extra comma is changing the constructor you really want to use, that extra comma turns ".txt" into the desired encoding which is invalid.

Follow the error messages (they point to specific line numbers if you double click on them), read the error, and then correct the mistake.


Error CS0165 Use of unassigned local variable 'sName' FUNCOCO2

You never define and assign sName prior to using it in your stream writer. If you are trying to create 1 file per user then you need to change your logic to reflect that and create a new streamwriter within the loop instead of outside the loop.


Argument 1: cannot convert from 'string' to 'System.IO.Stream'

new StreamWriter("NewUser" +sName,".txt", true))

should be

new StreamWriter("NewUser" +sName + ".txt", true))

which does accept a string as the 1st parameter.


comment Thanks, How would I then get StreamReader to then open that file?

You need to put the loop outside of the creation of the stream writer.

//loop for each user
for (iCount = 1; iCount <= iUserNumber; iCount++)
{
    Console.Write("Please Enter your Name" + iCount + ": ");
    var sName = Console.ReadLine(); // remove the previous instance you had of sName and make it a scoped variable to the for loop
    using (StreamWriter sw = new StreamWriter("NewUser" +sName + ".txt", true))
    { /*use the streamwriter here */ }
}

You can do something like this:

Console.WriteLine("Please enter your first Name: ");
            String sFirstName = Console.ReadLine();
            String information = String.Empty;
            String append = String.Empty;

            while (true)
            {

                Console.Write("Please Enter your Name: ");
                information = information + Console.ReadLine() + " ";

                Console.Write("Please Enter the Name of your School: ");
                information = information + Console.ReadLine() + " ";

                if (information.Trim().Equals(""))
                {
                    break;
                }

                Console.Write("Please Enter the Name of your Class: ");
                information = information + Console.ReadLine() + "\t";

                append = append + information;
                information = String.Empty;


            }

            String fileLocation = "C:...\\New Text Document.txt";


            using (StreamWriter sw = new StreamWriter(fileLocation))
            {
                // write to file
                sw.WriteLine(append);
                Console.WriteLine("User data entered: ");

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM