‘壹’ 本人在文本文档里写了一个c语言的程序
修改文本文档扩展名:把.txt改为.c ,然后直接打开,由于路径问题,你必须把更改后的文件复制到tc文件夹下面
‘贰’ C语言文本文件的读写
//---------------------------------------------------------------------------
#include <stdio.h>
struct pro
{float msl;
float la,lo;
double refractivity,density,pressure;
float temp;
}atm[200];
int main(void)
{
FILE *fp=fopen("infile.dat","r"),*fp2=NULL;
int i;
if (!fp||(fp2=fopen("out.txt","w"))==NULL) {
fprintf(stderr,"FILE ERROR");
return -1;
}
for (i = 0; i<200; i++) {
/*从infile.dat文件读取数据*/
fscanf(fp,"%f%f%f%lf%lf%lf%f",&atm[i].msl,&atm[i].la,&atm[i].lo,&atm[i].refractivity,&atm[i].density,&atm[i].pressure,&atm[i].temp);
/*将数据写到out.txt文件*/
fprintf(fp,"%f\t%f\t%f\t%lf\t%lf\t%lf\t%f\n",atm[i].msl,atm[i].la,atm[i].lo,atm[i].refractivity,atm[i].density,atm[i].pressure,atm[i].temp);
}
fclose(fp);
fclose(fp2);
return 0;
}
//---------------------------------------------------------------------------
‘叁’ 。求````用C语言编写一个文本文件
同上
但是我提醒一下,c语言可以实现这个程序!
先用fgets读,存变量里,建议数组,然后用fputs写入另一个txt文档里,fopen可以创建文档!
‘肆’ C语言文件写入怎么操作
C++的文本文件写入
// outfile.cpp -- writing to a file
#include <iostream>
#include <fstream> // for file I/O
int main()
{
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile; // create object for output
outFile.open("carinfo.txt"); // associate with a file
cout << "Enter the make and model of automobile: ";
cin.getline(automobile, 50);
cout << "Enter the model year: ";
cin >> year;
cout << "Enter the original asking price: ";
cin >> a_price;
d_price = 0.913 * a_price;
// display information on screen with cout
cout << fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout << "Make and model: " << automobile << endl;
cout << "Year: " << year << endl;
cout << "Was asking $" << a_price << endl;
cout << "Now asking $" << d_price << endl;
// now do exact same things using outFile instead of cout
outFile << fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << "Make and model: " << automobile << endl;
outFile << "Year: " << year << endl;
outFile << "Was asking $" << a_price << endl;
outFile << "Now asking $" << d_price << endl;
outFile.close(); // done with file
return 0;
}
‘伍’ C语言怎样创建一个文本文件
读文件是这样的:
#include
"stdlib.h"
main()
{
FILE
*fp;
char
buf;
fp=fopen("a.txt","r");
while(fread(&buf,1,1,fp))
printf("%c
",buf);
fclose(fp);
}
写文件是这样的:
#include
"stdlib.h"
main()
{
FILE
*fp;
char
buf;
fp=fopen("a.txt","w");
while((buf=getchar())!='q')
fwrite(&buf,1,1,fp);
fclose(fp);
}
PS:这里buf缓冲区只开了一个字节,做个例子,你可根据需要变化.
你的问题可能原因是:输出内容超过了你的缓冲区.
比如:你读出10个字节,甚至可能没读出,而你打印100个字节,后面的就很有可能是"烫".应该不是2进制的问题.
‘陆’ 用文本文档编写的一个c语言程序应该如何运行
使用C编译器编译为可执行文件。
常见的编译器有MSC、VC、TC、BC、BCB、GCC、GC等等。
C语言是不能解释运行的,必须先编译为可执行文件。
不过早前听说有可解释运行的C环境,好象还是国产的,可以搜索下。
‘柒’ 本人在文本文档里写了一个C语言的程序
进入turboc界面-alt+f-移动箭头键选择open-输入你的文本文档的完整路径或把文本文档拷贝到turboc目录里只输入文件名(都要有.txt)-回车-OK!
‘捌’ C语言如何实现对txt文件的读取和写入
使用fopen的r方式可以实现读取,用w+方式可以实现写入。
1.fopen的函数原型:FILE
*
fopen(const
char
*
path,const
char
*
mode);
fopen函数的第一个参数是文件路径,第二个参数是打开方式,有以下几种方式:
r
以只读方式打开文件,该文件必须存在。
r+
以可读写方式打开文件,该文件必须存在。
rb+
读写打开一个二进制文件,允许读数据。
rw+
读写打开一个文本文件,允许读和写。
w
打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
w+
打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
a
以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)
a+
以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。
(原来的EOF符不保留)
wb
只写打开或新建一个二进制文件;只允许写数据。
wb+
读写打开或建立一个二进制文件,允许读和写。
wt+
读写打开或着建立一个文本文件;允许读写。
at+
读写打开一个文本文件,允许读或在文本末追加数据。
ab+
读写打开一个二进制文件,允许读或在文件末追加数据。
上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b
字符用来告诉函数库打开的文件为二进制文件,而非纯文字文件。
返回值:文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno中。
2.例程:
#include<stdio.h>
#define F_PATH "d:\\myfile\\file.dat"
char c;
int main(){
FILE*fp=NULL;//需要注意
fp=fopen(F_PATH,"w"); //创建文件
if(NULL==fp) return -1;//要返回错误代码
while(scanf("%c",&c)!=EOF) fprintf(fp,"%c",c); //从控制台中读入并在文本输出
fclose(fp);
fp=NULL;//需要指向空,否则会指向原打开文件地址
return 0;
}
‘玖’ c语言 文本文件的操作 字符写入
#include<stdio.h>
static
int
write_flag=1;
/*写文件标志,即是否要将字符串写入文件*/
void
function(char
*str)
{
int
i=0;
do
{
if((str[i++]=='b')&&(str[i]=='y')&&(str[i+1]=='e'))/*字符串中有“bye字串”*/
{
str[i+2]='\0';
/*截断字符串*/
write_flag=0;
/*写文件标志置
0
*/
break;
}
}while(str[i+2]!='\0');
}
int
main()
{
int
i,j;
char
str[100]={0};
FILE
*fp;
fp=fopen("answer.txt","w");
do
{
gets(str);
function(str);
fprintf(fp,"%s\n",str);
}while(write_flag);
fclose(fp);
while(1)
gets(str);/*接收其他无效字符串。自己添加程序结束条件*/
return
0;
}
亲测通过!
‘拾’ 用C语言编写“读取一个给定的文本文件,并将文件的内容显示在屏幕上”的一个程序
#include<stdio.h>
#include<string.h>
voidmain()
{
FILE*fp;//创建一个文件指针*fp
charch;
fp=fopen("D:/data.txt","r");//以只读方式打开D:data.txt文件
if(fp==NULL)
printf("cannotopen! ");//如果fp指针指向为空,即文件为空,则输出cannotopen
else{
//读取字符:fscanf(fp,"%c",&ch),ch=fgetc(fp);
fscanf(fp,"%c",&ch);//读取字符
while(!feof(fp)){//feof()这个函数是用来判断指针是否已经到达文件尾部
putchar(ch);//输出
fscanf(fp,"%c",&ch);//再次读取字符
}
fclose(fp);//关闭文件
}
printf(" ");
}