FileStream Class C# Input from txt file to array Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?Create ArrayList from arrayAdding values to a C# arrayCreating a byte array from a streamRemove empty elements from an array in JavascriptPHP: Delete an element from an arrayGet int value from enum in C#How to remove item from array by value?How do I remove a particular element from an array in JavaScript?Remove duplicate values from JS array
Why is it faster to reheat something than it is to cook it?
Induction Proof for Sequences
Is it possible for SQL statements to execute concurrently within a single session in SQL Server?
How long can equipment go unused before powering up runs the risk of damage?
Semigroups with no morphisms between them
Google .dev domain strangely redirects to https
Lagrange four-squares theorem --- deterministic complexity
Karn the great creator - 'card from outside the game' in sealed
Why we try to capture variability?
How does light 'choose' between wave and particle behaviour?
Project Euler #1 in C++
What initially awakened the Balrog?
What does this say in Elvish?
Is multiple magic items in one inherently imbalanced?
How were pictures turned from film to a big picture in a picture frame before digital scanning?
Flash light on something
Do wooden building fires get hotter than 600°C?
Most bit efficient text communication method?
Draw 4 of the same figure in the same tikzpicture
How does Belgium enforce obligatory attendance in elections?
Why are vacuum tubes still used in amateur radios?
Strange behavior of Object.defineProperty() in JavaScript
How to pronounce 伝統色
Why are my pictures showing a dark band on one edge?
FileStream Class C# Input from txt file to array
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?Create ArrayList from arrayAdding values to a C# arrayCreating a byte array from a streamRemove empty elements from an array in JavascriptPHP: Delete an element from an arrayGet int value from enum in C#How to remove item from array by value?How do I remove a particular element from an array in JavaScript?Remove duplicate values from JS array
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to make use of StreamReader and taking data from text files and store it into an array. I am having an issue where I think the fix is simple, but I am stumped. When I print the array, it prints every single token in the txt file instead of the single line of data containing the search name along with the 11 int tokens.
Long_Name.txtsample
public class SSA
public void Search()
Console.WriteLine("Name to search for?");
string n = Console.ReadLine();
Search(n, "Files/Names_Long.txt");
public int[] Search(string targetName, string fileName)
int[] nums = new int[11];
char[] delimiters = ' ', 'n', 't', 'r' ;
using (TextReader sample2 = new StreamReader("Files/Exercise_Files/SSA_Names_Long.txt"))
string searchName = sample2.ReadLine();
if (searchName.Contains(targetName))
Console.WriteLine("Found 0!", targetName);
Console.WriteLine("YeartRank");
else
Console.WriteLine("0 was not found!", targetName);
while (searchName != null)
string[] tokensFromLine = searchName.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
int arrayIndex = 0;
int year = 1900;
foreach (string token in tokensFromLine)
int arrval;
if (int.TryParse(token, out arrval))
nums[arrayIndex] = arrval;
year += 10;
Console.WriteLine("0t1", year, arrval);
arrayIndex++;
searchName = sample2.ReadLine();
return nums;
c# arrays delimiter streamreader
|
show 1 more comment
I am trying to make use of StreamReader and taking data from text files and store it into an array. I am having an issue where I think the fix is simple, but I am stumped. When I print the array, it prints every single token in the txt file instead of the single line of data containing the search name along with the 11 int tokens.
Long_Name.txtsample
public class SSA
public void Search()
Console.WriteLine("Name to search for?");
string n = Console.ReadLine();
Search(n, "Files/Names_Long.txt");
public int[] Search(string targetName, string fileName)
int[] nums = new int[11];
char[] delimiters = ' ', 'n', 't', 'r' ;
using (TextReader sample2 = new StreamReader("Files/Exercise_Files/SSA_Names_Long.txt"))
string searchName = sample2.ReadLine();
if (searchName.Contains(targetName))
Console.WriteLine("Found 0!", targetName);
Console.WriteLine("YeartRank");
else
Console.WriteLine("0 was not found!", targetName);
while (searchName != null)
string[] tokensFromLine = searchName.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
int arrayIndex = 0;
int year = 1900;
foreach (string token in tokensFromLine)
int arrval;
if (int.TryParse(token, out arrval))
nums[arrayIndex] = arrval;
year += 10;
Console.WriteLine("0t1", year, arrval);
arrayIndex++;
searchName = sample2.ReadLine();
return nums;
c# arrays delimiter streamreader
putsearchName = sample2.ReadLine();outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine);nums[i] = arrval;you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.
– Irdis
Mar 8 at 22:07
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
looks fine.for (int i = 0; i < nums.Length; i++)delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.
– Irdis
Mar 8 at 22:31
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
you are welcome
– Irdis
Mar 8 at 22:39
|
show 1 more comment
I am trying to make use of StreamReader and taking data from text files and store it into an array. I am having an issue where I think the fix is simple, but I am stumped. When I print the array, it prints every single token in the txt file instead of the single line of data containing the search name along with the 11 int tokens.
Long_Name.txtsample
public class SSA
public void Search()
Console.WriteLine("Name to search for?");
string n = Console.ReadLine();
Search(n, "Files/Names_Long.txt");
public int[] Search(string targetName, string fileName)
int[] nums = new int[11];
char[] delimiters = ' ', 'n', 't', 'r' ;
using (TextReader sample2 = new StreamReader("Files/Exercise_Files/SSA_Names_Long.txt"))
string searchName = sample2.ReadLine();
if (searchName.Contains(targetName))
Console.WriteLine("Found 0!", targetName);
Console.WriteLine("YeartRank");
else
Console.WriteLine("0 was not found!", targetName);
while (searchName != null)
string[] tokensFromLine = searchName.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
int arrayIndex = 0;
int year = 1900;
foreach (string token in tokensFromLine)
int arrval;
if (int.TryParse(token, out arrval))
nums[arrayIndex] = arrval;
year += 10;
Console.WriteLine("0t1", year, arrval);
arrayIndex++;
searchName = sample2.ReadLine();
return nums;
c# arrays delimiter streamreader
I am trying to make use of StreamReader and taking data from text files and store it into an array. I am having an issue where I think the fix is simple, but I am stumped. When I print the array, it prints every single token in the txt file instead of the single line of data containing the search name along with the 11 int tokens.
Long_Name.txtsample
public class SSA
public void Search()
Console.WriteLine("Name to search for?");
string n = Console.ReadLine();
Search(n, "Files/Names_Long.txt");
public int[] Search(string targetName, string fileName)
int[] nums = new int[11];
char[] delimiters = ' ', 'n', 't', 'r' ;
using (TextReader sample2 = new StreamReader("Files/Exercise_Files/SSA_Names_Long.txt"))
string searchName = sample2.ReadLine();
if (searchName.Contains(targetName))
Console.WriteLine("Found 0!", targetName);
Console.WriteLine("YeartRank");
else
Console.WriteLine("0 was not found!", targetName);
while (searchName != null)
string[] tokensFromLine = searchName.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
int arrayIndex = 0;
int year = 1900;
foreach (string token in tokensFromLine)
int arrval;
if (int.TryParse(token, out arrval))
nums[arrayIndex] = arrval;
year += 10;
Console.WriteLine("0t1", year, arrval);
arrayIndex++;
searchName = sample2.ReadLine();
return nums;
c# arrays delimiter streamreader
c# arrays delimiter streamreader
edited Mar 10 at 2:08
pijoborde
asked Mar 8 at 21:45
pijobordepijoborde
106
106
putsearchName = sample2.ReadLine();outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine);nums[i] = arrval;you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.
– Irdis
Mar 8 at 22:07
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
looks fine.for (int i = 0; i < nums.Length; i++)delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.
– Irdis
Mar 8 at 22:31
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
you are welcome
– Irdis
Mar 8 at 22:39
|
show 1 more comment
putsearchName = sample2.ReadLine();outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine);nums[i] = arrval;you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.
– Irdis
Mar 8 at 22:07
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
looks fine.for (int i = 0; i < nums.Length; i++)delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.
– Irdis
Mar 8 at 22:31
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
you are welcome
– Irdis
Mar 8 at 22:39
put
searchName = sample2.ReadLine(); outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine); nums[i] = arrval; you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.– Irdis
Mar 8 at 22:07
put
searchName = sample2.ReadLine(); outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine); nums[i] = arrval; you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.– Irdis
Mar 8 at 22:07
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
looks fine.
for (int i = 0; i < nums.Length; i++) delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.– Irdis
Mar 8 at 22:31
looks fine.
for (int i = 0; i < nums.Length; i++) delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.– Irdis
Mar 8 at 22:31
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
you are welcome
– Irdis
Mar 8 at 22:39
you are welcome
– Irdis
Mar 8 at 22:39
|
show 1 more comment
1 Answer
1
active
oldest
votes
That sure is a lot of code, this snippet does not account for duplicates but if you were willing to work with linq, something like this might help? You could also just iterate over the file_text array using a for-loop and perhaps set your return array in that. Anyway a lot less code to mess with
public int[] Search(string targetName, string fileName)
List<string> file_text = File.ReadAllLines("Files/Exercise_Files/SSA_Names_Long.txt").ToList();
List<string> matching_lines = file_text.Where(w => w == targetName).ToList();
List<int> nums = new List<int>();
foreach (string test_line in matching_lines)
nums.Add(file_text.IndexOf(test_line));
return nums.ToArray();
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071436%2ffilestream-class-c-sharp-input-from-txt-file-to-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
That sure is a lot of code, this snippet does not account for duplicates but if you were willing to work with linq, something like this might help? You could also just iterate over the file_text array using a for-loop and perhaps set your return array in that. Anyway a lot less code to mess with
public int[] Search(string targetName, string fileName)
List<string> file_text = File.ReadAllLines("Files/Exercise_Files/SSA_Names_Long.txt").ToList();
List<string> matching_lines = file_text.Where(w => w == targetName).ToList();
List<int> nums = new List<int>();
foreach (string test_line in matching_lines)
nums.Add(file_text.IndexOf(test_line));
return nums.ToArray();
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
add a comment |
That sure is a lot of code, this snippet does not account for duplicates but if you were willing to work with linq, something like this might help? You could also just iterate over the file_text array using a for-loop and perhaps set your return array in that. Anyway a lot less code to mess with
public int[] Search(string targetName, string fileName)
List<string> file_text = File.ReadAllLines("Files/Exercise_Files/SSA_Names_Long.txt").ToList();
List<string> matching_lines = file_text.Where(w => w == targetName).ToList();
List<int> nums = new List<int>();
foreach (string test_line in matching_lines)
nums.Add(file_text.IndexOf(test_line));
return nums.ToArray();
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
add a comment |
That sure is a lot of code, this snippet does not account for duplicates but if you were willing to work with linq, something like this might help? You could also just iterate over the file_text array using a for-loop and perhaps set your return array in that. Anyway a lot less code to mess with
public int[] Search(string targetName, string fileName)
List<string> file_text = File.ReadAllLines("Files/Exercise_Files/SSA_Names_Long.txt").ToList();
List<string> matching_lines = file_text.Where(w => w == targetName).ToList();
List<int> nums = new List<int>();
foreach (string test_line in matching_lines)
nums.Add(file_text.IndexOf(test_line));
return nums.ToArray();
That sure is a lot of code, this snippet does not account for duplicates but if you were willing to work with linq, something like this might help? You could also just iterate over the file_text array using a for-loop and perhaps set your return array in that. Anyway a lot less code to mess with
public int[] Search(string targetName, string fileName)
List<string> file_text = File.ReadAllLines("Files/Exercise_Files/SSA_Names_Long.txt").ToList();
List<string> matching_lines = file_text.Where(w => w == targetName).ToList();
List<int> nums = new List<int>();
foreach (string test_line in matching_lines)
nums.Add(file_text.IndexOf(test_line));
return nums.ToArray();
answered Mar 8 at 22:46
vscodervscoder
1499
1499
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
add a comment |
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
actually you loaded entire file into the memory. i would recommend you using stream reader as it was made by the author.
– Irdis
Mar 8 at 22:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071436%2ffilestream-class-c-sharp-input-from-txt-file-to-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
put
searchName = sample2.ReadLine();outside foreach block, because you shouldn't jump to the next line in a file if you are not finished with current (tokensFromLine);nums[i] = arrval;you must increment index with each iteration of your foreach loop instead of running through the nums. so declare i outside the foreach loop and increment it like i++ before analyzing next token.– Irdis
Mar 8 at 22:07
I am now getting an: Index was outside the bounds of the array exception.
– pijoborde
Mar 8 at 22:26
looks fine.
for (int i = 0; i < nums.Length; i++)delete this. you dont need to iterate over nums for every token. you are accessing num's position though arrayIndex.– Irdis
Mar 8 at 22:31
You are correct. I had made an edit in the meantime I also forgot about that was tripping the index bounds. Thank you for the help!
– pijoborde
Mar 8 at 22:37
you are welcome
– Irdis
Mar 8 at 22:39