Ⅰ 怎么用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;
}