顯示具有 檔案 標籤的文章。 顯示所有文章
顯示具有 檔案 標籤的文章。 顯示所有文章

2009年9月12日 星期六

C/C++筆記-檔案內字串搜尋程式碼

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define fn "textfile.txt"

int search(FILE*,char *);
int main(void)
{
  int last=0;
  char buf[80];
  FILE *fp;
  fp = fopen(fn,"r+");
  scanf("%s",buf); //輸入要找的字串
  while(1)
  {
    int line = search(fp,buf);
    if(line)
      printf("%s found in line %d.\n",buf,line+last);
    else
      break;
    last += line; //更新目前計算的行數
  }
  fclose(fp);
  system("pause");
  return 0;
}

int search(FILE *fp,char *s)
{
  int i,j=0;
  char buf[80];
  for(i=1 ; fgets(buf,80,fp) != NULL ; i++)
    if(strstr(buf,s) != NULL) //從buf字串中找s字串
      return i;
  return 0;
}
##ShowAll##

C/C++筆記-fseek()、ftell()、rewind()變更串流內的位置函數

/*
會從指定的origin位置移動offset個位元組
origin定義:
  SEEK_SET,串流開始位置
  SEEK_CUR,串流目前位置
  SEEK_END,串流結束位置
若更改成功則傳回0值
*/
int fseek(FILE *fp, long offset, int origin);

/* 回傳串流目前位置,發生錯誤傳回-1 */
long ftell(FILE *stream);

/* 將串流的目前位置設為該串流的開頭 */
void rewind(FILE *stream);
##ShowAll##

C/C++筆記-fread()、fwrite()輸出入二進制串流

//數值不必轉為字元形式,效率較優勢
fread(void *buffer, size_t size, size_t num, FILE *fp);
/*
代表讀取num個大小為size的位元組,
之後存入buffer陣列(任意型態),
最後回傳成功讀取的資料數,
檢查回傳值是否等於num值則成功
*/

int n=999;
FILE *fp;
//開啟為二進位檔
if((fp = fopen("binfile","wb")) == NULL) exit(1);
//寫入一個整數999到檔案
if(fwrite(&n, sizeof(int), 1, fp) != 1) exit(1);
fclose(fp);
##ShowAll##

C/C++筆記-fprintf()、fscanf()輸出入串流

//與printf()、scanf()函數功能相同
fprintf(FILE *fp, char *control string, arguments...);
fscanf()(FILE *fp, char *control string, arguments...);

C/C++筆記-fgets()、fputs()輸出入字串

char buf[80];
FILE *fp;
if((fp = fopen("textfile","r")) == NULL) exit(1);
while((ch = fgets(buf,80,fp)) != NULL) //一次讀取一行,一行不超過80個字元
  fput(buf,stddout); //將字串輸出到螢幕
fclose(fp);
##ShowAll##

C/C++筆記-fgetc將輸入的字元轉整數

char ch;
int num;
num = fgetc(ch) - '0' ;

2009年9月11日 星期五

C/C++筆記-fgetc()、fputc()輸出入字元

FILE *fp;
if((fp = fopen("textfile","r")) == NULL) exit(1);
while((ch = fgetc(fp)) != EOF) //將字元一個個讀入
  fput(ch,stddout); //將字元一個個輸出到螢幕
fclose(fp);
##ShowAll##

C/C++筆記-顯示檔案內容並計算字數程式碼

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  int i;
  File *fp;
  char f[10],buf[80]; //檔名不超過10個字母,一行不超過80字元
  scanf(%s,f);
  if((fp = fopen(f,"r")) == NULL) exit(1);
  while(fgets(buf,80,fp)) != NULL)
    fputs(buf,stdout); //將檔案一行行輸出
  printf("\n");
  rewind(fp); //將串流fp指回檔案開頭
  for(i=0;;i++)
    if(fscanf(fp,"%s",buf) == EOF)
      break;
  fclose(fp);
  system("pause");
  return 0;
}
##ShowAll##

C/C++筆記-fopen()、fclose()開關檔

//開檔
FILE *fp;
if((fp = fopen("file","r")) == NULL)
{
  printf("file open error!\n");
}

//關檔
fclose(fp);

/*---開檔模式---*/
//[讀、寫、附加]
//"r", "w", "a"

//[讀、寫、附加,二進位模式]
//"rb", "wb", "ab"

//[不存在回傳NULL、存在刪除原檔、不存在建立新檔,皆可讀可寫]
//"r+", "w+", "a+"

//[不存在回傳NULL、存在刪除原檔、不存在建立新檔,皆可讀可寫,二進位模式]
//"rb+", "wb+", "ab+"
##ShowAll##

2009年8月15日 星期六

C/C++筆記-檔案複製程式碼

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  char ch;
  char src[] = "file1.txt";
  char dst[] = "file2.txt";
  File *from,*to;
  if((from = fopen(src,"rb")) == NULL) exit(1);
  if((to = fopen(dst,"wb")) == NULL) exit(1);
  while(fread(&ch,sizeof(ch),1,from) != 0)
    fwrite(&ch,sizeof(ch),1,to);
  fclose(from);
  fclose(to);
  system("pause");
  return 0;
}
##ShowAll##
/