生成手機號碼文本c++程序源碼
// makeyqm.cpp : 定義控制臺應用程序的入口點。
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
int i=1;
int _tmain(int argc, _TCHAR* argv[])
// 主程序_tmain支持unicode別名,定義一個整形argc記錄參數(shù)個數(shù)
//_TCHAR* argv[] 則是所有參數(shù)的一個數(shù)組
{
if(argc!=3) {
printf("USAGE: MAKEYQM phonefile yqmfile");
return 1;
} //如果發(fā)現(xiàn)參數(shù)不等于3個就提示USAGE: MAKEYQM phonefile yqmfile
FILE *fp1,*fp2,*fp3;
if( (fp1=fopen(argv[1], "r")) == NULL) {
printf("can't open file: %s", argv[1]);
return 1;
} //如果讀取第一個參數(shù)為空,就打印不能打開該文件
if( (fp2=fopen(argv[2], "r")) == NULL ) {
printf("can't open file: %s", argv[2]);
return 1;
}//如果讀取第二個參數(shù)為空,就打印不能打開該文件
char phone[255], yqm[255];
//定義兩個數(shù)組phone,yqm
while( fgets(phone, 255, fp1) && fgets(yqm, 255, fp2)) {
//當從fp1和fp2流中讀取255個字符串數(shù)據(jù)長度時
phone[strlen(phone)-2]=0;
//把0賦值給phone的長度-2
strcat(phone,".txt");
//把txt的內(nèi)容追加給到phone結尾處(覆蓋dest結尾處的'\0')并添加'\0'.
if( (fp3=fopen(phone,"a")) == NULL ) {
printf("can't open file: %s", phone);
return 1;
} //如果追加打開或創(chuàng)建文本為空
fputs(yqm,fp3); //把fp3的字節(jié)流送到y(tǒng)qm參數(shù)中
fclose(fp3); //關閉fp3參數(shù)內(nèi)容
printf("%d - Write %s ... OK\n", i, phone);
i++;
} //遞增輸出phone的字符內(nèi)容
fclose(fp2); //關閉fp2參數(shù)內(nèi)容
fclose(fp1); //關閉fp1參數(shù)內(nèi)容
printf("Complete!"); //打印完成
return 0; //結束參數(shù)
}
文章中涉及到的一些參考
fputs
函數(shù)名: fputs
功 能: 送一個字符串到一個流中
用 法: int fputs(char *string, FILE *stream);
程序例:
#include
int main(void)
{
/* write a string to standard output */
fputs("Hello world\n", stdout);
return 0;
}
fp=fopen(filename,"a");
fseek(fp,0,SEEK_SET);//我以為此時光標位置是文件開頭,結果發(fā)現(xiàn)光標位置是文件的末尾,也就是說“a”只能以追加方式寫,只能定位到原文件末尾,而不能是原文件中間的某個位置
“r” 打開文本文件用于讀
“w” 創(chuàng)建文本文件用于寫,并刪除已存在的內(nèi)容(如果有的話)
“a” 追加,打開或創(chuàng)建文本文件,并向文件末尾追加內(nèi)容
“r+” 打開文件用于更新(即讀和寫)
“w+” 創(chuàng)建文本文件用于更新,并刪除已存在的內(nèi)容(如果有的話)
“a+” 追加:打開或創(chuàng)建文本文件用于更新,寫文件是追加到文件末尾。
strcat 原型:extern char *strcat(char *dest,char *src);
用法:#include
功能:把src所指字符串添加到dest結尾處(覆蓋dest結尾處的'\0')并添加'\0'。
說明:src和dest所指內(nèi)存區(qū)域不可以重疊且dest必須有足夠的空間來容納src的字符串。
返回指向dest的指針。
舉例:
// strcat.c
#include
#include
main()
{
char d[20]="Golden Global";
char *s=" View";
clrscr();
strcat(d,s);
printf("%s",d);
getchar();
return 0;
}
程序執(zhí)行結果為:
Golden Global View