C 将文件逐行读取到结构中
代码:- 我想逐行读取这个文件,并将其存储在变量中,如C 将文件逐行读取到结构中,c,C,代码:- 我想逐行读取这个文件,并将其存储在变量中,如 01 Sun Oct 25 16:03:04 2015 john nice meeting you! 02 Sun Oct 26 12:05:00 2015 sam how are you? 03 Sun Oct 26 11:08:04 2015 pam where are you ? 04 Sun Oct 27 13:03:04 2015 mike good morning. 05 Sun Oct 29 15:0
01 Sun Oct 25 16:03:04 2015 john nice meeting you!
02 Sun Oct 26 12:05:00 2015 sam how are you?
03 Sun Oct 26 11:08:04 2015 pam where are you ?
04 Sun Oct 27 13:03:04 2015 mike good morning.
05 Sun Oct 29 15:03:07 2015 harry come here.
如何做到这一点?
是否可以将文件逐行存储到类似的结构中
int no = 01
char message_date[40] = Sun Oct 27 13:03:04 2015
char friend[20] = mike
char message[120] = good morning.
通过以上代码,我得到以下输出:-
(为了简单起见,我目前只读了一行)
你 一种方法是使用
fgets()
读取文件的每一行,并使用sscanf()
解析每一行。扫描集%24[^\n]
最多可读取换行符上的24个字符。日期格式有24个字符,因此可以读取日期。扫描集%119[^\n]
最多读取119个字符,以防止在消息中写入过多字符,并在换行时停止sscanf()
返回成功扫描的字段数,因此4表示成功扫描的行
01
Sun
Oct
25
16:03:04
2015
john
nice
meeting
#包括
#包括
#包括
结构文本{
国际贸易编号;
字符日期[40];
字符名[20];
字符消息[120];
};
int main()
{
字符行[200];
结构文本文本={0,“,”,“};
FILE*pf=NULL;
if((pf=fopen(“input.txt”,“r”))==NULL){
printf(“无法打开文件\n”);
返回1;
}
而((fgets(line,sizeof(line),pf))){
如果((sscanf(行,“%d%24[^\n]%19s%119[^\n]”
,&text.no,text.date,text.name,text.message))=4){
printf(“%d\n%s\n%s\n%s\n”,text.no,text.date,text.name,text.message);
}
}
fclose(pf);
返回0;
}
使用strstr
而不是strtok
如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Text {
int no;
char date[40];
char name[20];
char message[120];
};
int main( )
{
char line[200];
struct Text text = {0,"","",""};
FILE *pf = NULL;
if ( ( pf = fopen ( "input.txt", "r")) == NULL) {
printf ( "could not open file\n");
return 1;
}
while ( ( fgets ( line, sizeof ( line), pf))) {
if ( ( sscanf ( line, "%d %24[^\n] %19s %119[^\n]"
, &text.no, text.date, text.name, text.message)) == 4) {
printf ( "%d\n%s\n%s\n%s\n", text.no, text.date, text.name, text.message);
}
}
fclose ( pf);
return 0;
}
#包括
#包括
内部主(空){
文件*fp;
const char s[3]=“”;/*正在尝试将2个空格作为分隔符*/
字符*标记,*结束;
字符行[256];
fp=fopen(“input.txt”,“r”);
fgets(行,sizeof(行),fp);
如果(end=strchr(第“\n”行)
*end='\0';//删除换行符
fclose(fp);
令牌=行;
while(令牌!=NULL){
如果(结束=strstr(令牌)
*结束='\0';
printf(“%s”\n”,标记);
如果(结束!=NULL)
token=end+2;//两个空格长度移位
其他的
令牌=NULL;
}
返回0;
}
您可以发布您尝试过的代码吗?这当然是可能的,并且表明你已经尝试过帮助别人根据你目前的方法回答你的问题。当然这是可能的!将数据读入缓冲区,对其进行解析以找到令牌边界,并存储适当的项。如果要复制数据,请执行此操作。如果没有,请在每个条目的末尾将空值写入缓冲区,并在结构中存储指向每个条目开头的指针。我强烈建议您开始阅读一本关于C的书。问题很简单,显然您不了解C。开始学习和编码吧!我投票以离题的方式结束这个问题,因为没有提供编码或工作。可能吗?对如何实现?有数百种方法,我可能会使用fgets()
,strncpy()
和atoi()
。这里什么都看不见,当你真的遇到问题时,问一个问题:)
01
Sun
Oct
25
16:03:04
2015
john
nice
meeting
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Text {
int no;
char date[40];
char name[20];
char message[120];
};
int main( )
{
char line[200];
struct Text text = {0,"","",""};
FILE *pf = NULL;
if ( ( pf = fopen ( "input.txt", "r")) == NULL) {
printf ( "could not open file\n");
return 1;
}
while ( ( fgets ( line, sizeof ( line), pf))) {
if ( ( sscanf ( line, "%d %24[^\n] %19s %119[^\n]"
, &text.no, text.date, text.name, text.message)) == 4) {
printf ( "%d\n%s\n%s\n%s\n", text.no, text.date, text.name, text.message);
}
}
fclose ( pf);
return 0;
}
#include <stdio.h>
#include <string.h>
int main (void){
FILE *fp;
const char s[3] = " "; /* trying to make 2 spaces as delimiter */
char *token, *end;
char line[256];
fp = fopen ("input.txt","r");
fgets(line, sizeof(line), fp);
if(end = strchr(line, '\n'))
*end = '\0';//remove newline
fclose(fp);
token = line;
while(token != NULL){
if(end = strstr(token, s))
*end = '\0';
printf("'%s'\n", token);
if(end != NULL)
token = end + 2;//two space length shift
else
token = NULL;
}
return 0;
}