c中将文件读入字符串|c语言 把文件的内容读入到字符串中

|

A. C语言有没有从文件中读取一行字符串的方法

从文件中查找数据并输出,按以下步骤操作:1.打开文件,如果文件未找到,报错,结束 fopen2.输入待查的字符串,存到变量中gets3.逐行循环读取文件,直到文件结束fgets4.检查字符串是否在该行中,如果在,则输出该行,关闭文件,结束。否则继续查找strstr5.循环结束,输出未找到该字符串。6.关闭文件。fclose相关代码和运行效果如下图:

B. C程序,如何从指定文件中读取一个字符串

#include <stdio.h>#include <stdlib.h>int main(){FILE *fp;char cl[100];if((fp=fopen("1.txt","r"))==NULL)exit(1);while(fscanf(fp,"%s",cl)==1)printf("%s ",cl);if(fclose(fp)!=0)exit(1);return 0;}

C语言是目前世界上流行、使用最广泛的面向过程的高级程序设计语言。 C语言对操作系统和系统使用程序以及需要对硬件进行操作的场合,用C语言明显优于其它高级语言,许多大型应用软件都是用C语言编写的。

C. c语言 把文件的内容读入到字符串中

问题一:C语言里面没有string….需要定义为字符数组问题二:要读到结构体,你这个结构体里面少一个指向下一个的指针,这样才能构成链表。struct Car{ char area; char number[6]; unsigned long chnum; char owner[10]; char brand[10]; struct Car *next;} *LNCar;txt文件中间隔都用\t,然后读取到结构体的代码如下:/*fp 是程序开始定义的文件指针*/FILE *fp;int Biao=0;struct Car *p1=NULL,*p2=NULL,*p3=NULL;p1=(struct Car *)malloc(sizeof(struct Car)); /*给结构指针申请空间*/p2=(struct Car *)malloc(sizeof(struct Car)); while(fscanf(fp,"%c\t%s\t%s\t%s\t",&p1->area,&p1->number,&p1->ower,&p1->brand)!=EOF){ if(Biao==0) /*读取第一个的时候要付给LNCar*/ { Biao=1; LNCar=p1; p2=p1; LNCar->next=NULL; p1=(struct Car *)malloc(sizeof(struct Car)); } else { p2->next=p1; p2=p1; p2->next=NULL; p1=(struct Car *)malloc(sizeof(struct Car)); }} 经过上面的代码读取后,txt文件内容就读取到LNCar为头的链表中了。

D. C语言文件读入字符串问题

fscanf遇到\r, \n, \v, \t, \f, 空格时会结束,所以不能正确解析文件的数据行,可考虑使用sscanf.

E. C++如何从文件中读取字符串

一般来说在C++语言中读取txt文件的信息有三种方法:

1、使用C语言标准文件I/O中的fopen()、fread()等等函数。示例如下(vc6.0下编译通过):

#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);}}

2、使用C++语言中的文件I/O中的ofstream,ifstream,fstream。示例如下(vc6.0下编译通过):

#include<iostream>#include<fstream>#include<cstdlib>usingnamespacestd;intmain(){charbuffer[256];ifstreamin("test.txt");if(!in.is_open()){cout<<"Erroropeningfile";exit(1);}while(!in.eof()){in.getline(buffer,100);cout<<buffer<<endl;}return0;}

3、使用操作系统中的API函数,比如Windows上的ReadFile()、OpenFile()等等,现在操作系统一般都具备内存文件映射功能,对于大的txt文件,一般都使用这种方式操作。

F. C++中怎样将一个文件的内容读取到string类型的字符串中。谢谢!

先将文件全部读入 char* 变量。再用 string 类 构建函数建一个string 对象,在把 char* 内容放入。 下面是将文件全部读入char * buffer;/* fread example: read an entire file */ #include <stdio.h> #include <stdlib.h> int main () { FILE * pFile; long lSize; char * buffer; size_t result; pFile = fopen ( "myfile.bin" , "rb" ); if (pFile==NULL) {fputs ("File error",stderr); exit (1);} // obtain file size: fseek (pFile , 0 , SEEK_END); lSize = ftell (pFile); rewind (pFile); // allocate memory to contain the whole file: buffer = (char*) malloc (sizeof(char)*lSize); if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);} // the file into the buffer: result = fread (buffer,1,lSize,pFile); if (result != lSize) {fputs ("Reading error",stderr); exit (3);} /* the whole file is now loaded in the memory buffer. */ // terminate fclose (pFile); free (buffer); return 0; } 构建函数建一个string 对象,把 char * buffer 内容存入 程序部分,请自己补充:#include <windows.h>#include<iostream>#include <string>using namespace std;#include <stdio.h>// 插入上面程序 …..// 补充string sss;sss.assign(buffer,result);cout << sss << endl;

G. 怎么用C语言读取 TXT文件中的字符串

可以使用输入输出重定向来将TXT文本中的字符内容导入程序中,或者使用标准C库函数:fopen()和fgetc();先使用fopen()函数打开TXT文本文件,然后使用fgetc读取文本文件中的字符。读取全部文本中全部字符可以使用一个while循环加判断是否读取到文件结尾来实现:charch;while((ch=fgetc(fp))!=EOF)这样当读取到文件结尾时,while循环就会终止。C语文编写编译如下:#include #include #include #define MAXLINE 100000#define BUFLEN 1024int main(){FILE *file;char buf[BUFLEN];int len=0,i=0;char *array[MAXLINE];file=fopen("test.txt","r");//打开TXST.TxT文件if(!file)return -1;while(fgets(buf,BUFLEN,file))//读取TXT中字符{ len=strlen(buf); array[i]=(char*)malloc(len+1); if(!array[i])break; strcpy(array[i++],buf);}fclose(file);i–;while(i>=0&&array[i]){ printf("%s\n",array[i]);//打印test文档的字符 free(array[i–]);}}

H. C语言程序从文件中读取字符串

学了结构体了没?学过结构体就用以下方法读写文件/* 写文件 */#include <stdio.h>#include <stdlib.h>#include <string.h>// 用户信息结构体struct user_info{ char name[30]; // 姓名 char sex[10]; // 性别 int id; // 帐号 };int main(){ FILE *fp = fopen("user_info.txt","w+b"); // 创建一个文件 if(fp == NULL){ // 如果创建文件失败 perror("fopen()"); return -1; } struct user_info u; strcpy(u.name, "xiaoming"); strcpy(u.sex, "boy"); u.id = 12345678; fwrite(&u, sizeof(u), 1, fp); // 把信息写入到文件中 close(fp); return 0;}/* 读文件 */#include <stdio.h>#include <stdlib.h>#include <string.h>struct user_info{char name[30]; // 姓名 char sex[10]; // 性别 int id; // 帐号 };int main(){ FILE *fp = fopen("user_info.txt","r+b"); // 打开一个文件 if(fp == NULL){ // 如果打开文件失败 perror("fopen()"); return -1; } struct user_info u; fread(&u, sizeof(u), 1, fp); // 把读取文件 // 打印读取的信息 printf("name:%s sex:%s id:%d\n", u.name, u.sex, u.id); close(fp); return 0;}

I. C语言中,想把文件中的内容读取到一组字符串该怎么办帮忙找找错。

#include<stdio.h>#include<stdlib.h>voidmain(){charline[15][50];FILE*outFile=fopen("D:\Holidays.txt","r");for(inti=0;i<15;i++)fscanf(outFile,"%s",line[i]);system("pause");}

这是我帮你改的,你是打开文件失败了,还有就是没有空间存在字符串


赞 (0)