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?
Related
I am trying to make an application in AppleScript to read a list of songs in a text file and I am able to read the file fine until there is an apostrophe (') in the text.
My text file has the content of
Follow Me Down
Money For Nothing
Better When I’m Dancin’
And the code is:
set theFile to "/path/to/file"
set fileHandle to open for access theFile
set songs to the paragraphs of (read fileHandle)
close access fileHandle
display dialog songs
However AppleScript outputs Can’t make {"Follow Me Down", "Money For Nothing", "Better When I‚Äôm Dancin‚Äô"} into type string.. I know there is another error which says it cannot type the array into a string, I don't care about that since I just wanted the output of the array, what I do care about is why does an apostrophe make it output "‚Äô" instead.
If anyone could help me to fix the apostrophe changing to the characters "’", that would be great, also it would be good to know why this happens too.
I made a program which searches for a string, here is code: When we search word in txt file how we could colour in txt file
I want to print at console all text with underline string. How can I do this?
I am using linux
It depends on what your terminal is capable of. Check your terminfo, and see (for instance) http://man7.org/linux/man-pages/man4/console_codes.4.html for some coding variants (that page calls it 'underscored').
For me, on Mac OSX's Terminal, printing the following escape code works:
"\x1b[4m"
-- defined as PP_UNDERLINE, I can use it like this:
printf ("processing file " PP_UNDERLINE "%s" PP_RESET, argv[i]);
to get underlined text in my terminal output:
Something like this will generally work:
printf("Normal \x1b[4mUnderlined\x1b[24m Normal.\n");
I am using a C program which takes file contents as input. I am using input redirection at the command line to read the file. Can I use getchar() to obtain the content of the file? Or should I be reading it into a certain array and then access it?
Yes sure you can use any method to read input from file while running program through command.
You do not need to format it at all. the data in file should appear as if typed on command line while providing inputs. May be this helps.
Please provide more insight if this does not help.Thanks
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.
I am stuck/struggling with a problem I am trying in C(Linux) using API calls(only) to copy multiple input files via command line into one output file. I have searched the Internet for answers but none seem to solve.
My program allows me to specify multiple input files and one output file via the command line. For example:
./archiver file1.txt file2 file3 file4 outputfile
I read these parameters using argc/argv. For some reason when I do ls -l, ./archiver and outputfile have the same number of bytes, thus meaning none of my input files have been copied to my outputfile, just whatever was in memory (when I do cat outputfile it shows a bunch of these )
None of the contents from my input files are in my output files.
Please could you help me as after those bunch of "" I don't know what to do I have tried reading up on malloc() etc. but I don't know how to implement that or if thats even relevant here.
Any help is appreciated, thanks for your time.
file_desc_in = open(argv[i],O_RDONLY,0);
//NEED a loop to copy multiple files in...
while (!eof) {
bytes_read = read(file_desc_in, &buffer, sizeof(buffersize));
if (bytes_read > 1)
bytes_written = write(file_desc_out, &i, bytes_read);
else {
eof=1;
}
I haven't included the errors but I do have them. Thanks for replying immediately.
It'd help to see your code. There's not a lot here to go on, but I'm going to take a wild guess. I suspect you're copying the file specified by argv[0] (your program) and not getting the rest. I don't think I can do any better with what you've given.
You say you are only using API calls. What API are you talking about? The POSIX API? The standard C file I/O API?
If you are just combining input files, you don't really need to write a C program to do it. Since you are running Linux, try using the shell command cat input1 input2 input3 > output.
If you must write a C program to do it, start simple. Before you actually do any file I/O, make sure that you can interpret the input arguments correctly. Have your program simply read in the command-line input and print out something like this:
Input files: file1.txt file2.txt file2.txt
Output files: outputfile.txt
That way, you can verify that your CLI parsing code works correctly before you start worrying about file I/O. It's much easier to debug things one piece at a time.
Your outer loop needs to open each filename, and close it at the end of the loop. You close the output file at the very end, after all the input files are read.
You should also learn the difference between open, read, write and fopen, fread, fwrite.