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
read a line of both files, one by one, compare them, repeat until your condition is met
the problem is that i might have to print multiple lines of one file before i print the other file
why's that a problem?
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?
just close and re-open the stream?
or whatever
If i close the streamreader and reopen it. Does it start reading from the beggining of the file. Or does it continue?
it starts from the beginning
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?
is that not what you want?
what am i missing?
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
hm
@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
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.
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
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
✅ This post has been marked as answered!