热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在部分行上使用fread()时,如何移动到下一行?

我使用fread()和fseek()来收集字符串的一部分。我不是在整行上使用fread(

我使用fread()和fseek()来收集字符串的一部分。我不是在整行上使用fread()。

我会整行,但是据我所知,您不能在正确的字符数组上使用fseek()吗?

`int parse(const char *f,const struct stat *flightD,int type){
//file pointer
Airport_S *Air_Pts = (Airport_S *)malloc(sizeoftype(Airport_S));
FILE *fp;
//char need[10];
char airLineFile[2];
char chkAirPt[3];
fp = fopen(f,"r");
if(type == FTW_F){ // test for 'type' of FTW_F
//check to see if the file opened successfully
if(fp == NULL)
printf("Cannot open file %s",f)
return 1;
while (!(FEOF)){
//fgets(need,10,fp)
//must return zero to parent funtion to continue tree traversal
// ?? While current dir != originally called dir?
//open the file,read it's contents and assess them
fseek(fp,5,SEEK_SET) //set FP to right before airport code
chkAirPt = fread(chkAirPt,sizeof(char),3,fp)
fseek(fp,SEEK_SET);
//combine the airline abbreviation with '.txt'
airLineFile = strcat(fread(airLineFile,2,fp),".txt");
//if the struct has no values in it,populate it with this first one.
if(Air_Pts->airport == NULL){
//Set info for very first node
Air_Pts->airPt=strcpy(Air_Pts->airport,chkAirPt);
fseek(fp,SEEK_SET);
Air_Pts->fltInfo->airLine=airLineFile;
Air_Pts->fltInfo->next = NULL;
Air_Pts->fltInfo->prev = NULL;
Air_Pts->next = NULL;
Air_Pts->prev = NULL;
//what is the file going to do after this?
}
else if(strcmp(Air_Pts->airport,chkAirPt) == 0){
if(strcmp(Air_Pts->fltInfo->airLine,airLineFile) == 0){
Air_Pts->fltInfo->occ++;
}
else
Air_Pts->fltInfo = addAirline(Air_Pts->fltInfo);
}
// some code
return 0;
else //anything other than a file -or- FTW_D
return 1;
}
}

}`


您太努力了。只需读取并丢弃不需要的数据即可。例如:

/* Sample input line: AA43 DTW2315 ... */
/* Read first two columns of each line of a text file into a struct */
#include
#include
#include
struct data {
char airport[32];
char flight[32];
struct data *next;
};
FILE * Fopen(const char *path,const char *mode);
void * xmalloc(size_t);
void
push(struct data **head,struct data new)
{
struct data *t = xmalloc( sizeof *t);
t->next = *head;
strncpy(t->airport,new.airport,sizeof t->airport);
strncpy(t->flight,new.flight,sizeof t->flight);
*head = t;
}
int
main(int argc,char **argv)
{
FILE *ifp = argc > 1 ? Fopen(argv[1],"r") : stdin;
struct data *head = NULL;
struct data this;
int line = 1;
int c = 0;
while( (c = fscanf(ifp,"31%s %31s",this.airport,this.flight)) == 2) {
push(&head,this);
/* Discard until the end of line */
while( (c = fgetc(ifp)) != EOF ) {
if( c == '\n') {
line += 1;
break;
}
}
}
/* Print all the records in reverse order */
for( ; head; head = head->next ) {
printf(" %s: %s\n",head->airport,head->flight);
}
return 0;
}
FILE *
Fopen(const char *path,const char *mode)
{
FILE *rv = fopen(path,mode);
if( rv == NULL ) {
perror(path);
exit(EXIT_FAILURE);
}
return rv;
}
void *
xmalloc(size_t s)
{
void *rv = malloc(s);
if( rv == NULL ) {
perror("malloc");
exit(EXIT_FAILURE);
}
return rv;
}

推荐阅读
author-avatar
只属于我一个人的秘密
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有