如何在c中读取pc文件|C语言中如何从文件读取

|

1. 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 );

}

(1)如何在c中读取pc文件扩展阅读

使用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 );

}

2. 在c语言中,如何读取一个txt文件中的信息

一般来说在C语言中读取txt文件的信息有两种方法,一种是使用C语言标准文件I/O中的fopen()、fread()等等函数,一种是调用操作系统中的API函数,比如Windows上的ReadFile()、OpenFile()等等,现在操作系统一般都具备内存文件映射功能,对于大的txt文件,一般都使用这种方式操作。下面是一个使用C语言标准文件I/O操作文件的例子。

#include<stdio.h>FILE*stream;voidmain(void){ longl; floatfp; chars[81]; charc; stream=fopen("fscanf.out","w+"); if(stream==NULL) printf("Thefilefscanf.outwasnotopened
"); else { fprintf(stream,"%s%ld%f%c","helloworld", 65000,3.14159,'x'); /*Setpointertobeginningoffile:*/ fseek(stream,0L,SEEK_SET); /*Readdatabackfromfile:*/ fscanf(stream,"%s",s); fscanf(stream,"%ld",&l); fscanf(stream,"%f",&fp); fscanf(stream,"%c",&c); /*Outputdataread:*/ printf("%s
",s); printf("%ld
",l); printf("%f
",fp); printf("%c
",c); fclose(stream); }}

3. c语言中如何实现读取计算机中的文件

c不是有个OPENFILENAME么,那个就是打开文件对话框啊……比如指定个变量fpath存放选中文件的路径,然后fopen(fpath,)就可以了……OPENFILENAME例子神马的自行度娘吧

4. c语言程序怎么调用文件,例如调用电脑中的图片 ,视频等

java,c,c++在处理文件的时候都是大同小异,只不过c语言会更加底层一些。没经过任何封装。java封装了之后,就很丰富了。引申出很多流,字节流,字符流等。不同的文件,如果我们只关心读,写。那么操作都是一样的。比如一个图片,mp3等变成流了之后进行读写。但是如果我们关心的是打开。这个就不太一样了。因为“打开”这个词是站在用户层的角度来说的。打开文件需要关心文件的种类,也就是文件的格式。所以需要有一个打开对应格式文件的程序。比如打开照片可以用window照片查看器。底层的对文件的读写是不关心文件的种类的。因为所有的文件最终的最终都是二进制存储。以流的方式操作文件一般只有读写删。而楼主的意思确切的说,应该是想调用打开某种文件格式的程序。例如楼主可能想如何用c语言调用酷狗播放器,只需要传一个mp3路径就可以播放,如果用c语言调用图片查看器,只需要传一个图片地址就可以打开。

5. C语言如何使用dos命令,如何打开电脑文件

C语言里面使用DOS命令,需要使用system函数。

函数名: system功 能: 发出一个DOS命令用 法: int system(char *command);

打开电脑文件,在DOS下可以使用START命令。

start启动单独的“命令提示符”窗口来运行指定程序或命令。如果在没有参数的情况下使用,start 将打开第二个命令提示符窗口。语法start ["title"] [/dPath] [/i] [/min] [/max] [{/separate | /shared}] [{/low | /normal | /high | /realtime | /abovenormal | belownormal}] [/wait] [/b] [FileName] [parameters]参数"title" 指定在“命令提示符”窗口标题栏中显示的标题。/dpatch 指定启动目录。/i 将 Cmd.exe 启动环境传送到新的“命令提示符”窗口。/min 启动新的最小化窗口。/max 启动新的最大化窗口。/separate 在单独的内存空间启动 16 位程序。/shared 在共享的内存空间启动 16 位程序。/low 以空闲优先级启动应用程序。/normal 以一般优先级启动应用程序。/high 以高优先级启动应用程序。/realtime 以实时优先级启动应用程序。/abovenormal 以超出常规优先级的方式启动应用程序。/belownormal 以低出常规优先级的方式启动应用程序。/wait 启动应用程序,并等待其结束。/b 启动应用程序时不必打开新的“命令提示符”窗口。除非应用程序启用 CTRL+C,否则将忽略 CTRL+C 操作。使用 CTRL+BREAK 中断应用程序。

综上,在C中使用 system("start 文件名");命令即可打开文件。

6. 如何在C程序中访问文本文档,并读取数据

用到的C 函数fgets fopen fclosefgets 的具体用法为: fgets(由文件中读取一字符串) 相关函数 open,fread,fscanf,getc 表头文件 include<stdio.h> 定义函数 char * fgets(char * s,int size,FILE * stream); 函数说明 fgets()用来从参数stream所指的文件内读入字符并存到参数s所指的内存空间,直到出现换行字符、读到文件尾或是已读了size-1个字符为止,最后会加上NULL作为字符串结束。 返回值 gets()若成功则返回s指针,返回NULL则表示有错误发生。 范例 #include<stdio.h>void main(){ char s[80]; fputs(fgets(s,80,stdin),stdout);}但是首先要先打开文本文件,然后再进行读写操作,最后别忘记关闭文件。比如说下面的程序:#include <stdio.h> void main() { char s[255]; int i; int bytesNumber; FILE *fp; fp=fopen("/proc/net/dev", "r"); //选择读取的文件 r 以只读方式打开 fgets(s, 255, fp); printf("%s\n", s); //跳过第一行 fgets(s, 255, fp); printf("%s\n", s); //跳过第二行 fgets(s, 255, fp); printf("%s\n", s); printf("now read bytes:", s); //说明从第三行开始读 fscanf(fp, "%s", s); fscanf(fp, "%d", &bytesNumber); //输出bytes 这个数 printf("bytes is %d", bytesNumber); for( i=1; i<=9; i++) { //for 循环从1开始到9结束 fscanf(fp, "%d", &bytesNumber); } printf("\tAnother bytes is %d\n", bytesNumber); fgets(s, 255, fp); printf("Read complete.\n"); //说明结束 fclose(fp); //关闭文件读取 } }

7. 在C语言中如何打开文件夹中的文件

1、采用C语言的fopen函数2、fopen函数的原型如下:FILE * fopen(const char * path,const char * mode); 详细解释该函数的参数:path 表示文件路径名 mode 表示访问文件的模式(常用访问模式有:“r”表示只读,此时文件必须存在;“w”只写,每次以此方式访问文件都会将文件内容清除重新为写入准备,如果文件不存在,系统自动创建;“a”只写,若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。)3、举例说明:在D盘里的创建了一个文件夹(文件夹名为baozang),在该文件夹里创建一个文本文件(文件名为shuijing.txt)那么以只读方式打开该文件的一段示例代码为:FILE* file;if ( NULL != (file = fopen("D:\\baozang\\shuijing.txt" , "r"))){ 打开成功;}else{ 打开失败;}

8. C语言中如何从文件读取

#include <stdio.h>int main(void){ FILE *pfin, *pfout; int n, i; pfin = fopen("input.txt", "r"); pfout = fopen("output.txt", "w"); fscanf(pfin, "%d", &n); for(i = 0; i < n; i++) { //……. } //…………. //……………. fprintf(pfout, "%d", n); fclose(pfin); fclose(pfout); return 0;}

9. C语言如何读取指定路径下的所有指定格式的文件

用C语言读取目录中的文件名的方法:1、如果是在window环境下,可以用一下方法:使用stdlib.h头文件声明的system()函数_CRTIMP int __cdecl system (const char*);system("dir c:\ /a:h /b > c:\dir.txt");调用系统命令dir,把c:目录下文件列表写入文件dir.txt中2、使用dirent.h头文件中声明的opendir(),readdir()函数;

intmain(intargc,char*argv[]){DIR*directory_pointer;structdirent*entry;if((directory_pointer=opendir("d:\XL"))==NULL)printf("Erroropening
");else{while((entry=readdir(directory_pointer))!=NULL){printf("%s
",entry->d_name);}closedir(directory_pointer);}system("PAUSE");return0;}

3、如果没有dirent.h,可以使用io.h头文件中声明的_findfirst(),_findnext()函数;示例代码:

intmain(intargc,char*argv[]){longfile;struct_finddata_tfind;_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);system("PAUSE");return0;}


赞 (0)