Ⅰ 怎麼用c語言生成一個特定大小的文本文件
#include <stdio.h>
void CreateMyFile(char * szFileName,int nFileLength)
{
FILE* fp = fopen(szFileName, "wb+"); // 創建文件
if(fp==NULL)
printf("文件打開失敗");
else
{
fseek(fp, nFileLength-1, SEEK_SET); // 將文件的指針 移至 指定大小的位置
fputc(32, fp); // 在要指定大小文件的末尾隨便放一個數據
fclose(fp);
}
}
void main()
{
CreateMyFile("test.txt",1024*256); //調用測試
}
Ⅱ C語言將控制台命令輸出到文本文件
1)啟動一個command窗口
2)輸入命令,如下圖
在命令行上輸入 d:myText.exe >> r.txt
那樣,程序輸出將會被保存當前目錄下的 r.txt中了
Ⅲ 怎麼用C語言實現 輸入一個串字元存到一個文本文檔中
1.通過fopen打開文件,fputs像文件寫入數據,fclose關閉文件。
#include <stdio.h>
int main()
{
FILE *pf = fopen("F:/1.txt", "w+"); // 以寫、創建形式打開文件
if (!pf)
return -1;
fputs("123abc456-1452=!@#$", pf); // 像文件寫入字元串
fclose(pf); // 關閉文件
printf("ok");
return 0;
}
2.FILE *fopen( const char *fname, const char *mode );
fopen()函數打開由fname(文件名)指定的文件, 並返回一個關聯該文件的流.如果發生錯誤, fopen()返回NULL. mode(方式)是用於決定文件的用途(例如 用於輸入,輸出,等等)
Mode(方式) 意義
"r" 打開一個用於讀取的文本文件
"w" 創建一個用於寫入的文本文件
"a" 附加到一個文本文件
"rb" 打開一個用於讀取的二進制文件
"wb" 創建一個用於寫入的二進制文件
"ab" 附加到一個二進制文件
"r+" 打開一個用於讀/寫的文本文件
"w+" 創建一個用於讀/寫的文本文件
"a+" 打開一個用於讀/寫的文本文件
"rb+" 打開一個用於讀/寫的二進制文件
"wb+" 創建一個用於讀/寫的二進制文件
"ab+" 打開一個用於讀/寫的二進制文件
3.int fputs( const char *str, FILE *stream );fputs()函數把str(字元串)指向的字元寫到給出的輸出流. 成功時返回非負值, 失敗時返回EOF.
4.int fclose( FILE *stream );
函數fclose()關閉給出的文件流, 釋放已關聯到流的所有緩沖區. fclose()執行成功時返回0,否則返回EOF.
Ⅳ C語言如何將字元串寫入文本文件
從鍵盤輸入一行字元,寫入一個文件, 再把該文件內容讀出顯示在屏幕上。
#include<stdio.h>
main()
{
FILE *fp;
char ch;
if((fp=fopen("string","wt+"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
printf("input a string:\n");
ch=getchar();
while (ch!='\n')
{
fputc(ch,fp);
ch=getchar();
}
rewind(fp);
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
printf("\n");
fclose(fp);
}
Ⅳ 在c語言編程中如何實現程序對文本文件中字元串進行替換及生成新的文本文件
我以前剛學C++的時候寫過一個相似的程序,如果你要的是純C語言下的編程,那麼你就參考一下,這個演算法的原理是一樣的,即讀入一個字元就顯示出來。當然你也可以考慮其他實現方式。這個C++的程序中,和C語言區別的主要是有些輸入和輸出不太一樣。還有system("pause")這個是調用系統暫停功能,可能在TC等編譯環境下不能使用,可以考慮使用getch()替換。至於system("cls")是清屏。
相關功能函數為Display_text(),
#include<iostream>
#include<fstream>
#include <iomanip>
#include<windows.h>
using namespace std;
#define MaxSize 65535
int tag[100]; //匹配關鍵字的字元下標,設定最多找到100個關鍵字
typedef struct
{
char data[MaxSize]; //記錄字元值
int len; //保存有效字元串長度
}SqString;
void MainMenu(); //顯示主菜單
void Select_function(char op); //功能選擇
void Display_text(); //顯示本文內容
void Count_ch(); //統計字元數,空格數,行數
void Search_KeyWord(); //檢索關鍵字
void Replace_KeyWord(); //替換關鍵字
void index(SqString s,SqString t); //簡單匹配演算法(BF)
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor); //顏色函數
int main()
{
MainMenu();
return 0;
}
void MainMenu() //顯示主菜單
{
char op;
cout<<"I——打開文本文件\n";
cout<<"T——統計\n";
cout<<"S——檢索\n";
cout<<"R——替換\n";
cout<<"Q——退出\n\n";
cout<<"請選擇:";
cin>>op;
Select_function(op);
}
void Select_function(char op) //功能選擇
{
switch(op)
{
case 'i':
case 'I':Display_text();break;
case 't':
case 'T':Count_ch();break;
case 's':
case 'S':Search_KeyWord();break;
case 'r':
case 'R':Replace_KeyWord();break;
case 'q':
case 'Q':exit(0);
default:cout<<"輸入錯誤,請重新選擇"<<endl;
system("pause");system("cls");
MainMenu();
break;
}
}
void Display_text() //顯示本文內容
{
int i=0;
char c,ch[150];
cout<<"請輸入文件名:"<<endl;
cin>>ch;
system("cls");
ifstream infile;
infile.open(ch,ios::in);
if(!infile)
{
cerr<<"Open file error!"<<endl;
system("pause");system("cls");
MainMenu();
}
while(infile.get(c))
{
cout<<c;
i++;
if(i>=1000&&c=='\n'||i>=1500)
{
cout<<endl<<endl;
system("pause");system("cls");
i=0;
}
}
infile.close();
SetColor(11,8);
cout<<endl<<"文本內容結束!"<<endl;
SetColor(7,0);
system("pause");system("cls");
MainMenu();
}
void Count_ch() //統計字元數,空格數,段落數
{
int i=0,j=0,k=0;
char c,ch[150];
cout<<"請輸入文件名:"<<endl;
cin>>ch;
system("cls");
ifstream infile;
infile.open(ch,ios::in);
if(!infile)
{
cerr<<"Open file error!"<<endl;
system("pause");system("cls");
MainMenu();
}
while(infile.get(c))
{
i++;
if(c==' ')j++;
if(c=='\n')k++;
}
infile.close();
SetColor(11,8);
cout<<"字元數:"<<i<<endl;
cout<<"空字元數"<<j<<endl;
cout<<"段落數(回車次數)"<<k<<endl;
SetColor(7,0);
system("pause");system("cls");
MainMenu();
}
void Search_KeyWord() //檢索關鍵字
{
int i=0,j=0,k=0;
char c,ch[150],kw[50];
SqString s,t;
cout<<"請輸入文件名:"<<endl;
cin>>ch;
system("cls");
ifstream infile;
infile.open(ch,ios::in);
if(!infile)
{
cerr<<"Open file error!"<<endl;
system("pause");system("cls");
MainMenu();
}
cout<<"請輸入關鍵字:";
cin>>kw;
cout<<endl;
while(infile.get(c)&&i<MaxSize)
{
s.data[i]=c;i++;
}
s.len=i;
infile.close();
for(i=0;kw[i]!='\0';i++)
t.data[i]=kw[i];
t.len=i;
index(s,t);
if(tag[0]==-1)cout<<"無此關鍵字"<<endl;
else
{
for(i=0;i<=s.len;i++)
{
if(i==tag[j])
{
for(;i<tag[j]+t.len;i++)
{
SetColor(10,8);
cout<<s.data[i];
SetColor(7,0);
}
j++;
}
else cout<<s.data[i];
k++;
if(k>=1500&&s.data[i]=='\n'||k>=2000)
{
cout<<endl<<endl;
system("pause");system("cls");
k=0;
}
}
SetColor(11,8);
cout<<endl<<endl<<"文本內容結束!"<<endl;
SetColor(7,0);
}
system("pause");system("cls");
MainMenu();
}
void Replace_KeyWord() //替換關鍵字
{
int i=0,j=0,k=0;
char c,ch[150],kw[50],nkw[50];
SqString s,t,nt;
cout<<"請輸入文件名:"<<endl;
cin>>ch;
system("cls");
ifstream infile;
infile.open(ch,ios::in);
if(!infile)
{
cerr<<"Open file error!"<<endl;
system("pause");system("cls");
MainMenu();
}
cout<<"請輸入關鍵字:";
cin>>kw;
cout<<endl;
while(infile.get(c)&&i<MaxSize)
{
s.data[i]=c;i++;
}
s.len=i;
infile.close();
for(i=0;kw[i]!='\0';i++)
t.data[i]=kw[i];
t.len=i;
index(s,t);
if(tag[0]==-1)cout<<"無此關鍵字"<<endl;
else
{
cout<<"請輸入新的字元替代原關鍵字:"<<endl;
cin>>nkw;
for(i=0;nkw[i]!='\0';i++)
nt.data[i]=nkw[i];
nt.len=i;
for(i=0;i<=s.len;i++)
{
if(i==tag[j])
{
for(int n=0;i<tag[j]+nt.len;i++,n++)
{
s.data[i]=nt.data[n];
SetColor(10,8);
cout<<s.data[i];
SetColor(7,0);
}
j++;
}
else cout<<s.data[i];
k++;
if(k>=1500&&s.data[i]=='\n'||k>=2000)
{
cout<<endl<<endl;
system("pause");system("cls");
k=0;
}
}
SetColor(11,8);
cout<<endl<<endl<<"文本內容結束!"<<endl;
SetColor(7,0);
}
fstream outfile(ch,ios::out);
if(!outfile)
{
cerr<<"Open file error!"<<endl;
system("pause");system("cls");
MainMenu();
}
for(i=0;i<=s.len;i++)
{
outfile<<s.data[i];
}
outfile.close();
system("pause");system("cls");
MainMenu();
}
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor) //顏色函數
{
HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));
}
void index(SqString s,SqString t) //簡單匹配演算法(BF)
{
int i=0,j=0,k=0;
h0: while(i<s.len&&j<t.len)
{
if(s.data[i]==t.data[j])
{
i++;j++;
}
else
{
i=i-j+1;j=0;
}
}
while(j>=t.len)
{
tag[k]=i-t.len;
k++;
i++;j=0;
goto h0;
}
if(k==0)tag[0]=-1;
}
Ⅵ C語言編寫好代碼後,怎麼編譯,最後生成可執行文件
材料/工具:vc6.0
1、打開c語言編輯工具,在工具中寫入程序的源代碼。
Ⅶ 用C語言如何將結果輸出到一個文本文件中保存
文件的操作步驟:
#include <stdio.h> #include <stdlib.h> int main()
{
FILE *fp;
int i, d;
fp=fopen("data.txt","w");
if(fp==NULL)
{
printf("File cannot open! " );
exit(0);
}
for(i=0; i<10; i++)
{
scanf("%d", &d);
fprintf(fp,"%d ", d);
}
fclose(fp);
return 0;
}
格式化輸出:
#include <stdio.h> #include <stdlib.h> int main()
{
FILE *fp;
int i, No;
float salary;
fp=fopen("data.csv","w");
if(fp==NULL)
{
printf("File cannot open! " );
exit(0);
}
//輸入5名員工的工號,並保存到文件中
for(i=0; i<5; i++)
{
scanf("%d %f", &No, &salary);
fprintf(fp,"%d, %.2f ", No, salary);
}
fclose(fp);
return 0;
}
(7)用c語言將輸入的報文編譯成文本擴展閱讀:
從文件中讀取字元:
#include <stdio.h> #include <stdlib.h> int main()
{
FILE *fp;
char c;
if ((fp=fopen( "data.dat" , "r" ))==NULL)
{
printf("File cannot open!");
exit(0);
}
while((c=fgetc(fp))!=EOF)
putchar(c);
fclose(fp);
return 0;
}
Ⅷ 如何用C語言把一段文字輸出到txt中
可能用到的函數:
函數名:
fscanf
功
能:
從一個流中執行格式化輸入,fscanf遇到空格和換行時結束,注意空格時也結束。這與fgets有區別,fgets遇到空格不結束。
用法:
int
fscanf(file
*stream,
char
*format,[argument...]);
file
*stream:文件指針;
char
*format:格式字元串;
[argument...]:輸入列表。
例如:
file
*fp;
char
a[10];
int
b;
double
c;
fscanf(fp,"%s%d%lf",a,&b,&c)
返回值:整型,成功讀入的參數的個數
函數名:fprintf
傳送格式化輸出到一個文件中,
可用於列印機輸出。
用
法
#include
int
fprintf(
file
*stream,
const
char
*format,
...
);
fprintf()函數根據指定的format(格式)發送信息(參數)到由stream(流)指定的文件.
fprintf()只能和printf()一樣工作.
fprintf()的返回值是輸出的字元數,發生錯誤時返回一個負值.
返回值
若成功則返回輸出字元數,若輸出出錯則返回負值。
示例
#include
int
main(void)
{
file
*in,
*out;
if
((in
=
fopen("\\autoexec.bat",
"rt"))
==
null)
{
fprintf(stderr,
"cannot
open
input
file.\n");
return
1;
}
if
((out
=
fopen("\\autoexec.bat",
"wt"))
==
null)
{
fprintf(stderr,
"cannot
open
output
file.\n");
return
1;
}
while
(!feof(in))
fputc(fgetc(in),
out);
fclose(in);
fclose(out);
return
0;
}
Ⅸ 如何將在c語言中生成的數據保存到文本文件中
主要通過fprintf格式化輸出函數實現,主要代碼如下,
//程序功能,將10 12.345000 testinfo 寫入test.txt文件
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *pf=NULL;
int m=10;
float f=12.345;
char str[20]="testinfo";
pf=fopen("test.txt", "w" );//假設test.txt文件為空
if(!pf)
{
printf("打開文件失敗,程序退出!");
exit(1);
}
fprintf(pf,"%d %f %s\n",m,f,str);//寫入,test.txt文件內容為10 12.345000 testinfo
if(pf)//關閉文件
{
fclose( pf);
pf=NULL;
}
printf("數據已寫入test.txt文件!\n");
return 0;
}
int fprintf( FILE *stream, const char *format, ... );fprintf()函數根據指定的format(格式)發送參數到由stream指定的文件。fprintf()只能和printf()一樣工作,fprintf()的返回值是輸出的字元數,發生錯誤時返回一個負值。
Ⅹ 怎麼把c語言編的程序的結果輸入到一個文本文件中
c語言編的程序的結果輸入到一個文本文件中可以使用fprintf;
例:
#include<stdio.h>
main(){
FILE *fpt;
fpt = fopen("wendangming.txt","w");//打開文檔,寫入
fprintf(fpt,"Hello world");
fclose(fpt);
}
(10)用c語言將輸入的報文編譯成文本擴展閱讀
它打開一個文本文件,逐個字元地讀取該文件
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream testByCharFile;
int num;
char c;
testByCharFile.open("6.5.cpp",ios::in);
while(!testByCharFile.eof())
{
testByCharFile >> c;
num++;
}
testByCharFile.close();
cout << num << endl;
}