Created
May 1, 2011 18:02
-
-
Save johncrisostomo/950693 to your computer and use it in GitHub Desktop.
Insert Word (a file handling problem by Marcos Souza)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#something like this, it's a snippet : | |
f = File.open("plaintext.txt", "r+") | |
bytes_per_line = f.readline.length+1 | |
f.seek(bytes_per_line*5, IO::SEEK_SET) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def open_file_and_getlines(filename) | |
lines = [] | |
File.open(filename) do |f| | |
lines = f.readlines | |
end | |
lines | |
end | |
def seek_target_word(lines, word) | |
pos = -1 | |
lines.each_with_index do |line, index| | |
pos = index if line.split(" ").include?(word) | |
end | |
pos | |
end | |
def insert_new_word(lines, word, new_word, pos) | |
target = -1 | |
lines[pos].split(" ").each_with_index do |element, index| | |
target = index if element == word | |
end | |
lines[pos] = lines[pos].split.to_a.insert(target, new_word).join(" ") | |
lines | |
end | |
def replace_file_contents(filename, lines) | |
File.open(filename, "w+") do |f| | |
lines.each do |line| | |
f.puts line | |
end | |
end | |
end | |
print "Enter filename : " | |
fname = gets.chomp! | |
print "Enter target word : " | |
word = gets.chomp! | |
print "Enter word to be inserted : " | |
new_word = gets.chomp! | |
target_position = seek_target_word(open_file_and_getlines(fname), word) | |
if target_position >= 0 | |
modded_lines = insert_new_word(open_file_and_getlines(fname), word, new_word, target_position) | |
replace_file_contents(fname, modded_lines) | |
else | |
puts "Word not found!" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def open_file_and_get_lines(file_name) | |
f = File.open(file_name, "r") | |
a = f.readlines | |
f.close | |
a | |
end | |
def find_line_to_be_replaced(lines) | |
lines.each_with_index do |line, index| | |
tmp = line.split(" ") | |
return index if tmp.include?("word") | |
end | |
puts "Word not found!" | |
exit | |
end | |
def clear_file_and_rewrite(file_name, lines) | |
f = File.open(file_name, "w+") | |
lines.each {|line| f.puts line} | |
f.close | |
end | |
puts "Enter file name : " | |
fname = gets.chomp! | |
lines_arr = open_file_and_get_lines(fname) | |
index = find_line_to_be_replaced(lines_arr) | |
lines_arr[index] = "test test inserted word test test" | |
clear_file_and_rewrite(fname, lines_arr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def open_file(filename) | |
lines_arr = [] | |
File.open(filename) do |f| | |
lines_arr = f.readlines | |
end | |
lines_arr | |
end | |
def insert_and_replace_file(filename, lines) | |
lines[5].insert(10, 'inserted ') | |
File.open(filename, "w+") do |f| | |
lines.each do |line| | |
f.puts line | |
end | |
end | |
end | |
insert_and_replace_file("plaintext.txt", open_file("plaintext.txt")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment