電腦五國語言 程式基本輸入功能: 逐行讀檔
2011/04/13 21:30
瀏覽757
迴響0
推薦0
引用0
基本程式輸入包含File, URL, Socket, Message Bus,Queue, DB Table 等:
Input:
-ReadFile
-GetFileFromURL
-ReadSocket
-ReadMessageBus
-ReadQueue
-ReadDBTable
讀檔細步分解如下:
InputActions:
ReadFile 讀檔
OpenFile 開檔
ReadWholeFile 讀全檔
或
ReadLineByLine 逐行讀
LoopNOTEOF 非檔案結束時迴圈
ReadALine 讀一行
EndLoop 結束迴圈
CloseFile 關檔
下面是各種語言(VB5,C,JAVA,PHP,C#.NET)之逐行讀檔範例
整個寫法是以VB6為基準 其他語言也做相對的片段對應方式編寫
並不是該語言的標準寫法
這點需要特別注意
VB6:
' *******************************************
' File Functions
' *******************************************
Function EndOfFile() as Boolean
EndOfFile=EOF(1)
End Function
Function ReadHandle() as Boolean
ReadHandle=Not EOF(1)
End Function
Sub FileFunction()
Dim i as Integer
Dim ss as String
' Read File
Open "C:\\test.txt" For Input As #1
' check open file result
If ReadHandle() Then
While (NOT EndOfFile())
Line Input #1, ss
Debug.Print " ";ss;
Debug.Print
WEnd
Close #1
End If
End Sub
C:
/* ******************************************* */
/* File Functions */
/* ******************************************* */
FILE *finput;
FILE *foutput;
FILE *frandom;
int fhinput;
int fhoutput;
bool EndOfFile() {
return feof(finput);
}
bool ReadHandle() {
if (finput!=0)
return 1;
else
return 0;
}
void FileFunction() {
int i;
char ss[100];
/* Read File */
finput=fopen("C:\\test.txt", "r+b");
/* check open file result */
if (ReadHandle()) {
while ( ! EndOfFile()) {
fscanf(finput, "%[^\n]", ss);
fgetc(finput); / skip crlf
printf(" %s" , ss);
printf("\n" );
}
fclose(finput);
}
vfree((char*)ss);
}
JAVA:
// *******************************************
// File Functions
// *******************************************
BufferedReader input = null;
BufferedWriter output = null;
FileInputStream in = null;
FileOutputStream out = null;
File randomfile = null;
String line="";
char [] charbuf = null;
public boolean EndOfFile() {
if (line==null) {
return true;
} else {
return false;
}
}
public boolean ReadHandle() {
if (input!=null) {
return true;
} else {
return false;
}
}
public void FileFunction() {
int i;
String ss = null;
// Read File
try {
input = new BufferedReader( new FileReader(new File("C:\\test.txt")) );
line = "";
} catch(Exception e) { input = null; System.err.println(e.getMessage()); }
// check open file result
if (ReadHandle()) {
while ( ! EndOfFile()) {
try {
line = input.readLine();
ss = line;
} catch(Exception e) { System.err.println(e.getMessage());
}
System.out.print(" "+ss );
System.out.println("");
}
try {
if (input!=null) { input.close(); input=null; System.gc(); };
} catch(Exception e) { System.err.println(e.getMessage()); }
}
}
PHP:
/* ******************************************* */
/* File Functions */
/* ******************************************* */
function EndOfFile() {
global $hinput;
return feof($hinput);
}
function ReadHandle() {
global $hinput;
if ($hinput==null)
return false;
else
return true;
}
function FileFunction() {
/* Read File */
$hinput = fopen("C:\\test.txt", "r+b");
/* check open file result */
if (ReadHandle()) {
while ( ! EndOfFile()) {
$ss = fgets($hinput);
echo " ".$ss;
echo "";
}
fclose($hinput);
}
}
C#.NET:
/* ******************************************* */
/* File Functions */
/* ******************************************* */
StreamWriter sw;
StreamReader sr;
FileStream fsw;
FileStream fsr;
bool EndOfFile() {
return sr.EndOfStream;
}
bool ReadHandle() {
if (sr!=null) {
return true;
}
else
{
return false;
}
}
public void FileFunction() {
int i;
String ss = null;
/* Read File */
try { sr = new StreamReader("C:\\test.txt", Encoding.GetEncoding("big5"));}
catch { sr = null; }
/* check open file result */
if (ReadHandle()) {
while ( ! EndOfFile()) {
ss = sr.ReadLine();
Console.Write(" "+ss);
Console.WriteLine("");
}
sr.Close();
sr = null;
}
}
Input:
-ReadFile
-GetFileFromURL
-ReadSocket
-ReadMessageBus
-ReadQueue
-ReadDBTable
讀檔細步分解如下:
InputActions:
ReadFile 讀檔
OpenFile 開檔
ReadWholeFile 讀全檔
或
ReadLineByLine 逐行讀
LoopNOTEOF 非檔案結束時迴圈
ReadALine 讀一行
EndLoop 結束迴圈
CloseFile 關檔
下面是各種語言(VB5,C,JAVA,PHP,C#.NET)之逐行讀檔範例
整個寫法是以VB6為基準 其他語言也做相對的片段對應方式編寫
並不是該語言的標準寫法
這點需要特別注意
VB6:
' *******************************************
' File Functions
' *******************************************
Function EndOfFile() as Boolean
EndOfFile=EOF(1)
End Function
Function ReadHandle() as Boolean
ReadHandle=Not EOF(1)
End Function
Sub FileFunction()
Dim i as Integer
Dim ss as String
' Read File
Open "C:\\test.txt" For Input As #1
' check open file result
If ReadHandle() Then
While (NOT EndOfFile())
Line Input #1, ss
Debug.Print " ";ss;
Debug.Print
WEnd
Close #1
End If
End Sub
C:
/* ******************************************* */
/* File Functions */
/* ******************************************* */
FILE *finput;
FILE *foutput;
FILE *frandom;
int fhinput;
int fhoutput;
bool EndOfFile() {
return feof(finput);
}
bool ReadHandle() {
if (finput!=0)
return 1;
else
return 0;
}
void FileFunction() {
int i;
char ss[100];
/* Read File */
finput=fopen("C:\\test.txt", "r+b");
/* check open file result */
if (ReadHandle()) {
while ( ! EndOfFile()) {
fscanf(finput, "%[^\n]", ss);
fgetc(finput); / skip crlf
printf(" %s" , ss);
printf("\n" );
}
fclose(finput);
}
vfree((char*)ss);
}
JAVA:
// *******************************************
// File Functions
// *******************************************
BufferedReader input = null;
BufferedWriter output = null;
FileInputStream in = null;
FileOutputStream out = null;
File randomfile = null;
String line="";
char [] charbuf = null;
public boolean EndOfFile() {
if (line==null) {
return true;
} else {
return false;
}
}
public boolean ReadHandle() {
if (input!=null) {
return true;
} else {
return false;
}
}
public void FileFunction() {
int i;
String ss = null;
// Read File
try {
input = new BufferedReader( new FileReader(new File("C:\\test.txt")) );
line = "";
} catch(Exception e) { input = null; System.err.println(e.getMessage()); }
// check open file result
if (ReadHandle()) {
while ( ! EndOfFile()) {
try {
line = input.readLine();
ss = line;
} catch(Exception e) { System.err.println(e.getMessage());
}
System.out.print(" "+ss );
System.out.println("");
}
try {
if (input!=null) { input.close(); input=null; System.gc(); };
} catch(Exception e) { System.err.println(e.getMessage()); }
}
}
PHP:
/* ******************************************* */
/* File Functions */
/* ******************************************* */
function EndOfFile() {
global $hinput;
return feof($hinput);
}
function ReadHandle() {
global $hinput;
if ($hinput==null)
return false;
else
return true;
}
function FileFunction() {
/* Read File */
$hinput = fopen("C:\\test.txt", "r+b");
/* check open file result */
if (ReadHandle()) {
while ( ! EndOfFile()) {
$ss = fgets($hinput);
echo " ".$ss;
echo "";
}
fclose($hinput);
}
}
C#.NET:
/* ******************************************* */
/* File Functions */
/* ******************************************* */
StreamWriter sw;
StreamReader sr;
FileStream fsw;
FileStream fsr;
bool EndOfFile() {
return sr.EndOfStream;
}
bool ReadHandle() {
if (sr!=null) {
return true;
}
else
{
return false;
}
}
public void FileFunction() {
int i;
String ss = null;
/* Read File */
try { sr = new StreamReader("C:\\test.txt", Encoding.GetEncoding("big5"));}
catch { sr = null; }
/* check open file result */
if (ReadHandle()) {
while ( ! EndOfFile()) {
ss = sr.ReadLine();
Console.Write(" "+ss);
Console.WriteLine("");
}
sr.Close();
sr = null;
}
}
你可能會有興趣的文章:
限會員,要發表迴響,請先登入










