① c语言读取指定行数据
#include<stdio.h>
#defineMAXC1024
intReadSpeacialLine(inti)
{
charfilename[]="test.c";//文件名
FILE*fp;
intret=0,L;
intWhichLine=i;//指定要读取哪一行
intCurrentIndex=0;//当前读取的行
charStrLine[MAXC];//每行最大读取的字符数,可据实际情况更改
if((fp=fopen(filename,"r"))==NULL)//打开文件
{
printf("openfileerror! ");
returnret;
}
while(1){
if(NULL==fgets(StrLine,MAXC,fp))break;
L=strlen(StrLine);
if(' '==StrLine[L-1]){
memset(ln+L-1,0,MAXC-L+1);
CurrentIndex++;
if(WhichLine==CurrentIndex)
{
fgets(StrLine,MAXC,fp);//读取一行
printf("%s",StrLine);//输出
ret=1;
returnret;
}
}
else{//行内容太多StrLine会读不到行结尾的情况
CurrentIndex++;
if(WhichLine==CurrentIndex)
{
fgets(StrLine,MAXC,fp);//读取一行
printf("%s",StrLine);//输出
ret=1;
returnret;
}
while(1){
if(NULL==fgets(StrLine,MAXC,fp))gotoEND;
L=strlen(ln);
if(' '==ln[L-1])break;
}
}
}
end:
fclose(fp);//关闭文件
returnret;
}
使用fgets函数,fgets是每次读取一行。
使用memset来清除StrLine,防止上一次StrLine中的内容未清除。
满意请采纳,不懂请追问。
② C语言如何实现从txt中读取特定数据
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char *pcTmp = NULL;
char szBuf[] = "abcdef 123456";
char seps[] = " ";
char ch[20];
int num;
pcTmp = strtok(szBuf,seps);
if( NULL != pcTmp )
{
strcpy(ch, pcTmp);
printf("ch is %s\n", ch);
}
pcTmp = strtok(NULL,seps);
if( NULL != pcTmp )
{
num = atoi(pcTmp);
printf("num is %d\n", num);
}
system("pause");
}
③ C语言中怎么从文件中读取特定数据
哥哥我想出的办法是这样的:
因为兄弟文件格式是一行一行的, 且每行开头是一个关键字,然后后面是相应数据, 所以哥哥是这么想的, 比如要读"AB2345"这个关键字对应行的内容, 那首先用变量或者宏定义定义下来, 然后从文件开头开始, 一个字符一个字符的扫描, 对每一行的开始的6个字符组成的关键字读出来跟"AB2345"这个关键字比较, 判断是否是想要读取的, 如果是, 那么通过ftell, fseek 两个函数分别得出当前指针的位置和适当移动指针的位置, 最后读取相应内容输出来!
详细代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 1024
#define KEY "AB2345"
#define KEY_LEN 7
int main()
{
int ch = 0;
int first = 1;//开始时的标志,因为是一个字符一个字符的扫描
int flag = 0;//文件开头是不是所要读内容的标志
int count = 0;//遇到'\n'的个数
int pre_pos = 0, cur_pos = 0;//前一次和当前文件指针的位置
char buf[BUF_SIZE] = {0};
FILE *fp = NULL;
fp = fopen("test.txt", "r");
if (fp == NULL)
{
printf("Cann't open the file!\n");
exit(1);
}
else
{
while ((ch = fgetc(fp)) != EOF)
{
if (first)
{
//若要读取的内容在文件开头就有时
//移动指针到文件开头
fseek(fp, -1L, SEEK_CUR);
fgets(buf, KEY_LEN, fp);
if (strcmp(buf, KEY) == 0)
{
first = 0;
flag = 1;
continue;
}
else
{
first = 0;
}
}
if (ch == '\n')
{
count++;//遇到'\n'的个数
pre_pos = cur_pos;//上次遇到'\n'时文件指针的位置
cur_pos = ftell(fp);//当前遇到'\n'时文件指针的位置
//文件开头内容符合要求的就适当移动指针位置
//然后读取输出来
if (count == 1 && flag == 1)
{
fseek(fp, 0L, SEEK_SET);
memset(buf, 0, sizeof(buf));
fgets(buf, cur_pos - 1, fp);
printf("%s\n", buf);
}
//之后内容符合要求的就适当移动指针位置
//然后读取输出来
else
{
memset(buf, 0, sizeof(buf));
fgets(buf, KEY_LEN, fp);
if (strcmp(buf, KEY) == 0)
{
fseek(fp, (-1) * (KEY_LEN - 1), SEEK_CUR);
memset(buf, 0, sizeof(buf));
fgets(buf, cur_pos-1-pre_pos, fp);
printf("%s\n", buf);
}
}
}
}
}
fclose(fp);
return 0;
}
④ 如何用C语言,读文件中某行某列的数据。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int fileToFile(char *nameSrc, char *nameDes);
char * getLineBuf(int line, int col, int len, char *filename);
/**************************************************************
说明:
1.使用了内存实时分配,可以满足读取任意长度的字符
但是,需要调用时手动释放内存,否则内存泄露。
2.对中文没有判断,会出现乱码。
***************************************************************/
char * getLineBuf(int line, int col, int len, char *filename)
{
char ch;
int l = 1;
char *pT = NULL;
int i = 0;
char *pA = (char *)malloc(sizeof(char)*len+1);
memset(pA, 0x00, sizeof(char)*len+1);
FILE *fp = NULL;
if (!(fp = fopen(filename, "r")))
{
printf("打开文件失败\n");
goto End;
}
while(!feof(fp))
{
if (l == line)
{
fseek(fp, col-1, SEEK_CUR);
i = fread(pA, sizeof(char), len, fp);
if(i<len)
{
printf("字符长度不足!");
pA[i] = '\0';
goto End;
}
if (pT = strstr(pA,"\n"))
{
printf("本行长度不足");
pA[pT-pA] = '\0';
goto End;
}
break;
}
ch = fgetc(fp);
if(ch == '\n')
{
l++;
}
}
if (l+1 < line)
{
printf("文件行数不足");
}
End:
fclose(fp);
return pA;
}
/*******************************************
说明:
1.系统命令的调用,可以查询下DOS命令的使用
2.如果需要复杂的DOS命令,可以使用字符串拼装
********************************************/
int main()
{
int i = 0;
char *s = NULL;
FILE *fp = NULL;
char filename[100];
char buf[100];
memset(buf, 0x00, sizeof(buf));
char *log = "log.txt";
//sprintf(buf, "dir /B %s > %s", "*.txt", log);//字符串拼装
//system(buf);//调用系统命令
system("dir /B *.txt > log.txt");//调用系统命令
fp = fopen(log, "r");
if (fp == NULL)
printf("文件打开失败");
while(!feof(fp))
{
fscanf(fp, "%s", filename);
printf("\n文件=%s\n", filename);
for (i=1; i<=3; i++)
{
s = getLineBuf(i, 1, 2 , filename);
printf("%s\n", s);
if (s)
{
free(s); //手动释放内存,否则内存泄露
s = NULL;
}
}
}
fclose(fp);
system("del log.txt");
return 0;
}
⑤ C语言怎么实现读取txt文件中指定的数据
可以用fscanf读取后 再判断
比如
charname[100];
inta,b,stack_size;
FILE*fp;
fp=fopen("task.txt","r");//假定存在这个文件中。
while(~fscanf("%s%*d%*s%*d%d,%dK",name,&a,&b))
if(strcmp(name,"QQ.exe")==0)break;//找到一个就退出。如果要多个,可以自行优化。
stack_size=a*1000+b;//KBytes
...//处理
fclose(fp);
⑥ C程序怎样从文件中读某些特定字符串开头的数据
每次读一块,如果是字符串形式的,那么以%s读取。
如果每个数据段是一行,那么用fgets。
然后用strncmp去比较开头部分数据,如果相同,则处理。否则抛弃。
⑦ c语言如何读取文件中的特殊数据。
我的方法是采用fgetc()函数逐个读取,遇到空格字符就将之前的内容保存到数组里面,遇到换行符就参数归零,在第二行重新读取文件内容。比如说第一个空格之前的是配置项vertex
第二个空格之前的是配置内容1,第三个空格之前的是配置内容2,读取完毕按照规则打印出来就行了,代码我懒得写了,希望你多多了解基本C库函数,锻炼自己的思路,C语言重要的是编程思想。
无论一个数据怎么长,怎么复杂,你都可以把他看成是一个个字符组成的,或者当成一个字符串。
⑧ C语言如何读取文件中指定的某一段
#include<stdio.h>
#include<string.h>
#define TBEGIN "B"
#define TEND "END B"
void main() { FILE *fp; char buffer[256];
if ( fp=fopen("c:\test.txt","r") ) {
while ( !feof(fp) ) { fgets(buffer,255,fp); if ( strcmp(buffer,TBEGIN)==0 ) break; }
while ( !feof(fp) ) {
fgets(buffer,255,fp); if ( strcmp(buffer,TEND)==0 ) break; else printf("%s ",buffer);
}
fclose(fp);
} else printf("无法打开文本文件. ");
}
⑨ c语言怎么实现从文件中读取指定行内容
//假设一行的文本内容不超过1000字符,如果估计超过,请自行修改函数中的1000
//如果打不开文件,返回NULL
//如果文件不足n行,返回NULL
//如果正获得正常数据,返回str的地址
char
*getfileline(char
*p,int
n,
char
*str){
FILE
*fp;
int
i;
if
((fp=fopen(p,"r"))==NULL){
printf("打开文件错误\n");
return
NULL;
}
for(i=1;i<n;i++)
⑩ C语言中如何读取txt文件中指定的数据
//其中的in.txt就是你要读取数据的文件,当然把它和程序放在同一目录
-------------------------------------
#include
int
main()
{
int
data;
file
*fp=fopen("in.txt","r");
if(!fp)
{
printf("can't
open
file\n");
return
-1;
}
while(!feof(fp))
{
fscanf(fp,"%d",&data);
printf("%4d",data);
}
printf("\n");
fclose(fp);
return
0;
}