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
Not sure what the issue is, exactly?
Split by
|
yes but the paragraph part is what im struggling with
bc im using streamreader which reads line by line
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 |
yes
The second line will be the paragraph
will it take the whole thing?
š¤ i just assumed it wouldnt
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
---
it does not, in notepad if i turn off word wrap its one long line!
That's good
so i want to read from
book|title|etc
summary summary
----
hit enter too early
Yeah
this is the code rn
how do i read until the dashes then?
if (line == "-----")
where would that go? also would i add \n as a delimiter for the splitting since summary is on its own line?
You're reading line by line, no?
yes thats the line = dta.ReadLine() which i assume is reading one line at a time
So you're getting
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?
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 summariesah i checked its not
it isnt a consistet book movie song
Well, sure
So you parse the metadata to figure out which is it
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?
I'd do it something like this, in pseudocode
Sure, you can just read everything
No need to read it line-by-line
It would even be easier, tbh
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!
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
mm might try saving it into a single character variable š¤
is not happy with that either hm
Odd...
What's your .NET version?
Works just fine in .NET 8
im not sure, how do i check?
Open the
.csproj
file in Notepad for example
Or just open it if it's VS Code
Right-click and properties if VSopened in vsc
Oof, 4.7
That's old
Old-style
.csproj
toohm im fairly certain i tried to dl the latest
Latest is 8.0
If you made the project via VS, though...
$newproject
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
The
(.NET Framework)
templates use the legacy versionsactually
i have to use the .NET Framework
thats probably why then
its for school so š
Ah
Schools
i think i can figure out how the split string[] works tho since thats still valid
Being in the stone age since times immemorial
yep
yeah got the split string[] working
ive got this set up but when i print the media array i only get the very first one. i am using this
to print all of the array. the lines array DOES contain all the text when i print that...im not sure whats going on?
Why not just use a list?
Then you can
.Add()
to it, without having to track the indexes manuallyhas to be array (school)
Ah
Well, use the debugger and see
$debug
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.
Looks about correct at a glance
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
You can trim each line of whitespace
StringSplitOptions.TrimWhitespace
, IIRC
Or with LINQ
Or just .Trim()
each line before you do anything with itthe 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
Ah, my bad, it's
.TrimEntries
https://learn.microsoft.com/en-us/dotnet/api/system.stringsplitoptions?view=net-8.0yes also not available š
bit of a cunt move sir
ah okay so theres on emore \r\n at the end thats giving me errors
and this entry is going to give me problems bc im using \r\n as a delimiter
ah and some other entries are multi paragraphs
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
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?
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 finei guess it doesnt count as empty then bc i have StringSplitOptions.RemoveEmptyEntries but its still an entry i have of just \r\n
Well, yeah, it's not empty
You sure there's no string split option to trim all entries?
yes
Ah well
.Select(l => l.Trim())
LINQ FTWill 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? š¤
You can just check if it's only whitespace and then
continue
the loop
string.IsNullOrWhitespace(line)
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? š¤
.Slice()
has an overload that just takes the starting index
Huhwill it still be a string? im reading the document and it says it will return an array segement?
.Slice()
works only on ArraySegment
Yeah
LINQ .Skip()
thenagain idk if im allowed to use linq š
much less how that work anyways
Array.Copy()
thenlemme look into that