2016-12-01 95 views
0

我需要使用StreamWriter寫入/存儲用戶正在輸入的信息。我已經得到了以下代碼的工作。C#StreamWriter不能隱式地將類型轉換爲字符串到SYSTEMS.IO

我現在需要讓它用用戶的名字寫一個文件,並將信息存儲在那裏。

隱藏展開復制代碼

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: "); 
     } 
    } 

我似乎無法得到這個工作,並保持未來與這些錯誤:

參數1:無法從「字符串」轉換爲 'System.IO.Stream' FUNCOCO2

未分配的局部變量 'SNAME' FUNCOCO2的錯誤CS0165使用

錯誤CS1503參數2:無法從「字符串」轉換爲 「System.Text.Encoding」

我不知道如何解決這個問題。

我改成了這樣:

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: "); 
     } 
    } 

這似乎沒有任何工作。

+1

'這似乎並沒有工作either.' <=請還包括錯誤消息就像你與你的第一次嘗試做。 – Igor

+0

聽起來好像你想爲每個用戶創建一個新文件,但是你只打開一次文件 - 在循環之外。在你原來的「嘗試」中,你也錯誤地在'new StreamWriter(「NewUser」+ sName,「。txt」,true)'中使用了逗號而不是字符串連接。由於構造函數沒有這樣的重載,所以會出現一些令人困惑的錯誤。 – Luaan

回答

0

請按照錯誤消息(如果您雙擊它們指向特定的行號),請閱讀錯誤,然後糾正錯誤。未分配的局部變量 'SNAME' FUNCOCO2


錯誤CS0165使用你從來沒有定義和分配在流作家用它sName之前。如果您試圖爲每個用戶創建一個文件,那麼您需要更改邏輯以反映該內容,並在循環內而不是在循環外部創建新的Streamwriter。


參數1:無法從 '串' 到 'System.IO.Stream' 轉換

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

應該

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

它不接受一個字符串作爲第一個參數。


評論謝謝你,我怎麼會那麼得到的StreamReader來,然後打開該文件?

您需要將循環創建爲流寫入器之外。

//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 */ } 
} 
+0

然後'sName'也必須填在_before_這一行上 –

+0

Thanks,How然後我會得到StreamReader然後打開該文件? – Lorna

+0

@Lorna - 請參閱更新 – Igor

0

對於代碼的第一部分,您正嘗試在爲sName分配一個值之前打開一個Streamwriter。 using關鍵字只是確保在代碼退出該部分時正確處理對象,這意味着在這種情況下您不會將文件全部打開。你應該做的是在for循環中打開一個文件。正確的代碼是:

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: "); 

       } 

      } 

編輯:plus,整個','而不是'+'在streamwriter的開頭。我錯過了那個顯而易見的贊成。

+0

謝謝,我移動它,現在我收到以下錯誤:參數3:不能從'字符串'轉換爲'int',不能將字符串轉換爲系統文本編碼,並且不能將字符串轉換爲Systems.IO.Stream – Lorna

+0

檢查什麼類型iUserNumber是。另外,請檢查iCount的類型。你沒有在你提供的代碼片段中聲明iCount。大多數情況下,當你創建一個for循環時,你在那裏聲明一個int,比如「for(int iCount = 1; iCount <= IUserNumber; iCount ++) –

0

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

似乎有+ SNAME之後額外的逗號。這逗號應該是一個+以連接3串部分一起

像這樣 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' 

錯誤1,因爲「」 NEWUSER「+ SNAME」不是一個有效的文件路徑,因此不能被流

錯誤2,因爲......不知道,因爲我沒有看到你在哪裏聲明SNAME

錯誤3因爲...額外的逗號正在改變你真的想使用構造函數,那多餘的逗號轉彎」。文本」 轉換爲無效的所需編碼。

0

你可以做這樣的事情:

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: "); 

      } 
+0

您必須按兩次Enter才能退出 –

相關問題