Suppose I wrote a letter in notepad and I saved it as letter.txt. Then I realized that I forgot to say one matter in that letter. So I opened letter.txt using any text editor such as Notepad, Wordpad or something. Now I inserted the letters which I want to say in this letter at the middle of the file. How does it work?
Example:
Here is a message:
" Hi, How are you. Today i want to meet you. Thank you.
It is actually stored in memory like this:
"Hi,\nHow\0are\0you.\0TodayToday\0i\0want\0to\nmeet\0you.\nThank\0you.
Now I want to add I am fine after How are you. How does it work? How is it added in the middle of the file? how are the other words not overwritten? What is the process behind it?
For small files, text editors just read the whole file in memory. When you modify the text, the text editor modifies the in-memory version. Then when you save, the text editor overwrites the original file with the new contents -- so the whole file is overwritten, and the text is written to the file as-is without any references or other tricks.
Related
I'm using text files as a database for saving users' information for a game which i made using swi-prolog. The information is saved like this:user(Name,Password,Age,Points). What i want to do is to change a user's Points without having to rewrite the entire db. In other words, I am looking for something that will work like retractall(user(Name,_,_,_)), but with the text file. I know how to find the specific user using read/2, and how to assert a new fact using write/2, but i don't know how to delete one specific line in the text file.
Thank you for helping.
Take a look at SWI-Prolog's library(persistency). It removes a fact by adding a line that the fact is removed. If the file gets too big with add/remove lines, it provides db_sync/1 to write a clean file. OS file system operations do not allow to remove part of a file (except from truncating the end). The normal way to do this is to write a new file and, if successful, rename this to the existing one, so nothing is lost if you crash while writing the new file.
SITUATION:
My instructor for my micro-controller class refuses to save sample code to a text file and instead saves it to a word document file instead. When I open up the doc file and copy/paste the code into my IDE "CodeWarrior" it causes errors upon compile time.
I am having to rewrite all the code into a text editor and then copy/paste it into my IDE.
MY UNDERSTANDING:
I was told to always save code as a text file because when you save code as a word document file it will bring in unwanted characters when your copy/pasting the code into your IDE for compiling.
MY QUESTIONS TO YOU:
1.)
Can someone explain this dilemma to me so I can understand it better? I would like to present a better case next time when I receive errors and to also know more about what is happening.
2.)
Is it possible to write a script that will show me all the characters that are being copied and pasted into a file when the code is coming from a word document vs. a text file? In otherwords is there a program that will allow me to see what is going on between copying/pasting code from a word doc file versus a txt file?
Saving source code as a Word document is just silly. If your instructor is insisting on this, chances are no matter how well-reasoned and thorough your argument, they're not going to listen. They're beyond help.
However, to answer your questions: 1) It depends on what you're pasting the thing into. Programs that copy onto the clipboard usually make the data available in several different formats, ranging from their own internal format to plain ASCII text, to maximize compatibility so that the data can be pasted into pretty much any target program. Most text editors will only accept the plan-text version, in which case no extra characters should be transferred. However if your text editor supports RTF or HTML, this may not be true. I'm not sure what CodeWarrior supports but it is certainly possible.
A workaround if this is the case: First paste into a PURE text editor like Notepad. Then copy from Notepad into CodeWarrior. This should eliminate any hidden formatting. As shoover said above, make sure double-quotes " are really double-quotes and not the fancy left- and right-specific quotes that Word sometimes uses.
Use a hex editor like XVI32 to see the raw contents of the file, including nonprinting characters. Or use a text editor with support for showing nonprinting characters (vi/vim, etc.).
I'm studying C and I've just had the same problem. When coping a piece of code from a PDF file and trying to compiling it, gcc would return a serie of errors. Reading the answer above I had an idea: "What if I converted the utf8 into ascii?". Well, I found a website that does just that (https://onlineutf8tools.com/convert-utf8-to-ascii). But instead of also converting the utf8 characters into ascii, it showed them as hexadecimals (Copying from the website to the text editor you can see it better). From there i realised that the problem were mostly the quote marks "".
I then copied the ascii "translation" into my code editor (I must add that it worked fine with Sublime, while VScode read the same utf8 code as it was in the original file, even after cp from the website) and replaced all the hex with the actual ascii characters that were needed to compile the code properly. I used the function find and replace from my editor to do it. I must say that it wasn't very fast doing it. But I believe that in some cases, if the code you're trying to copying is too long, doing it the way I've just described could be faster than rewriting the entire code.
I'm working on a programming assignment and I was wondering if somebody could help me out with this issue. This assignment says to write a program in Prolog which takes the text from an input text file and write it to an output text file. In order to get the location of the text files, the user needs to be prompted to write the path of the text files.
I have figured out how to do it, but I have one small issue that is really annoying. Here is my code:
main:-
%Ask the user for the input text file and then open the file
write('Please enter the filename you would like to read from:'),
nl,
read(X),
open(X,read,In),
%Ask the user for the output text file and then open the file
write('Please enter the filename you would to write to:'),
nl,
read(Y),
open(Y,write,Out),
%Read in characters from the input text file and then put them
%on the output text file.
tell(Out),
repeat,
get_char(In,T),
write(T),
T == end_of_file, !,
close(In),
told,
close(Out).
Let's say the text file that is going to be read says "this is a test". My issue is if I use the program to save this text and write it to another text file, it will write "this is a testend_of_file" instead.
I realize that this is happening because the loop isn't being terminated at the right time, but I'm not sure how to go about fixing the loop so "end_of_file" doesn't get accidentally written to the text file as well. Any help would be much appreciated. I feel like I've tried everything.
You first do write(T), and after that your testing for T == end_of_file, so no surprise end_of_file will be written.
Try ( T == end_of_file -> ! ; write(T), fail ),
What Prolog system are you using, BTW?
I wonder how can I update an existing file, and add a word in a given position.
so let say my file looks like:
this the first line in the file
and I want to add the word "is " in position 6 so the file will look like:
this is the first line in the file
what is the best method to achieve that?
what should be the fopen mode?
assume my file is to big to copy to memory, or create a temporary clone
thanks!
There is no magic "insert in the middle" open mode. You have to do that yourself.
If it can't fit in memory, and you don't want/can't create a temporary, you can rewrite it "from the bottom". (I.e. read the last "block", write it back shifted by the amount you want, repeat.)
Unfortunately, it is not possible to simply update a file this way. If it is a flat file, you will have to move the parts yourself.
I am reading info (numbers) from a txt file and after that I am adding to those numbers, others I had in another file, with the same structure.
At the start of each line in the file is a number, that identifies a specific product. That code will allow me to search for the same product in the other file. In my program I have to add the other "variables" from one file to the other, and then replace it, in the same place in one of those files.
I didn't open any of those files with a or a+, I did it with r and r+ because i want to replace the information in the lines that may be in the middle of the file, and not in the end of it.
The program compiles, and runs, but when it comes to replace the info in the file, it just doesn't do anything.
How should I resolve the problem?
A program can replace (overwrite) text in the middle of the file. But the question is whether or not this should be performed.
In order to insert larger text or smaller text (and close up the gap), a new text file must be written. This is assuming the file is not fixed width. The fundamental rule is to copy all original text before the insertion to a new file. Write the new text. Finally write the remaining original text. This is a lot of work and will slow down even the simplest programs.
I suggest you design your data layout before you go any further. Also consider using a database, see my post: At what point is it worth using a database?
Your objective is to design the data to minimize duplication and data fetching.