问题
I have a problem reading a textfile where I skip the first line and afterwards place it into a char array.
The operation is very easy if I want to place it into an array which would looks like the code below, but again it should be a char array.
var content = File.ReadAllLines("Labyrint.txt");
File.WriteAllLines("Labyrint.txt", content.Skip(1).ToArray());
回答1:
I actually did find a way to make it work, but the problem is it only works the first time. The reason why is that the first time the program starts it deletes the first line in the file. However the file is saved with the first line deleted. So next time the program starts it will delete the new first line and so on until there are no more lines in the text file.
//Delete the first line in the textfile
var Labo = File.ReadAllLines("Labyrint.txt");
File.WriteAllLines("Labyrint.txt", Labo.Skip(1).ToArray());
//import the textfile after the first line has been deleted
string content = File.ReadAllText("Labyrint.txt", Encoding.UTF8);
// Use ToCharArray to convert string to array.
char[] array = content.ToCharArray();
回答2:
To do this you will have to manually read ll the lines using a stream reader like this
EDIT Corrected Peek Systax
StringBuilder sb = new StringBuilder();
using(StreamReader sr = new StreamReader("Labyrint.txt")){
sr.ReadLine(); //skip first Line
while(sr.Peek()!=-1){
sb.AppendLine(sr.ReadLine());
}
}
char[] content = sb.ToString().ToCharArray();
File.WriteAllText("Labyrint.txt",sb.ToString());
回答3:
Okay after 1 day with frustration and different approaches I finally found the solution. Beside finding the solution I also managed to keep the code extremely simple, so it has been a great day. You guys find the correct solution below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace ConsoleApplication25
{
class Program
{
static char[] values;
static void Main(string[] args)
{
//Increase Console Buffer Height
Console.BufferHeight = Int16.MaxValue - 1;
foreach (var line in File.ReadAllLines("Labyrint.txt", Encoding.UTF8).Skip(1))
{
values = line.ToCharArray();
Console.WriteLine(values);
}
//finds the character on index 0
Console.WriteLine(values[0]);
}
}
}
回答4:
I wonder if I am the only one that follow this thread or if people just don't have any clue of what to do? Anyway I came up with a solution that now almost work. Meaning that the first line in the text file is skipped and afterwards it is with the help of stringbuilder placed in a char array. However not everything is perfect, cause when I look the elements up by the index number, they are 2 places off where they should be. Forinstens the character B should be at index 21 but for some reason it is at index 23? I checked the text file and there are no spaces after any of the lines in the textfile, and builder doesn't have a trim method, so anybody has any suggestions? You guys find my code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace ConsoleApplication25
{
class Program
{
static char[] FloridaArray;
static string DenverString;
static void Main(string[] args)
{
//Increase Console Buffer Height
Console.BufferHeight = Int16.MaxValue - 1;
DenverString = ConvertStringArrayToString();
FloridaArray = DenverString.ToArray();
Console.WriteLine(DenverString);
Console.WriteLine(FloridaArray[43]);
}
static string ConvertStringArrayToString()
{
// Concatenate all the elements into a StringBuilder.
StringBuilder builder = new StringBuilder();
foreach (var value in File.ReadAllLines("Labyrint.txt", Encoding.UTF8).Skip(1))
{
builder.Append(value);
builder.AppendLine();
}
return builder.ToString();
}
}
}
来源:https://stackoverflow.com/questions/43189953/problems-reading-a-textfile-where-i-skip-1-line-and-place-it-into-a-char-array