 
    要写一个文本文件也很简单,只要用output file = 文件名 reset;
语句就可以打开一个文件并从头开始写,这时用print语句输出的结果在显示到屏幕的同时被输出到指定的文件。如果上面的reset改为on,则输出是附加在指定文件末尾。为了关闭屏幕输出,用screen off;语句。再打开用“screen on”语句。也可以暂时关闭文件输出,用:output off;语句。恢复用“output on”语句。对于初学者,输出最好用print语句,简单明了,结果只在gauss系统下的Command Input-Output窗口显示。如使用过多的文本输出语句,不利于自己理解。
注意:在调入数据文件时,其路径一定要符合文件夹所处的地址。
如:output file=gmm.out reset;语句中等号后面没有具体的路径,这是因为我们在菜单栏已经把路径设置到D:\gauss7.0\ex下了。下面的语句load d[172,9]=habit.dat;也是如此。如果菜单栏只是把路径设置到D:\gauss7.0,这时,调用数据以及产出路径就要为load,
d[172,9]=D:\gauss7.0\ex\ habit.dat了,不然gauss系统提示找不到文件。
Gauss data 数据文件的后缀为.dat或.dht文件,打开或输入Gauss数据文件时的语句为,
Open x = data;
For example,
load x[] = dat1.asc;
will load the data in the file dat1.asc into an Nx1 matrix x. This method is preferred because rows(x) can be used to determine how many elements were actually loaded, and the matrix can be reshape’d to the desired form:
load x[] = dat1.asc;
if rows(x) eq 500; 
x = reshape(x,100,5); 
else; 
errorlog “Read Error”; 
end; 
endif;
For quick interactive loading without error checking, use
load x[100,5] = dat1.asc;
This will load the data into a 100x5 matrix. If there are more or fewer than 500 numbers in the data set, the matrix will automatically be reshaped to 100x5.
2、Writing
To write data to an ASCII file, the print or printfm command is used to print to the auxiliary output. The resulting files are standard ASCII files and can be edited with GAUSS’s editor or another text editor.
The output and outwidth commands are used to control the auxiliary output. The print or printfm command is used to control what is sent to the output file.
The window can be turned on and off using screen. When printing a large amount of data to the auxiliary output, the window can be turned off using the command
screen off;
This will make the process much faster, especially if the auxiliary output is a disk file.
It is easy to forget to turn the window on again. Use the end statement to terminate your programs; end will automatically perform screen on and output off.
The following commands can be used to control printing to the auxiliary output:
format Specify format for printing a matrix.
output Open, close, rename auxiliary output file or device.
outwidth Auxiliary output width.
printfm Formatted matrix print.
print Print matrix or string.
screen Turn printing to the window on and off.
This example illustrates printing a matrix to a file:
format /rd 8,2;
outwidth 132;
output file = myfile.asc reset;
screen off;
print x;
output off;
screen on;
The numbers in the matrix x will be printed with a field width of 8 spaces per number, and with 2 places beyond the decimal point. The resulting file will be an ASCII data file. It will have 132 column lines maximum.
A more extended example follows. This program will write the contents of the GAUSS file mydata.dat into an ASCII file called mydata.asc. If there is an existing file by the name of mydata.asc, it will be overwritten:
output file = mydata.asc reset;
screen off;
format /rd 1,8;
open fp = mydata;
do until eof(fp); 
print readr(fp,200);; 
endo;
fp = close(fp);
end;
The output ... reset command will create an auxiliary output file called mydata.asc to receive the output. The window is turned off to speed up the process. The GAUSS data file mydata.dat is opened for reading, and 200 rows will be read per iteration until the end of the file is reached. The data read will be printed to the auxiliary output mydata.asc only, because the window is off.
getf will read a file and return it in a string variable. Any kind of file can be read in this way as long as it will fit into a single string variable.
To read files sequentially, use fopen to open the file and use fgets, fputs, and associated functions to read and write the file. The current position in a file can be determined with ftell. The following example uses these functions to copy an ASCII text file:
proc copy(src, dest);
local fin, fout, str;
fin = fopen(src, “rb”);
if not fin;
retp(1);
endif;
fout = fopen(dest, “wb”);
if not fin;
call close(fin);
retp(2);
endif;
do until eof(fin);
str = fgets(fin, 1024);
if fputs(fout, str) /= 1;
call close(fin);
call close(fout);
retp(3);
endif;
endo;
call close(fin);
call close(fout);
retp(0);
endp;

















 扫码加好友,拉您进群
扫码加好友,拉您进群 
    
 
    

 收藏
收藏





















