当前位置:首页 » 编程语言 » c语言文件读取代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言文件读取代码

发布时间: 2022-04-20 07:55:33

c语言编写保存一个文件并读取,按照以下要求编写代码

#include<stdio.h>
void main() { FILE *fp; char str1[80],str2[80],*p,*q;
gets(str1); p=q=str1;
while ( *p ) {
if ( *p=='!' ) break;
if ( *p>='a' && *p<='z' ) { *q=*p; q++; }
p++;
}
*q=0;
if ( fp=fopen("D:\\fie.txt","w+") ) {
fprintf(fp,"%s\n",str1); fclose(fp);
if ( fp=fopen("D:\\fie.txt","r") ) {
fscanf(fp,"%s",str2); fclose(fp); printf("%s\n",str2);
} else printf("无法打开文件读取。\n");
} else printf("无法建立文件。\n");

}

❷ C语言如何实现对txt文件的读取和写入

1、使用VS新建空工程,直接点击确定,如下所示。

❸ C语言怎么读取某一文件夹下的所有文件夹和文件

读取的代码方式如下:

int main()

{

long file;

struct _finddata_t find;

_chdir("d:\");

if((file=_findfirst("*.*", &find))==-1L)

{

printf("空白! ");

exit(0);

}

printf("%s ", find.name);

while(_findnext(file, &find)==0)

{

printf("%s ", find.name);

}

_findclose(file);

return 0;

}

❹ c语言读取文本文件

1、C语言标准库提供了一系列文件操作函数。文件操作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件操作位置的获取与设置。
2、例程:

#include<stdio.h>
inta;
charb,c[100];
intmain(){
FILE*fp1=fopen("input.txt","r");//打开输入文件
FILE*fp2=fopen("output.txt","w");//打开输出文件
if(fp1==NULL||fp2==NULL){//若打开文件失败则退出
puts("不能打开文件!");
rturn0;
}
fscanf(fp1,"%d",&a);//从输入文件读取一个整数
b=fgetc(fp1);//从输入文件读取一个字符
fgets(c,100,fp1);//从输入文件读取一行字符串

printf("%ld",ftell(fp1));//输出fp1指针当前位置相对于文件首的偏移字节数

fputs(c,fp2);//向输出文件写入一行字符串
fputc(b,fp2);//向输出文件写入一个字符
fprintf(fp2,"%d",a);//向输出文件写入一个整数

fclose(fp1);//关闭输入文件
fclose(fp2);//关闭输出文件,相当于保存
return0;
}

❺ c语言从文件读取数据

四个整型 一个浮点型, 所以 用数组的话 只能是用浮点型数组, 即float a[5];

或者用五个变量, 可以是四个整型,一个浮点型. int a,b,d,e; float c;

打开文件部分相同.

FILE*fp=fopen("input1.txt","r");

读取数据, 数组方式:

inti;
floata[5];
fscanf(fp,"%f,",&a[0]);
for(i=1;i<5;i++)
fscanf(fp,"%f",&a[i]);

变量方式:

inta,b,d,e;
floatc;
fscanf(fp,"%d,%d%f%d%d",&a,&b,&c,&d,&e);

❻ C语言读取文件数据

先用getline或fgets读取数据到一个字符串中,这个串定义的长一点,保证够用就行。

举例:

char str[1024]。

fgets( str , sizeof(str), fp ),//fp打开的文件指针。

然后调用for循环去读取串的的数据,假设你的分隔符是空格,遍历str,遇到空格就得到一个数据,然后继续遍历,直到str尾。

再去fgets数据,再遍历,即可得到所有的数据。

拓展资料:

C语言是一门通用计算机编程语言,广泛应用于底层开发。

C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

❼ C语言读取文件内容的程序

感觉你贴出来的代码跟题目要求差距有点大啊

代码(ps:这里输入的文件名就是一个相对路径,所以给定的测试文本要放在本程序同目录下):

#include<stdio.h>
#include<stdlib.h>
voidmain()
{
FILE*fp;
charfilename[30],temp[1024];
intcount,i,flag=1;
printf("请输入文件名:");
gets(filename);
if((fp=fopen(filename,"r"))==NULL)//文件不存在
{
printf("FileNameError ");
exit(0);
}
else//文件存在
{
printf("请选择行数:");
scanf("%d",&count);
for(i=1;i<=count;i++)
{
if(fgets(temp,1024,fp)==NULL)//不存在第count行
{
flag=0;
break;
}
}
if(flag==0)
printf("LineNoError ");
else
printf("第%d行是:%s",count,temp);//打印第count行
}
fclose(fp);
}

测试文本内容:

❽ C语言读取文件内容,按行读

C语言逐行读取文件内容 ,参考代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineMAX_LINE1024
intmain()
{
charbuf[MAX_LINE];/*缓冲区*/
FILE*fp;/*文件指针*/
intlen;/*行字符个数*/
if((fp=fopen("test.txt","r"))==NULL)
{
perror("failtoread");
exit(1);
}
while(fgets(buf,MAX_LINE,fp)!=NULL)
{
len=strlen(buf);
buf[len-1]='';/*去掉换行符*/
printf("%s%d ",buf,len-1);
}
return0;
}

❾ 关于C语言中文本文件的逐行读取的实现

若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。

以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。

C语言中文本文件的逐行读取的实现的代码如下:

#include<stdio.h>

main()

{

FILE * fp;

fp=fopen(“noexist”,”a+”);

if(fp= =NULL) return;

fclose(fp);

}

(9)c语言文件读取代码扩展阅读

1、如果输入文本每行中没有空格,则line在输入文本中按换行符分隔符循环取值。

2、如果输入文本中包括空格或制表符,则不是换行读取,line在输入文本中按空格分隔符或制表符或换行符特环取值。

3、可以通过把IFS设置为换行符来达到逐行读取的功能。

❿ C语言如何读取txt文本里面的内容

C语言可以使用fopen()函数读取txt文本里。

示例:

#include <stdio.h>

FILE *stream, *stream2;

void main( void )

{

int numclosed;

/* Open for read (will fail if file "data" does not exist) */

if( (stream = fopen( "data", "r" )) == NULL )

printf( "The file 'data' was not opened " );

else

printf( "The file 'data' was opened " );

/* Open for write */

if( (stream2 = fopen( "data2", "w+" )) == NULL )

printf( "The file 'data2' was not opened " );

else

printf( "The file 'data2' was opened " );

/* Close stream */

if(fclose( stream2 ))

printf( "The file 'data2' was not closed " );

/* All other files are closed: */

numclosed = _fcloseall( );

printf( "Number of files closed by _fcloseall: %u ", numclosed );

}

(10)c语言文件读取代码扩展阅读

使用fgetc函数

#include <stdio.h>

#include <stdlib.h>

void main( void )

{

FILE *stream;

char buffer[81];

int i, ch;

/* Open file to read line from: */

if( (stream = fopen( "fgetc.c", "r" )) == NULL )

exit( 0 );

/* Read in first 80 characters and place them in "buffer": */

ch = fgetc( stream );

for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )

{

buffer[i] = (char)ch;

ch = fgetc( stream );

}

/* Add null to end string */

buffer[i] = '';

printf( "%s ", buffer );

fclose( stream );

}