C
C#ā€¢2mo ago
seijatachiis

reading a txt file and saving the paragraph as one variable

hi so ive got a txt file that looks smth like this BOOK|The Fellowship of the Ring|1954|J.R.R. Tolkien Fnheba, gur Qnex Ybeq, unf tngurerq gb uvz nyy gur Evatf bs Cbjre rkprcg bar - gur Bar Evat gung ehyrf gurz nyy - juvpu unf snyyra vagb gur unaqf bs gur uboovg Ovyob Onttvaf. Lbhat Sebqb Onttvaf svaqf uvzfrys snprq jvgu na vzzrafr gnfx jura Ovyob ragehfgf gur Evat gb uvf pner. Sebqb zhfg znxr n crevybhf wbhearl npebff Zvqqyr-rnegu gb gur Penpxf bs Qbbz, gurer gb qrfgebl gur Evat naq sbvy gur Qnex Ybeq va uvf rivy checbfr. ----- SONG|Fly Me to the Moon|1964|It Might as Well be Swing|Frank Sinatra ----- MOVIE|The Princess Bride|1987|Rob Reiner N xvaqyl tenaqsngure fvgf qbja jvgu uvf tenaqfba naq ernqf uvz n orqgvzr fgbel. Gur fgbel vf bar gung unf orra cnffrq qbja guebhtu sebz sngure gb fba sbe trarengvbaf. Nf gur tenaqsngure ernqf gur fgbel, gur npgvba pbzrf nyvir. Gur fgbel vf n pynffvp gnyr bs ybir naq nqiragher nf gur ornhgvshy Ohggrephc vf xvqanccrq naq uryq ntnvafg ure jvyy va beqre gb zneel gur bqvbhf Cevapr Uhzcreqvapx, naq Jrfgyrl (ure puvyqubbq ornh, abj erghearq nf gur Qernq Cvengr Eboregf) nggrzcgf gb fnir ure. Ba gur jnl ur zrrgf na nppbzcyvfurq fjbeqfzna naq n uhtr, fhcre fgebat tvnag, obgu bs jubz orpbzr uvf pbzcnavbaf va uvf dhrfg. Gurl zrrg n srj onq thlf nybat gur jnl gb erfphr Ohggrephc. ----- i know how to read the file, and i can save the book/song/movie parts correctly with their titles, years, etc. however for the book and movie summaries, im not sure how to do that. ive not added a summary constructor bc i wasnt sure (however a get/set for summary exists). every media entry is split by 5 dashes. its just getting the whole paragraph as one string (i dont think it can be a string array its just listed as "string property" ). im unsure of how to go about this
75 Replies
Angius
Angiusā€¢2mo ago
Not sure what the issue is, exactly? Split by |
seijatachiis
seijatachiisOPā€¢2mo ago
yes but the paragraph part is what im struggling with bc im using streamreader which reads line by line
Angius
Angiusā€¢2mo ago
Ah, this bit is separated by a newline, huh So the first line is MOVIE|The Princess Bride|1987|Rob Reiner, which you can split by |
seijatachiis
seijatachiisOPā€¢2mo ago
yes
Angius
Angiusā€¢2mo ago
The second line will be the paragraph
seijatachiis
seijatachiisOPā€¢2mo ago
will it take the whole thing? šŸ¤” i just assumed it wouldnt
Angius
Angiusā€¢2mo ago
Is the whole paragraph a single line? Or does it have line breaks? Try and see if it works If the text can be multiline, then just read all lines until ---
seijatachiis
seijatachiisOPā€¢2mo ago
it does not, in notepad if i turn off word wrap its one long line!
Angius
Angiusā€¢2mo ago
That's good
seijatachiis
seijatachiisOPā€¢2mo ago
so i want to read from book|title|etc summary summary ---- hit enter too early
Angius
Angiusā€¢2mo ago
Yeah
seijatachiis
seijatachiisOPā€¢2mo ago
this is the code rn
No description
seijatachiis
seijatachiisOPā€¢2mo ago
how do i read until the dashes then?
Angius
Angiusā€¢2mo ago
if (line == "-----")
seijatachiis
seijatachiisOPā€¢2mo ago
where would that go? also would i add \n as a delimiter for the splitting since summary is on its own line?
Angius
Angiusā€¢2mo ago
You're reading line by line, no?
seijatachiis
seijatachiisOPā€¢2mo ago
yes thats the line = dta.ReadLine() which i assume is reading one line at a time
Angius
Angiusā€¢2mo ago
So you're getting
[
"aaaaa|bbbbb|cccc",
"summary summary summary",
"-----",
"ddddd|eeeeeeee|fffffff",
"blah blah",
"-----",
]
[
"aaaaa|bbbbb|cccc",
"summary summary summary",
"-----",
"ddddd|eeeeeeee|fffffff",
"blah blah",
"-----",
]
seijatachiis
seijatachiisOPā€¢2mo ago
yes but if i use if line == "---" how do i get summary since that the line before? is there a way to just say read until the dashes so i can work with the lines until then?
Angius
Angiusā€¢2mo ago
You loop over the lines, one by one. If you encounter the --- create a new object and fill it with what follows The next line after dashes will have metadata The one after will be the summary Just use some boolean flags to tell you what is being parsed right now Or, alternatively, if the format is consistent enough... just count the lines 0, 3, 6, 9 will be metadata 1, 4, 7, 10 will be summaries
seijatachiis
seijatachiisOPā€¢2mo ago
ah i checked its not it isnt a consistet book movie song
Angius
Angiusā€¢2mo ago
Well, sure So you parse the metadata to figure out which is it
seijatachiis
seijatachiisOPā€¢2mo ago
mm i understand what youre telling me im just unable to visualize how the code would look can i read the metadata and summary as one line to break up later using | and \n šŸ¤” or well still two lines i guess but break it up later to put into its respective object oh could i read all teh text into one variable, then break it up by the dashes and from there loop thru that string array to put it into objects?
Angius
Angiusā€¢2mo ago
I'd do it something like this, in pseudocode
var meta = null;
var summary = null;
var open = true;

var items = [];

foreach line in lines
if meta is not null
summary = line
if meta is null
meta = line
if meta is not null and summary is not null and line is "-----"
var ex = line.Split('|')
var item;

if (ex[0] == "Movie")
item = new Movie(ex[1], ex[2], summary)
if (...)

items.Add(item);
meta = null;
summary = null;
var meta = null;
var summary = null;
var open = true;

var items = [];

foreach line in lines
if meta is not null
summary = line
if meta is null
meta = line
if meta is not null and summary is not null and line is "-----"
var ex = line.Split('|')
var item;

if (ex[0] == "Movie")
item = new Movie(ex[1], ex[2], summary)
if (...)

items.Add(item);
meta = null;
summary = null;
Sure, you can just read everything No need to read it line-by-line It would even be easier, tbh
var text = await File.ReadAllTextAsync("stuff.txt");
var segments = text.Split("-----");
// then loop over segments and split each by newline
var text = await File.ReadAllTextAsync("stuff.txt");
var segments = text.Split("-----");
// then loop over segments and split each by newline
seijatachiis
seijatachiisOPā€¢2mo ago
ohhh okay i see what you were saying now, with the pseudocode example okay yeah i was thinking if i could do smth like that :0 okay ill try that! thanks!
seijatachiis
seijatachiisOPā€¢2mo ago
its giving me an error this way and when viewing the different types of split, Split(String, Int32, StringSplitOptions) isnt an option :bigthonk: wonder if i have to try your pseudocode way
No description
seijatachiis
seijatachiisOPā€¢2mo ago
mm might try saving it into a single character variable šŸ¤” is not happy with that either hm
Angius
Angiusā€¢2mo ago
Odd... What's your .NET version?
Angius
Angiusā€¢2mo ago
Works just fine in .NET 8
No description
seijatachiis
seijatachiisOPā€¢2mo ago
im not sure, how do i check?
Angius
Angiusā€¢2mo ago
Open the .csproj file in Notepad for example Or just open it if it's VS Code Right-click and properties if VS
seijatachiis
seijatachiisOPā€¢2mo ago
opened in vsc
No description
Angius
Angiusā€¢2mo ago
Oof, 4.7 That's old Old-style .csproj too
seijatachiis
seijatachiisOPā€¢2mo ago
hm im fairly certain i tried to dl the latest
Angius
Angiusā€¢2mo ago
Latest is 8.0
No description
Angius
Angiusā€¢2mo ago
If you made the project via VS, though... $newproject
MODiX
MODiXā€¢2mo ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
Angius
Angiusā€¢2mo ago
The (.NET Framework) templates use the legacy versions
seijatachiis
seijatachiisOPā€¢2mo ago
actually i have to use the .NET Framework thats probably why then its for school so šŸ˜…
Angius
Angiusā€¢2mo ago
Ah Schools
seijatachiis
seijatachiisOPā€¢2mo ago
i think i can figure out how the split string[] works tho since thats still valid
Angius
Angiusā€¢2mo ago
Being in the stone age since times immemorial
seijatachiis
seijatachiisOPā€¢2mo ago
yep yeah got the split string[] working
seijatachiis
seijatachiisOPā€¢2mo ago
ive got this set up but when i print the media array i only get the very first one. i am using this
foreach (Media med in media)
{
if (med != null)
{
Console.WriteLine(med);
}
}
foreach (Media med in media)
{
if (med != null)
{
Console.WriteLine(med);
}
}
to print all of the array. the lines array DOES contain all the text when i print that...im not sure whats going on?
No description
Angius
Angiusā€¢2mo ago
Why not just use a list? Then you can .Add() to it, without having to track the indexes manually
seijatachiis
seijatachiisOPā€¢2mo ago
has to be array (school)
Angius
Angiusā€¢2mo ago
Ah Well, use the debugger and see $debug
MODiX
MODiXā€¢2mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Angius
Angiusā€¢2mo ago
Looks about correct at a glance
seijatachiis
seijatachiisOPā€¢2mo ago
ah okay its returning false for movies and songs šŸ¤” ah its bc the next line is \r\n ghm my delimiter set is only \n bc its char i cant do \r\n gotta switch back to string separators i think
Angius
Angiusā€¢2mo ago
You can trim each line of whitespace StringSplitOptions.TrimWhitespace, IIRC Or with LINQ Or just .Trim() each line before you do anything with it
seijatachiis
seijatachiisOPā€¢2mo ago
the trim white space doesnt work but ill try trimming im running into exception errors bc i think the last entry has a lot of white space or smth
seijatachiis
seijatachiisOPā€¢2mo ago
yes also not available šŸ˜…
seijatachiis
seijatachiisOPā€¢2mo ago
bit of a cunt move sir
No description
seijatachiis
seijatachiisOPā€¢2mo ago
ah okay so theres on emore \r\n at the end thats giving me errors
seijatachiis
seijatachiisOPā€¢2mo ago
and this entry is going to give me problems bc im using \r\n as a delimiter
No description
seijatachiis
seijatachiisOPā€¢2mo ago
ah and some other entries are multi paragraphs
Angius
Angiusā€¢2mo ago
Huh Well, the 0th line will still be the metadata You'll simply need to glue all the other lines to get the full text
seijatachiis
seijatachiisOPā€¢2mo ago
its not white or empty spacesince its technically a \r\n but thats whats giving me errors is that last string šŸ¤” i guess i can hardcode removing it? how do i do that?
Angius
Angiusā€¢2mo ago
Nowadays you'd use [1..] to get all lines but the first one... you'll have to use .Slice() I believe \r\n is whitespace .Trim() should remove it just fine
seijatachiis
seijatachiisOPā€¢2mo ago
i guess it doesnt count as empty then bc i have StringSplitOptions.RemoveEmptyEntries but its still an entry i have of just \r\n
Angius
Angiusā€¢2mo ago
Well, yeah, it's not empty You sure there's no string split option to trim all entries?
seijatachiis
seijatachiisOPā€¢2mo ago
yes
No description
Angius
Angiusā€¢2mo ago
Ah well .Select(l => l.Trim()) LINQ FTW
seijatachiis
seijatachiisOPā€¢2mo ago
ill have to check if im allowed to use linq hold on hm i know for previous assignments we werent i can just remove the element entirely since its only at the end it appears? šŸ¤”
Angius
Angiusā€¢2mo ago
You can just check if it's only whitespace and then continue the loop string.IsNullOrWhitespace(line)
seijatachiis
seijatachiisOPā€¢2mo ago
okay that worked so now i need to just put the multiparagraph summaries together i know that all summaries their first line will be index 4 what i dont know is how many for each? šŸ¤”
Angius
Angiusā€¢2mo ago
.Slice() has an overload that just takes the starting index Huh
seijatachiis
seijatachiisOPā€¢2mo ago
will it still be a string? im reading the document and it says it will return an array segement?
Angius
Angiusā€¢2mo ago
.Slice() works only on ArraySegment Yeah LINQ .Skip() then
seijatachiis
seijatachiisOPā€¢2mo ago
again idk if im allowed to use linq šŸ˜” much less how that work anyways
Angius
Angiusā€¢2mo ago
Array.Copy() then
seijatachiis
seijatachiisOPā€¢2mo ago
lemme look into that
Want results from more Discord servers?
Add your server