I hope this helps you . Why not count the number of characters read while reading back to the fifth newline (call that n) and then do a read of n characters? You don't need to store the data, it's already stored in the file.
C++- Code not working to alternate lines when writing on a text file
wish of those help The code looks correct and should work under normal circumstances. This might be a problem with the encoding of the second file, where the newline characters are not being recognised as such on your platform, which could result in the entire second file being interpreted as a single line by the C++ standard library. Windows (CR+LF), Unix/Linux (LF), and Mac (CR) each have different conventions for newlines. Search about the carriage return and line feed characters across platforms to learn more about this topic.
how to write alternate lines in text file to another text file in python?
like below fixes the issue First of all,for safety reasons, you must open your file using the with keyword instead. Even you're not working on a a more important project with your actual code, it is good to have such good programming habits. I propose you a simple approach to resolve your problem:
# Safely open your file
with open('file.txt', 'r') as f:
lines = f.readlines()
# Rearrange the lines according to the logic you are looking for
even_lines, odd_lines = lines[::2], lines[1::2]
# Safely open your files
with open('file1.txt','w') as f1:
for odd_line in odd_lines:
with open('file2.txt','w') as f2:
for even_line in even_lines:
secuirty@begueradj:~/Desktop$ python tester.py
security@begueradj:~/Desktop$ cat file1.txt
This is the line number 1
This is the line number 3
This is the line number 5
security@begueradj:~/Desktop$ cat file2.txt
This is the line number 0
This is the line number 2
This is the line number 4
How to Start and Stop printing lines with Specific Text in Linux/Unix Terminal
I wish this help you A simple way in awk is just keep a flag and set n = 1 when "Start" is encountered in field 4. Checking n == 1 gives you a test to print each line while n is 1. When "End" is reached, simply print that line and exit, e.g.