Pascal I need to modify the program as the result to be write in text file maxim.out - pascal

I have a program which is counting the biggest number from 3 numbers. I need to modify the program as the result to be write in text file maxim.out (PASCAL)

You can write the value (assuming it is an integer and it has the name, say, yourValue) with:
var
maximFile: Text;
...
Assign(maximFile, 'maxim.out'); // link the name to the Text variable
Rewrite(maximFile); // open it for writing
Writeln(maximFile, yourValue); // write the value as a line of its own
Close(maximFile); // close the file
You can then read back the value later on with:
Assign(maximFile, 'maxim.out');
Reset(maximFile);
Readln(maximFile, yourValue);
Close(maximFile);
I did not add any error handling (e.g. if the file can't be found, or if it is readonly, or empty, or ...). Depending on settings, that is either done with exceptions or with IOResult values. Read the documentation on how to do that. There should be examples in the docs.

You should read about "file management in pascal". Anyway, declare a variable of type textfile:
var
outputfile : TextFile;
then assignfile() to it your name of choice (maxim.out), rewrite() the file, use writeln() to write into it, and finally closefile() it.
You can find a complete example program here: http://wiki.freepascal.org/File_Handling_In_Pascal

Related

Deleting contents of file after a specific line in ruby

Probably a simple question, but I need to delete the contents of a file after a specific line number? So I wan't to keep the first e.g 5 lines and delete the rest of the contents of a file. I have been searching for a while and can't find a way to do this, I am an iOS developer so Ruby is not a language I am very familiar with.
That is called truncate. The truncate method needs the byte position after which everything gets cut off - and the File.pos method delivers just that:
File.open("test.csv", "r+") do |f|
f.each_line.take(5)
f.truncate( f.pos )
end
The "r+" mode from File.open is read and write, without truncating existing files to zero size, like "w+" would.
The block form of File.open ensures that the file is closed when the block ends.
I'm not aware of any methods to delete from a file so my first thought was to read the file and then write back to it. Something like this:
path = '/path/to/thefile'
start_line = 0
end_line = 4
File.write(path, File.readlines(path)[start_line..end_line].join)
File#readlines reads the file and returns an array of strings, where each element is one line of the file. You can then use the subscript operator with a range for the lines you want
This isn't going to be very memory efficient for large files, so you may want to optimise if that's something you'll be doing.

Saving Variables Permanently

I have a variable called random number that needs to be stored when the application has be shutdown or the computer has been shutdown. Every time this number is used I also need to +1 to it.
I have a few variables in my current vb6 application that need to be saved when the app is closed and loaded when the app is launched. Is this possible? I could use a text file or a config file to store the variables?
EDIT -------------------------------------------------------------------------------
I managed to fix this problem had just using a simple input and output text file. Please read my answer below if you have the same problem and need assistance.
The standard way to save values in VB6 apps was to use INI files. If I remember there are a couple of Win32 functions to read/write them.
They are GetPrivateProfileString and WritePrivateProfileString.
Using the registry is the correct way to do it.
VB has built in functions SaveSetting and GetSetting for writing to and reading from the registry.
See registry tutorial or Stack Overflow question to help you out.
I managed to complete the task by creating a file in my C Drive and putting in the number "123" to the top line of the text file. I then wrote the following code:
Function GetPOIRandomNum()
Dim LineA As String
'Collect stored variables
Open "C:\TestPartner\Config\POIRandomNum.txt" For Input As #1
While Not EOF(1)
Line Input #1, LineA 'Read the first line in the file
POIRandomNum = LineA + 1 'Give POIRandomNum the integer from line 1 and add 1 to it
Wend
Close #1
'Save the new random number variable to the file
Open "C:\TestPartner\Config\POIRandomNum.txt" For Output As #1 'Open for output to replace the old number
Write #1, POIRandomNum 'Input the new number to the text file
Close #1
End Function
Now whenever the Random number Variable is needed I call the above function.

Ruby .count operation truncates input file

I want to read a file in and show how large it is. .count is acting like .count! and changing the size of my input file buffer. so now logfile.each doesn't iterate. What's going on?
logfile = open(input_fspec)
puts "logfile size: #{logfile.count} lines"
count will read all the lines from the input in order to do the counting. If you want to read the lines again (e.g. using readline or each) then you will need to call logfile.rewind to move back to the start of the file.
In fact, what count is actually returning is the number of lines that have not been read yet. For example, if you had already read through the file and called count afterwards then it would return 0.
You could do this instead before you even open it:
File.size("input_fspec")

How to set vb textbox to the contents of a "buffer"?

I am new to VB. I am testing some old VB code. The code is as follows -
Public GlobalCommArea() As Byte
...
...
'GlobalCommArea is set to some value
Now, I want to see the contents of this GlobalCommArea variable. (By the way, is it a variable?)
So I tried
outputBox.Text = GlobalCommArea
But the outputBox (which is a textbox) didn't show anything. What should I do to print the contents of GlobalCommArea into the textbox?
The Byte data type is an array of bytes actually. You need to convert it to a string.
Use this to convert it:
outputBox.Text = StrConv(GlobalCommArea, vbUnicode)
Depending on what's stored in GlobalCommArea you may have to change the vbUnicode parameter.
Hope this helps

Pass data from workspace to a function

I created a GUI and used uiimport to import a dataset into matlab workspace, I would like to pass this imported data to another function in matlab...How do I pass this imported dataset into another function....I tried doing diz...but it couldnt pick diz....it doesnt pick the data on the matlab workspace....any ideas??
[file_input, pathname] = uigetfile( ...
{'*.txt', 'Text (*.txt)'; ...
'*.xls', 'Excel (*.xls)'; ...
'*.*', 'All Files (*.*)'}, ...
'Select files');
uiimport(file_input);
M = dlmread(file_input);
X = freed(M);
I think that you need to assign the result of this statement:
uiimport(file_input);
to a variable, like this
dataset = uiimport(file_input);
and then pass that to your next function:
M = dlmread(dataset);
This is a very basic feature of Matlab, which suggests to me that you would find it valuable to read some of the on-line help and some of the documentation for Matlab. When you've done that you'll probably find neater and quicker ways of doing this.
EDIT: Well, #Tim, if all else fails RTFM. So I did, and my previous answer is incorrect. What you need to pass to dlmread is the name of the file to read. So, you either use uiimport or dlmread to read the file, but not both. Which one you use depends on what you are trying to do and on the format of the input file. So, go RTFM and I'll do the same. If you are still having trouble, update your question and provide details of the contents of the file.
In your script you have three ways to read the file. Choose one on them depending on your file format. But first I would combine file name with the path:
file_input = fullfile(pathname,file_input);
I wouldn't use UIIMPORT in a script, since user can change way to read the data, and variable name depends on file name and user.
With DLMREAD you can only read numerical data from the file. You can also skip some number of rows or columns with
M = dlmread(file_input,'\t',1,1);
skipping the first row and one column on the left.
Or you can define a range in kind of Excel style. See the DLMREAD documentation for more details.
The filename you pass to DLMREAD must be a string. Don't pass a file handle or any data. You will get "Filename must be a string", if it's not a string. Easy.
FREAD reads data from a binary file. See the documentation if you really have to do it.
There are many other functions to read the data from file. If you still have problems, show us an example of your file format, so we can suggest the best way to read it.

Resources