C
C#3y ago
Triufelis

Copying from files [Answered]

I have a task. 2 files. We print from the first one up until we find the first word that matches in the second file that we had not printed. F.e. File1 text is : My name is John. File2 text is: Is this absurd?. So the outcome should be My name is this absurd john. I can only read a line at a time from each file. Example of the code structure is attached via picture. Any help is appreciated.
16 Replies
TheSnowOwl
TheSnowOwl3y ago
read a line of both files, one by one, compare them, repeat until your condition is met
Triufelis
TriufelisOP3y ago
the problem is that i might have to print multiple lines of one file before i print the other file
ero
ero3y ago
why's that a problem?
Triufelis
TriufelisOP3y ago
Im not really sure how to accomplish said goal. Because if i read one line from each file every single while loop(my while loop would be until both lines are null). I would have to save the lines i havent used. Do i do it in a string array? And if i used half of the line. Do i do a substring then? And save the substring line to the said array aswell? Wouldnt that be a bit inneficient?
ero
ero3y ago
just close and re-open the stream?
using var fs = File.CreateText(fileOut);
using var r1 = new StreamReader(fileIn1, Encoding.UTF8);

while (r1.ReadLine() is string line)
{
using var r2 = new StreamReader(fileIn2, Encoding.UTF8);

// ...
}
using var fs = File.CreateText(fileOut);
using var r1 = new StreamReader(fileIn1, Encoding.UTF8);

while (r1.ReadLine() is string line)
{
using var r2 = new StreamReader(fileIn2, Encoding.UTF8);

// ...
}
or whatever
Triufelis
TriufelisOP3y ago
If i close the streamreader and reopen it. Does it start reading from the beggining of the file. Or does it continue?
ero
ero3y ago
it starts from the beginning
Triufelis
TriufelisOP3y ago
I'm sorry if this is a bad question. But how does this help me? I read the first file regularly and the second one i start from the beginning after every line?
ero
ero3y ago
is that not what you want? what am i missing?
Triufelis
TriufelisOP3y ago
Alright im gonna try explain it again Give me a second So i have two files. I start reading from the first one and print until i find a word from the second file that i havent copied yet. Or so to say and identical word. Once i find the identical word i have to print from the second file until i find the word that was about to be printed from the first file. And then continue this until both files end. I hope i made myself clear
ero
ero3y ago
hm
TheSnowOwl
TheSnowOwl3y ago
@Triufelis make a method or something that reads all lines from a file. then execute it on both your files. the result will be two arrays/lists of strings containing the value of each line. then you do your desired logic with that
Triufelis
TriufelisOP3y ago
Can't read a whole file to memory. Have to read line by line. If the files were thousands of pages, that would be memory inneficient.
TheSnowOwl
TheSnowOwl3y ago
hmm fair point hmm.... I fail to understand the problem here... if you need a line from one file, make it read the next line, and if you need a line from the other file, make it reed that next line or am I missing something
Triufelis
TriufelisOP3y ago
The promlem for me is that i need to find words that match and then switch to print the different lines until i find a word that matches in the other file
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server