trouble reading and writing to a file c# - c#

I am currently trying to take a file of words that are not in alphabetical, re-order the words so that they are in alphabetical order (I am trying to use a non-built in sort method), and then write the newly ordered list into a new txt file(one that must be created). For example, lets say there is only five words in the txt file that are as follows "dog bat apple rabbit cat". I would want the program to resort these in alphabetical order, and then create a txt file that saves that order. As of right now, the program will iterate through the txt file, but will not save the re-ordered list into the new txt file. What is saved into the new file is this... "System.Collections.Generic.List`1[System.String]"
Truth be told, I am not very savvy with c# yet, so i apologize if my structuring or coding is not very well. The original file that is un-ordered is called "jumbled english FILTERED.ALL.txt", and the file I am trying to write to is called "english FILTERED.ALL.txt".
static void Main(string[] args)
{
// declaring integer for minimum.
int min = 0;
// declare the list for the original file
List<string> LinuxWords = new List<string>();
List<string> lexicalOrder = new List<string>();
// read the text from the file
string[] lines = System.IO.File.ReadAllLines("jumbled english FILTERED.ALL.txt");
string line = string.Empty;
// seperate each word into a string
//foreach (string line in lines)
//{
//add each word into the list.
//LinuxWords.Add(line);
//}
for (int i = 0; i < lines.Length - 1; i++)
{
for (int j = i + 1; j < lines.Length; j++)
{
if (lines[i].Length < lines[j].Length)
{
min = lines[i].Length;
}
else
{
min = lines[j].Length;
}
for (int k = 0; k < min; k++)
{
if (lines[i][k] > lines[j][k])
{
line = lines[i].ToString();
lines[i] = lines[j];
lines[j] = line;
break;
}
else if (lines[i][k] == lines[j][k])
{
continue;
}
else
{
break;
}
}
}
}
for (int i = 0; i < lines.Length; i++)
{
Console.WriteLine("The program is formatting the correct order");
lexicalOrder.Add(lines[i]);
}
//lexicalOrder.ForEach(Console.WriteLine);
//}
//LinuxWords.ForEach(Console.WriteLine);
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "english FILTERED.ALL.txt",
lexicalOrder.ToString());
// write the ordered list back into another .txt file named "english FILTERED.ALL.txt"
// System.IO.File.WriteAllLines("english FILTERED.ALL.txt", lexicalOrder);
Console.WriteLine("Finished");
}

Assuming you mean that you don't get the list saved (if that's not the problem - please be more specific) - you need to change
lexicalOrder.ToString()
to something like
lexicalOrder.Aggregate((s1, s2) => s1 + " " + s2)

Related

Find the maximum length of every column in a csv file

So, I was trying to present a csv document in a console application. However, due to the varying text size in it, the output was not in a presentable format.
To present it, I tried to count the maximum length of text for each column and then append white space to the remaining text in that column so that there's equal length of characters in each column.
I tried to get the character count, but can't seem to figure out how to proceed further.
var file = File.ReadAllLines(#"E:\File.csv");
var lineList = file.Select(x => x.Split(',').ToList()).ToList();
int maxColumn = lineList.Select(x => x.Count).Max(x => x);
List<int> maxElementSize = new List<int>();
for (int i = 0; i < maxColumn; i++)
{
//Some Logic
}
Any help would be highly appreciated.
Here's a sample console application to get maximum character length for each column :
static void Main(string[] args)
{
string CSVPath = #"D:\test.csv";
string outputText = "";
using (var reader = File.OpenText(CSVPath))
{
outputText = reader.ReadToEnd();
}
var colSplitter = ',';
var rowSplitter = new char[] { '\n' };
var rows = (from row in outputText.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries)
let cols = row.Split(colSplitter)
from col in cols
select new { totalCols = cols.Count(), cols = cols }).ToList();
int[] maxColLengths = new int[rows.Max(o => o.totalCols)];
for (int i = 0; i < rows.Count; i++)
{
for (int j = 0; j < rows[i].cols.Count(); j++)
{
int curLength = rows[i].cols[j].Trim().Length;
if (curLength > maxColLengths[j])
maxColLengths[j] = curLength;
}
}
Console.WriteLine(string.Join(", ", maxColLengths));
}
Hope this helped.
Try with a nested for loop:
var inputLines = File.ReadAllLines(#"E:\File.csv");
Dictionary<int,int> dictIndexLenght = new Dictionary<int,int>();
foreach(var line in inputLines)
{
List<string> columList = line.Split(',').ToList();
for (int i = 0; i < columList.Count; i++)
{
int tempVal = 0;
if(dictIndexLenght.TryGetValue(i,out tempVal))
{
if(tempVal<columList[i].Length)
{
dictIndexLenght[i]=columList[i].Length;
}
}
else
dictIndexLenght[i]=columList[i].Length;
}
}
Can check the result here or with this lines of code:
for(int i=0;i<dictIndexLenght.Count;i++)
{
Console.WriteLine("Column {0} : {1}", i, dictIndexLenght[i]);
}
Here's how I would do it, very similar to un-lucky's answer, only using a List<int> instead of a Dictionary<int, int>. I added dummy data for testing, but you can see the actual call to read the file is left in there, so you can just remove the dummy data and the line that reads it, and it should work ok:
static void Main(string[] args)
{
var fileLines = new List<string>
{
"Lorem, Ipsum, is, simply, dummy, text, of, the, printing, and, typesetting,",
"industry., Lorem, Ipsum, has, been, the, industry's, standard, dummy, text,",
"ever, since, the, 1500s, when, an, ",
"unknown, printer, took, a, galley, of, type, and, scrambled, it, to, make,",
"a, type, specimen, book.,",
"It, has, survived, not, only, five, centuries, but, also, the, leap,",
"into, electronic, typesetting, remaining, essentially, unchanged.,",
"It, was, popularised, in, the, 1960s, with, the, release,",
"of, Letraset, sheets, containing, Lorem, Ipsum, passages, and, more, ",
"recently, with, desktop, publishing,",
"software, like, Aldus, PageMaker, including, versions, of, Lorem, Ipsum."
};
var filePath = #"f:\public\temp\temp.csv";
var fileLinesColumns = File.ReadAllLines(filePath).Select(line => line.Split(','));
var colWidths = new List<int>();
// Remove this line to use file data
fileLinesColumns = fileLines.Select(line => line.Split(','));
// Get the max length of each column and add it to our list
foreach (var fileLineColumns in fileLinesColumns)
{
for (int i = 0; i < fileLineColumns.Length; i++)
{
if (i > colWidths.Count - 1)
{
colWidths.Add(fileLineColumns[i].Length);
}
else if (fileLineColumns[i].Length > colWidths[i])
{
colWidths[i] = fileLineColumns[i].Length;
}
}
}
// Write out our columns, padding each one to match the longest line
foreach (var fileLineColumns in fileLinesColumns)
{
for (int i = 0; i < fileLineColumns.Length; i++)
{
Console.Write(fileLineColumns[i].PadRight(colWidths[i]));
}
Console.WriteLine();
}
Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();
}
Output
Initialise your list, then loop over your lines, and within that line, loop over your columns:
for (i = 0; i < lineList.Count; i++)
{
maxElementSize[i] = 0;
}
for (i = 0; i < lineList.Count; i++)
{
for (j = 0; j < maxColumn; j++)
{
if(lineList[i][j].Length > maxElementSize[j])
maxElementSize[j] = lineList[i][j].Length
}
}
I use the following code to make sure the columns in a database are large enough to take the csv input data...
#!/usr/bin/python3
import array as arr
from csv import reader
import argparse
def csv_getFldLens (in_file, has_header=0, delimiter=','):
# open file in read mode
fldMaxLens = arr.array('i')
headers = []
has_header = has_header
with open(in_file, 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj, delimiter=delimiter)
# Iterate over each row in the csv using reader object
rcnt = 0
lastIndx = 0
for row in csv_reader:
# row variable is a list that represents a row in csv
# print(row)
if has_header and rcnt == 0:
for fld in row:
headers.append(fld)
rcnt += 1
continue
j = 0
for fld in row:
fldLen = len(fld)
if (lastIndx == 0) or (lastIndx < j):
# print("if --- li, i: ", lastIndx, i, "\n")
fldMaxLens.append(fldLen)
lastIndx = j
else:
# print("else --- li, i: ", lastIndx, i, "\n")
v1 = fldMaxLens[j]
v2 = fldLen
fldMaxLens[j] = max(v1,v2)
j = j + 1
rcnt += 1
j = 0
if has_header:
for f in headers:
print(f,": ", fldMaxLens[j])
j += 1
else:
for i in fldMaxLens:
print("Col[",j+1,"]: ",fldMaxLens[j])
j += 1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Get column lengths of CVS fields.')
parser.add_argument('--in_file', default='', help='The CSV input file')
parser.add_argument('--has_header', action='store_true', help='The CSV file has headers')
parser.add_argument('--delimiter', default=',', help='Sets the delimiter. Default is comma \',\'.')
args = parser.parse_args()
csv_getFldLens(in_file=args.in_file, has_header=args.has_header, delimiter=args.delimiter)

Read and process 100 text files in c# in parallel

I have project that reads 100 text file with 5000 words in it.
I insert the words into a list. I have a second list that contains english stop words. I compare the two lists and delete the stop words from first list.
It takes 1 hour to run the application. I want to be parallelize it. How can I do that?
Heres my code:
private void button1_Click(object sender, EventArgs e)
{
List<string> listt1 = new List<string>();
string line;
for (int ii = 1; ii <= 49; ii++)
{
string d = ii.ToString();
using (StreamReader reader = new StreamReader(#"D" + d.ToString() + ".txt"))
while ((line = reader.ReadLine()) != null)
{
string[] words = line.Split(' ');
for (int i = 0; i < words.Length; i++)
{
listt1.Add(words[i].ToString());
}
}
listt1 = listt1.ConvertAll(d1 => d1.ToLower());
StreamReader reader2 = new StreamReader("stopword.txt");
List<string> listt2 = new List<string>();
string line2;
while ((line2 = reader2.ReadLine()) != null)
{
string[] words2 = line2.Split('\n');
for (int i = 0; i < words2.Length; i++)
{
listt2.Add(words2[i]);
}
listt2 = listt2.ConvertAll(d1 => d1.ToLower());
}
for (int i = 0; i < listt1.Count(); i++)
{
for (int j = 0; j < listt2.Count(); j++)
{
listt1.RemoveAll(d1 => d1.Equals(listt2[j]));
}
}
listt1=listt1.Distinct().ToList();
textBox1.Text = listt1.Count().ToString();
}
}
}
}
I fixed many things up with your code. I don't think you need multi-threading:
private void RemoveStopWords()
{
HashSet<string> stopWords = new HashSet<string>();
using (var stopWordReader = new StreamReader("stopword.txt"))
{
string line2;
while ((line2 = stopWordReader.ReadLine()) != null)
{
string[] words2 = line2.Split('\n');
for (int i = 0; i < words2.Length; i++)
{
stopWords.Add(words2[i].ToLower());
}
}
}
var fileWords = new HashSet<string>();
for (int fileNumber = 1; fileNumber <= 49; fileNumber++)
{
using (var reader = new StreamReader("D" + fileNumber.ToString() + ".txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
foreach(var word in line.Split(' '))
{
fileWords.Add(word.ToLower());
}
}
}
}
fileWords.ExceptWith(stopWords);
textBox1.Text = fileWords.Count().ToString();
}
You are reading through the list of stopwords many times as well as continually adding to the list and re-attempting to remove the same stopwords over and again due to the way your code is structured. Your needs are also better matched to a HashSet than to a List, as it has set based operations and uniqueness already handled.
If you still wanted to make this parallel, you could do it by reading the stopword list once and passing it to an async method that will read the input file, remove the stopwords and return the resulting list, then you would need to merge the resulting lists after the asynchronous calls came back, but you had better test before deciding you need that, because that is quite a bit more work and complexity than this code already has.
If I understand you correctly, you want to:
Read all words from a file into a List
Remove all "stop words" from the List
Repeat for 99 more files, saving only the unique words
If this is correct, the code is pretty simple:
// The list of words to delete ("stop words")
var stopWords = new List<string> { "remove", "these", "words" };
// The list of files to check - you can get this list in other ways
var filesToCheck = new List<string>
{
#"f:\public\temp\temp1.txt",
#"f:\public\temp\temp2.txt",
#"f:\public\temp\temp3.txt"
};
// This list will contain all the unique words from all
// the files, except the ones in the "stopWords" list
var uniqueFilteredWords = new List<string>();
// Loop through all our files
foreach (var fileToCheck in filesToCheck)
{
// Read all the file text into a varaible
var fileText = File.ReadAllText(fileToCheck);
// Split the text into distinct words (splitting on null
// splits on all whitespace) and ignore empty lines
var fileWords = fileText.Split(null)
.Where(line => !string.IsNullOrWhiteSpace(line))
.Distinct();
// Add all the words from the file, except the ones in
// your "stop list" and those that are already in the list
uniqueFilteredWords.AddRange(fileWords.Except(stopWords)
.Where(word => !uniqueFilteredWords.Contains(word)));
}
This can be condensed into a single line with no explicit loop:
// This list will contain all the unique words from all
// the files, except the ones in the "stopWords" list
var uniqueFilteredWords = filesToCheck.SelectMany(fileToCheck =>
File.ReadAllText(fileToCheck)
.Split(null)
.Where(word => !string.IsNullOrWhiteSpace(word) &&
!stopWords.Any(stopWord => stopWord.Equals(word,
StringComparison.OrdinalIgnoreCase)))
.Distinct());
This code processed over 100 files with more than 12000 words each in less than a second (WAY less than a second... 0.0001782 seconds)
One issue I see here that can help improve performance is listt1.ConvertAll() will run in O(n) on the list. You are already looping to add the items to the list, why not convert them to lower case there. Also why not store the words in a hash set, so you can do look up and insertion in O(1). You could store the list of stop words in a hash set and when you are reading your text input see if the word is a stop word and if its not add it to the hash set to output the user.

StreamWriter C# formatting output

Problem Statement
In order to run gene annotation software, I need to prepare two types of files, vcard files and coverage tables, and there has to be one-to-one match of vcard to coverage table. Since Im running 2k samples, its hard to identify which file is not one-to-one match. I know that both files have unique identifier numbers, hence, if both folders have files that have same unique numbers, i treat that as "same" file
I made a program that compares two folders and reports unique entries in each folder. To do so, I made two list that contains unique file names to each directory.
I want to format the report file (tab delimited .txt file) such that it looks something like below:
Unique in fdr1 Unique in fdr2
file x file a
file y file b
file z file c
I find this difficult to do because I have to iterate twice (since I have two lists), but there is no way of going back to the previous line in StreamWriter as far as I know. Basically, once I iterate through the first list and fill the first column, how can I fill the second column with the second list?
Can someone help me out with this?
Thanks
If design of the code has to change (i.e. one list instead of two), please let me know
As requested by some user, this is how I was going to do (not working version)
// Write report
using (StreamWriter sw = new StreamWriter(dest_txt.Text + #"\" + "Report.txt"))
{
// Write headers
sw.WriteLine("Unique Entries in Folder1" + "\t" + "Unique Entries in Folder2");
// Write unique entries in fdr1
foreach(string file in fdr1FileList)
{
sw.WriteLine(file + "\t");
}
// Write unique entries in fdr2
foreach (string file in fdr2FileList)
{
sw.WriteLine(file + "\t");
}
sw.Dispose();
}
As requested for my approach for finding unique entries, here's my code snippet
Dictionary<int, bool> fdr1Dict = new Dictionary<int, bool>();
Dictionary<int, bool> fdr2Dict = new Dictionary<int, bool>();
List<string> fdr1FileList = new List<string>();
List<string> fdr2FileList = new List<string>();
string fdr1Path = folder1_txt.Text;
string fdr2Path = folder2_txt.Text;
// File names in the specified directory; path not included
string[] fdr1FileNames = Directory.GetFiles(fdr1Path).Select(Path.GetFileName).ToArray();
string[] fdr2FileNames = Directory.GetFiles(fdr2Path).Select(Path.GetFileName).ToArray();
// Iterate through the first directory, and add GL number to dictionary
for(int i = 0; i < fdr1FileNames.Length; i++)
{
// Grabs only the number from the file name
string number = Regex.Match(fdr1FileNames[i], #"\d+").ToString();
int glNumber;
// Make sure it is a number
if(Int32.TryParse(number, out glNumber))
{
fdr1Dict[glNumber] = true;
}
// If number not present, raise exception
else
{
throw new Exception(String.Format("GL Number not found in: {0}", fdr1FileNames[i]));
}
}
// Iterate through the second directory, and add GL number to dictionary
for (int i = 0; i < fdr2FileNames.Length; i++)
{
// Grabs only the number from the file name
string number = Regex.Match(fdr2FileNames[i], #"\d+").ToString();
int glNumber;
// Make sure it is a number
if (Int32.TryParse(number, out glNumber))
{
fdr2Dict[glNumber] = true;
}
// If number not present, raise exception
else
{
throw new Exception(String.Format("GL Number not found in: {0}", fdr2FileNames[i]));
}
}
// Iterate through the first directory, and find files that are unique to it
for (int i = 0; i < fdr1FileNames.Length; i++)
{
int glNumber = Int32.Parse(Regex.Match(fdr1FileNames[i], #"\d+").Value);
// If same file is not present in the second folder add to the list
if(!fdr2Dict[glNumber])
{
fdr1FileList.Add(fdr1FileNames[i]);
}
}
// Iterate through the second directory, and find files that are unique to it
for (int i = 0; i < fdr2FileNames.Length; i++)
{
int glNumber = Int32.Parse(Regex.Match(fdr2FileNames[i], #"\d+").Value);
// If same file is not present in the first folder add to the list
if (!fdr1Dict[glNumber])
{
fdr2FileList.Add(fdr2FileNames[i]);
}
I am a quite confident that this will work as I've tested it:
static void Main(string[] args)
{
var firstDir = #"Path1";
var secondDir = #"Path2";
var firstDirFiles = System.IO.Directory.GetFiles(firstDir);
var secondDirFiles = System.IO.Directory.GetFiles(secondDir);
print2Dirs(firstDirFiles, secondDirFiles);
}
private static void print2Dirs(string[] firstDirFile, string[] secondDirFiles)
{
var maxIndex = Math.Max(firstDirFile.Length, secondDirFiles.Length);
using (StreamWriter streamWriter = new StreamWriter("result.txt"))
{
streamWriter.WriteLine(string.Format("{0,-150}{1,-150}", "Unique in fdr1", "Unique in fdr2"));
for (int i = 0; i < maxIndex; i++)
{
streamWriter.WriteLine(string.Format("{0,-150}{1,-150}",
firstDirFile.Length > i ? firstDirFile[i] : string.Empty,
secondDirFiles.Length > i ? secondDirFiles[i] : string.Empty));
}
}
}
It's a quite simple code but if you need help understanding it just let me know :)
I would construct each line at a time. Something like this:
int row = 0;
string[] fdr1FileList = new string[0];
string[] fdr2FileList = new string[0];
while (row < fdr1FileList.Length || row < fdr2FileList.Length)
{
string rowText = "";
rowText += (row >= fdr1FileList.Length ? "\t" : fdr1FileList[row] + "\t");
rowText += (row >= fdr2FileList.Length ? "\t" : fdr2FileList[row]);
row++;
}
Try something like this:
static void Main(string[] args)
{
Dictionary<int, string> fdr1Dict = FilesToDictionary(Directory.GetFiles("path1"));
Dictionary<int, string> fdr2Dict = FilesToDictionary(Directory.GetFiles("path2"));
var unique_f1 = fdr1Dict.Where(f1 => !fdr2Dict.ContainsKey(f1.Key)).ToArray();
var unique_f2 = fdr2Dict.Where(f2 => !fdr1Dict.ContainsKey(f2.Key)).ToArray();
int f1_size = unique_f1.Length;
int f2_size = unique_f2.Length;
int list_length = 0;
if (f1_size > f2_size)
{
list_length = f1_size;
Array.Resize(ref unique_f2, list_length);
}
else
{
list_length = f2_size;
Array.Resize(ref unique_f1, list_length);
}
using (StreamWriter writer = new StreamWriter("output.txt"))
{
writer.WriteLine(string.Format("{0,-30}{1,-30}", "Unique in fdr1", "Unique in fdr2"));
for (int i = 0; i < list_length; i++)
{
writer.WriteLine(string.Format("{0,-30}{1,-30}", unique_f1[i].Value, unique_f2[i].Value));
}
}
}
static Dictionary<int, string> FilesToDictionary(string[] filenames)
{
Dictionary<int, string> dict = new Dictionary<int, string>();
for (int i = 0; i < filenames.Length; i++)
{
int glNumber;
string filename = Path.GetFileName(filenames[i]);
string number = Regex.Match(filename, #"\d+").ToString();
if (int.TryParse(number, out glNumber))
dict.Add(glNumber, filename);
}
return dict;
}

Loop only fires once

Hi guys i am reading a file containing playlists.
(Rows of mp3 file locations)
I currently have the format ( name ; c:\song.mp3 ; c:\song.mp3 ; c:\song.mp3 ; c:\song.mp3 etc etc)
There are many rows of these in the file.
I have tried both foreach loops and for loops to try and solve this (as shown below)
string[] lines = File.ReadAllLines("playlists.txt");
MessageBox.Show(lines.Count().ToString());
for (int y = 0; y <= lines.Count(); y++)
{
string[] res = lines[y].Split(';');
for (int x = 0; x <= res.Count(); x ++)
{
if (x == 0) { currentPlaylist = new Playlist(res[x]); }
else { currentPlaylist.Add(new MP3(res[x])); }
}
}
But for some reason it will only loop once (I have replaced the outer loop with a foreach which had the same result.
even if the lines.Count() shown in the messagebox shows a number greater then 1
I'm sure once this is solved it must be basic mistake but Im lost
Thanks
EDIT* this is the file dont know how this will help...
Library;C:\Users\Blah\Desktop\Iphone Music\Queens of the Stone Age - If I Had A Tail.mp3
playlist1;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Can t Stop.mp3;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Otherside .mp3
playlist2;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Otherside .mp3;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - Best Of You.mp3
playlist3;C:\Users\Blah\Desktop\Iphone Music\Red Hot Chili Peppers - Otherside.mp3;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - The Pretender.mp3
playlist4;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - Everlong.mp3;C:\Users\Blah\Desktop\Iphone Music\Foo Fighters - My Hero.mp3;C:\Users\Blah\Desktop\Iphone Music\I Am Giant - City Limits.mp3
I put it in as code so it would be easier to read
The only issue i am having is that the inner loop only fires once and i am unsure as to why.
Something in the following ....
for (int x = 0; x <= res.Count(); x ++)
{
if (x == 0) { currentPlaylist = new Playlist(res[x]); }
else { currentPlaylist.Add(new MP3(res[x])); }
}
is causing the outer loop to only fire once no matter the amount of lines in the code, if i remove the inner loop the outer loop loops the expected amount of times
Based on your question and comments you wrote, I think you are looking for something like:
// This method read the file and populate a List of Playlist.
// Each PlayList contain a list of MP3 songs
public List<Playlist> GetPlayLists()
{
// read all lines from the text file
string[] lines = File.ReadAllLines(#"c:\Temp\playlists.txt");
// declare a playlists variable to hold all the playlists and their songs
var playlists = new List<Playlist>();
// Loop through all the playlists (each line in the text file represents a playlist)
for (int plIdx = 0; plIdx < lines.Length; plIdx++)
{
// split in order to fins all the MP3 songs
string[] res = lines[plIdx].Split(';');
// create a new playlist (its name is passed into the constructor)
var playlist = new Playlist(res[0]);
// loop the songs (starting from index 1 since index=0 is the playlist name)
for (int songIdx = 1; songIdx < res.Length; songIdx++)
{
// Add to the playlist each song
playlist.Add(new MP3(res[songIdx]));
}
playlists.Add(playlist);
}
return playlists;
}
// Play list class containing all the MP3 songs (each line in text file)
class Playlist
{
public List<MP3> SongList { get; private set; }
public string Name { get; private set; }
public Playlist(string name)
{
Name = name;
SongList = new List<MP3>();
}
public void Add(MP3 mp3)
{
SongList.Add(mp3);
}
}
// MP3 Song class
class MP3
{
public string Location { get; private set; }
public MP3(string location)
{
Location = location;
}
}
This is how your playlists variable looks like after it has been populated with your playlists.txt file:
Do the following steps in order to make it work. Once working, you will have a reference so you can simply merge it into your existing project.
Create a new Console Application
Your class program:
class Program
{
static void Main(string[] args)
{
// FileReader is the class that contains the method that read from the playlists.txt file
var filereader = new FileReader();
var playlists = filereader.GetPlayLists();
}
}
Create a FileReader class and put the GetPlayLists method into
class FileReader
{
// This method read the file and populate a List of Playlist.
// Each PlayList contain a list of MP3 songs
public List<Playlist> GetPlayLists()
{
// .....
}
}
Put the other classes PlayList and MP3.
Now you should be able to run it without any issues.
Your code should throw an exception, as far as I can see. You use <= in your loop conditions, but when x == res.Count() you can not access res[x], because the index must be between 0 and Count-1.
Try to replace <= by <, at least.
And don't use Count() on arrays. They have .Length property.
Assuming that what you posted is the file you're reading then each line contains a single file location of the mp3. After splitting each line with ';' you can safely assume that the 2nd entry of the new array is the file path.
string[] lines = File.ReadAllLines("playlists.txt"); // 100 lines
for (int y = 0; y < lines.Length; y++) loops through array index 0 to 99
{
string[] res = lines[y].Split(';'); // lines[y] should be something like c:\song.mp3 ; c:\song.mp3 ; c:\song.mp3
// res[0] = Library
// res[1] = C:\Users\Blah\Desktop\Iphone Music\Queens of the Stone Age - If I Had A Tail.mp3
currentPlaylist = new Playlist(res[0].Trim());
currentPlaylist.Add(new MP3(res[1].Trim()));
}
Or if each line has multiple file paths..
string[] lines = File.ReadAllLines("playlists.txt"); // 100 lines
for (int y = 0; y < lines.Length; y++) loops through array index 0 to 99
{
string[] res = lines[y].Split(';'); // lines[y] should be something like c:\song.mp3 ; c:\song.mp3 ; c:\song.mp3
// res[0] = Library
// res[1] = C:\Users\Blah\Desktop\Iphone Music\Queens of the Stone Age - If I Had A Tail.mp3
currentPlaylist = new Playlist(res[0].Trim()); // res[0] is the playlist name right?
for(int x = 1; x < res.Length; x++)
{
currentPlaylist.Add(new MP3(res[x].Trim()));
}
}
Try this, you should not initialise currentPlayList in the inner loop as it will get reinitialised every time it loops through y
Playlist currentPlaylist;
string[] lines = File.ReadAllLines("playlists.txt");
for (int y = 0; y < lines.Length; y++)
{
if (y == 0) {
currentPlaylist = new Playlist();
}
string[] res = lines[y].Split(';');
for (int x = 0; x < res.Length; x ++)
{
currentPlaylist.Add(new MP3(res[x]));
}
}
Your outer loop actually runs more than once, but because you reinitialise the playlist, the only state that is persisted at the end of the outer loop is the very last iteration, this giving the appearance of having run only once.

Index was outside the bounds of the array in a for loop C#

So I've been woking a Console game for a while now and I desided to use .txt files to hold the maps. This code opens and stores the txt file contents:
static void LoadMap(string fname)
{
string _org = File.ReadAllText("Maps/" + fname + ".txt");
_org.Split(',');
string[] _tmp = new string[_org.Length];
for (int i=0;i<_org.Length;i++)
{
_tmp[i] = _org[i].ToString();
}
//This line has the error
for (int i=0;_tmp[i]!="$";i+=2)
{
mapwidth += 1;
}
for (int i=0;i<_tmp.Length;i++)
{
leveldata.Add(_tmp[i]);
}
}
I get this error: Index was outside the bounds of the array. I can't figgure out why. Can anyone help? Thanks
Check that variable i does not take values beyond the Length - 1 of the array.
static void LoadMap(string fname)
{
string _org = File.ReadAllText("Maps/" + fname + ".txt");
_org.Split(',');
string[] _tmp = new string[_org.Length];
for (int i = 0;i < _org.Length; i++)
{
_tmp[i] = _org[i].ToString();
}
for (int i = 0;i < _tmp.Length && _tmp[i] != "$"; i += 2)
{
mapwidth += 1;
}
for (int i = 0;i < _tmp.Length; i++)
{
leveldata.Add(_tmp[i]);
}
}
I am not super sure what you are getting at here but I feel a foreach may be a step in the right direction.
First, here are a few things I noticed in the code you presented.
string _org = File.ReadAllText("Maps/" + fname + ".txt");
_org.Split(',');
Here you have ingested your text file and split _org with a comma delimiter. Though you never actually assigned the split array to a variable. _org is still a string, not a string[].
string[] _tmp = new string[_org.Length];
for (int i = 0; i < _org.Length; i++)
{
_tmp[i] = _org[i].ToString();
}
In this block we have set _tmp as a string array using the length of _org. The Length property in this case will retrieve the number of characters in the string _org. assuming _org is set to "foo" the size of the _tmparray is now 3. (0,1,2)
You then go on to load _tmp with the nth character of _org converted to a string.
At this point we have the following in our variables.
_org = "foo"
_tmp = {"f","o","o"}
This next part caught me a bit off guard as I cant tell for certain what you are trying to accomplish.
for (int i = 0; _tmp[i] != "$"; i += 2)
{
mapwidth += 1;
}
You are in a for loop until _tmp[i] equals "$", and you are adding 2 to i every time you move through the loop using i += 2. This logic will break the loop until it hits a "$", or if i grows outside of the bounds of the array it will throw an exception.
You then go on to add one to the mapwidth for every two increments of i
In this final block you are adding the contents of the _tmp array to leveldata
for (int i = 0; i < _tmp.Length; i++)
{
leveldata.Add(_tmp[i]);
}
Now for the good stuff, foreach.
I have made the assumption you wanted to set _org as an array, and that you wanted everything that was not a "$". That in mind, this should accomplish what you were going for, though I am not sure the purpose of incrementing by 2 in the second for loop so I have left that out for now.
static void LoadMap(string fname)
{
string[] _org = File.ReadAllText("Maps/" + fname + ".txt").Split(',');
foreach (var _tmp in _org)
{
if (_tmp != "$")
{
mapwidth += 1;
}
leveldata.Add(_tmp);
}
}
If you want to continue coding it is always a good idea to get a good grasp on the basics.
Though a bit dated here is a good tutorial by Microsoft for programming basics in C#:
C# Programmer's Reference
Try this link.
https://www.programiz.com/csharp-programming/foreach-loop
Have suggestion keyword to avoid Index was outside the bounds of the array error.
Sample code.
for(int i = 0; i < myArray.Length; i++)
{
// Coding to Array for loop.
}

Categories

Resources