C 如何使用strtok()读取文本文件并创建结构?
给定一个文本文件:C 如何使用strtok()读取文本文件并创建结构?,c,arrays,file,struct,linked-list,C,Arrays,File,Struct,Linked List,给定一个文本文件: I Angelina Jolie 1 7728323 I Mel Gibson 3 7809606 7733889 7724609 I Robert Redford 2 7721170 7731959 I Jennifer Aniston 4 2188989 2189898 2181020 2183456 I Jami Gertz 4 7734404 7774012 7773023 7921492 I Brad Pitt 2 7774017 7878485 R Sylvest
I Angelina Jolie 1 7728323
I Mel Gibson 3 7809606 7733889 7724609
I Robert Redford 2 7721170 7731959
I Jennifer Aniston 4 2188989 2189898 2181020 2183456
I Jami Gertz 4 7734404 7774012 7773023 7921492
I Brad Pitt 2 7774017 7878485
R Sylvester Stallone 0
I Victoria Principal 3 7933045 7771234 7820987
R Jennifer Aniston 0
R Sean Penn 0
I Kevin Costner 1 7874014
Q
我需要阅读每一行,用空格分隔值,并创建每一行的结构。我目前的代码是:
int main(){
int y;
FILE *data;
char action;
char line[100];
int counter = 0;
int index = 0;
struct number{
int phoneNumber;
struct number *next;
};
struct contact{
char fName[10];
char lName[10];
struct number *start;
};
struct number numbers[50];
struct contact directory[10];
if((data=fopen("hw6data.txt", "r")) != NULL){
while(fscanf(data, "%s", line) != EOF){
char s[2] = " ";
char *token;
token = strtok(line, s);
while(token != NULL){
if(counter==0){
if(s == "I") {
if(counter==1){
strcpy(directory[index].fName, s);
}
if(counter==2){
strcpy(directory[index].lName, s);
}
}
}
token = strtok(NULL, s);
}
}
}
for(y = 0; y < 10; y++){
printf("%s ", directory[y].fName);
printf("%s\n", directory[y].lName);
}
fclose(data);
return 1;
帮助?我可以一眼看到一些问题(不一定是完整的列表):
- 行
一次不能读取整行(这似乎是您的意图,因为您将变量命名为while(fscanf(data,“%s”,line)!=EOF)
)。您可能希望执行line
while(fgets(data,100,line)!=NULL)
- 如果(s==“I”),则不能在C中进行字符串比较。如果您只是检查第一个字符,您可以执行
(请注意,这里使用单引号(如果(s[0]=“I”)
)表示字符文字,而使用双引号(“”
)表示字符串文字(“”)
- 您在
和if(counter==1)
中嵌套了if(counter==2)
,因此这些条件永远不会为真,除非您在if(counter==0)
之后和if(counter==0)
之前的某个点修改if(counter==1)
counter
和计数器
永远不会递增,因此整个索引
循环对while
数组没有任何影响。这就是为什么当您尝试打印其值时会收到垃圾目录
- 解析“I”行并打印所读内容的示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int y;
FILE *data;
char action;
char line[100];
int counter = 0;
int index = 0;
struct contact{
char fName[10];
char lName[10];
};
struct contact directory[10];
if((data=fopen("hw6data.txt", "r")) != NULL){
while(fgets(line,sizeof(line),data)){
char s[2] = " ";
char *token = strtok(line, s);
while(token != NULL) {
if(strcmp(token,"I")==0) {
counter = 0;
}
if(counter==1) {
strcpy(directory[index].fName, token);
}
if(counter==2) {
strcpy(directory[index].lName, token);
index++;
}
counter++;
token = strtok(NULL, s);
}
}
}
for(y = 0; y < index; y++){
printf("%s ", directory[y].fName);
printf("%s\n", directory[y].lName);
}
fclose(data);
return 1;
}
#包括
#包括
#包括
int main(){
int-y;
文件*数据;
炭作用;
字符行[100];
int计数器=0;
int指数=0;
结构接触{
char-fName[10];
字符名称[10];
};
结构联系人目录[10];
if((data=fopen(“hw6data.txt”,“r”)!=NULL){
while(fgets(行、sizeof(行)、数据)){
字符s[2]=“”;
char*token=strtok(行,s);
while(令牌!=NULL){
if(strcmp(令牌,“I”)==0){
计数器=0;
}
如果(计数器==1){
strcpy(目录[index].fName,令牌);
}
如果(计数器==2){
strcpy(目录[index].lName,令牌);
索引++;
}
计数器++;
令牌=strtok(空,s);
}
}
}
对于(y=0;y
如果有帮助的话…很多错误:计数和索引始终为0,因此目录[y]永远不会受到影响…这:
(s==“I”)
绝大多数情况下肯定不会像你认为的那样做。而且要感谢这一点,因为不管怎样,它的变量是错误的,非常确定你的意思是行
而不是s
。没关系;不管怎样,它是错误的。我尝试过这样做,即使联系人的指针被注释掉了,但我仍然以seg错误结束胡言乱语。奇怪!它在我的机器上正确运行,valgrind不会报告错误。如果有超过10个“I”行,数组目录将溢出。gdb,valgrind可能会帮助您找到问题。我执行了这个程序,它对我来说也工作正常。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int y;
FILE *data;
char action;
char line[100];
int counter = 0;
int index = 0;
struct contact{
char fName[10];
char lName[10];
};
struct contact directory[10];
if((data=fopen("hw6data.txt", "r")) != NULL){
while(fgets(line,sizeof(line),data)){
char s[2] = " ";
char *token = strtok(line, s);
while(token != NULL) {
if(strcmp(token,"I")==0) {
counter = 0;
}
if(counter==1) {
strcpy(directory[index].fName, token);
}
if(counter==2) {
strcpy(directory[index].lName, token);
index++;
}
counter++;
token = strtok(NULL, s);
}
}
}
for(y = 0; y < index; y++){
printf("%s ", directory[y].fName);
printf("%s\n", directory[y].lName);
}
fclose(data);
return 1;
}