本文目錄一覽:
- 1、C語言:D盤根下有字母文件file1.txt,輸入一字母ch,把其中與ch對應的大小寫對變存入file2.txt中,咋寫?
- 2、c語言寫一個程序,完成文本文件的拷貝,即將當前目錄下已有的file1.txt拷貝一份?
- 3、C語言編程通過文件操作建立文件file1.txt,怎麼弄?
C語言:D盤根下有字母文件file1.txt,輸入一字母ch,把其中與ch對應的大小寫對變存入file2.txt中,咋寫?
此代碼前提是D根目錄下存在file1.txt文件。
代碼文本:
#include “stdio.h”
int main(int argc,char *argv[]){
FILE *p1,*p2;
char ch,t;
p2=fopen(“D:\\file2.txt”,”w+”);
if(p2 (p1=fopen(“D:\\file1.txt”,”r”))){
printf(“Please enter letters to find…\nch=”);
scanf(” %c”,ch);
while((t=fgetc(p1))!=EOF)
fputc(t==ch || t==(ch^0x20) ? t^0x20 : t,p2);
rewind(p1);
rewind(p2);
for(t=0;(ch=fgetc(p1))!=EOF t10;putchar(ch),t++);
putchar(‘\n’);
for(t=0;(ch=fgetc(p2))!=EOF t10;putchar(ch),t++);
fclose(p1);
fclose(p2);
putchar(‘\n’);
}
else
printf(“Open the file(s) failure, exit…\n”);
return 0;
}
c語言寫一個程序,完成文本文件的拷貝,即將當前目錄下已有的file1.txt拷貝一份?
代碼文本:
#include “stdio.h”
int main(int argc,char *argv[]){
FILE *fpi,*fpo;
char ch,fname[30],i;
for(i=0;fname[i]=”file1.txt”[i];i++);
fpi=fopen(fname,”r”);
fname[4]=’2′;
if(!fpi || (fpo=fopen(fname,”w”))==NULL){
printf(“Open the files failure, exit…\n”);
return 0;
}
while((ch=fgetc(fpi))!=EOF)
fputc(ch,fpo);
fclose(fpi);
fclose(fpo);
printf(“Copy success! The file name is ‘%s’\n”,fname);
return 0;
}
C語言編程通過文件操作建立文件file1.txt,怎麼弄?
看你要在哪個磁盤建文件了,如C盤,可以這樣:
FILE *fin;
fin=fopen(“c:\\file1.txt”,”w”);
如果這樣輸入:
FILE *fin;
fin=fopen(“file1.txt”,”w”);
即不指定哪個盤,這樣會將文件建在所編的程序名下。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/298016.html