• Unity
  • Services
  • Made with Unity
  • Learn
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by VSuper · Jul 01, 2014 at 10:32 AM · linetxt

Write a specific line in a txt file (C#)

Hello, I want to write a specific line in a text file but I don't know how to do?

I've tried this:

 string[] lines = System.IO.File.ReadAllLines("C:/" + GameTitle + "/PlayerSets.txt");
                 string li = lines [0];
                 using (StreamWriter sw = File.AppendText ("C:/" + GameTitle + "/PlayerSets.txt")) {
                     if (lines [0] != null) sw.WriteLine ("lol");
                 }

And sure, it doesn't work because Unity write add a new line, and I want it to change, not add, the line 0.

Cordialy.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by HarshadK · Jul 01, 2014 at 10:39 AM

After a surface search on Google:

  • Writing a single line on a txt document - C#

  • How to write a file?

  • How do I write a line of text to a file?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VSuper · Jul 01, 2014 at 10:51 AM 0
Share

I've already seen these links... they don't work that's why I've post here. The 3rd link is helpful but doesn't work to, the AppendAllText just add text, it doesn't CHANGE it.

avatar image HarshadK · Jul 01, 2014 at 01:18 PM 0
Share

If I understand your problem correctly then what you want is this:

 void writeFile() {
 string[] lines = System.IO.File.ReadAllLines("F:/PlayerSets.txt");
 string allString = "";
     if (lines [0] != null) {
         lines [0] = "lol";
     }
     for(int i=0; i<lines.Length; i++) {
         allString += lines[i]+"\n";
     }                    System.IO.File.WriteAllText("F:/PlayerSets.txt", allString);
 }

The file reads the content from the file and replaces the first line with 'lol' and writes the remaining content with first line replaced with 'lol' to the file again.

avatar image VSuper · Jul 01, 2014 at 04:45 PM 0
Share

It doesn't, it just add at the end of the 2nd line (there 2 line) "lol"... it doesn't replace anything, again.

avatar image HarshadK · Jul 01, 2014 at 04:57 PM 0
Share

I've tested the code I provided and it works for me. Can you provide the code as to how you are using it?

avatar image VSuper · Jul 01, 2014 at 07:37 PM 0
Share

EDIT: it's working fine, i was using the default windows viewer, but with Notepad C++ it's ok. Thanks :)

avatar image
0

Answer by drudiverse · Mar 19, 2016 at 12:11 PM

WORKING CODE:

to edit single lines using system io.

1 down vote the easiest way is :

static void lineChanger(string newText, string fileName . int line_to_edit) {

         string[] arrLine = File.ReadAllLines(fileName);
         arrLine[line_to_edit - 1] = newText;
         File.WriteAllLines(fileName, arrLine);

 }

usage :

lineChanger("new content for this line" , "sample.text" , 34);

JS

function lineChanger( newText: String, fileName :String, line_to_edit: int) {

         var arrLine : String[] = File.ReadAllLines(fileName);
         arrLine[line_to_edit - 1] = newText;
         File.WriteAllLines(fileName, arrLine);

 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving data in to files (Android) 1 Answer

Cannot read or write a txt file after build 2 Answers

Writing a single line on a txt document - C# 1 Answer

I'm using Java Script with import System.IO. 1 Answer

Loading TextAsset under different scenes 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges