读取多个变量类型由单一行文件C [英] reading multiple variable types from single line in file C

查看:140
本文介绍了读取多个变量类型由单一行文件C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我一直在这个整天,不能为我的生命得到这个下来,也许你家伙可以提供帮助。我有一个文件,该文件内容如下

Alright I've been at this all day and can't for the life of me get this down, maybe you chaps can help. I have a file that reads as follows

1301,105515018,水手长,迈克尔R上。,ABC,123 =R01

1301,105515018,"Boatswain","Michael R.",ABC, 123,="R01"

1301,103993269,卡斯蒂利亚,迈克尔·JR,ABC,123 =R03

1301,103993269,"Castille","Michael Jr",ABC, 123,="R03"

1301,103993267,卡斯蒂利亚,卫,ABC,123 =R03

1301,103993267,"Castille","Janice",ABC, 123,="R03"

1301,104727546,Bonczek,克劳德,ABC,123 =R01

1301,104727546,"Bonczek","Claude",ABC, 123,="R01"

1301,104731479,克鲁兹,迈克的Akeem,ABC,123 =R01

1301,104731479,"Cruz","Akeem Mike",ABC, 123,="R01"

1301,105415888,Digiacomo,泉,ABC,123 =R02

1301,105415888,"Digiacomo","Stephen",ABC, 123,="R02"

1301,106034479,Annitto Grassis,苏珊,ABC,123 =R04

1301,106034479,"Annitto Grassis","Susan",ABC, 123,="R04"

1301,106034459,阿尔斯,基督徒,ABC,123 =R01

1301,106034459,"Als","Christian",ABC, 123,="R01"

这是我的code ...

And here is my code...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NAME 15
#define MAX_SUBSEC 3
#define N 128

//void printArr(struct *students);

struct student{

int term;
int id;
char lastname[MAX_NAME];
char firstname[MAX_NAME];
char subjectname[MAX_SUBSEC];
int catalog;
char section[MAX_SUBSEC];

}students[10];

int main(){

int term;
int id;
char lastname[MAX_NAME];
char firstname[MAX_NAME];
char sub[MAX_SUBSEC];
int cat;
char sec[MAX_SUBSEC];

char fname[N];
FILE *inputf;

printf("Enter the name of the text file: ");
scanf("%123s",fname);

strcat(fname,".txt");

inputf = fopen(fname,"r");

if (inputf == NULL){

     printf("I couldn't open the file for reading.\n");
     exit(0);

}
//TROUBLE HERE!

fscanf(inputf, "%d,%d,%[^,]s", &students[0].term, &students[0].id,students[0].lastname);

printf("%d\n", students[0].term);

printf("%d\n", students[0].id);

printf("%s\n", students[0].lastname);

/*for (j = 1 ; j <= 10-1 ; j++){

    for(k = 0 ; k <= 10-2 ; k++){

        if(students[k] > students[k+1]){

            temp = students[k];
            students[k] = students[k+1];
            students[k+1] = temp;

            }
        }
    }*/

fclose(inputf);

system("pause");

return 0;

}

void printArr(int a[], int tally){

int i;

for(i = 0 ; i < tally ; i++){

 printf("%d ", a[i]);

}

printf("\n");

}

我的目标是把这些值中的每一个文本文件,并将其输入给它所属的结构,随后该结构数组,其中,但我不能获得通过的第2个整数。

My objective is to take each one of those values in the text file and input it to where it belongs in the struct and subsequently the struct array, but I can't get passed the first 2 ints.

获取姓氏字符串,因为它是15个字符最多,它波及到之后它的第一个名字字符串,其余需要哪些字符是为了填补姓字符数组的需要。很显然,我不想这样。正如你可以看到我已经试过strtok的,但它没有做任何事情,不知道我必须做的,虽然我从来没有使用过它。也尝试过包括所有的变量引入的fscanf语句,但我要么得到的结果相同,也变得一团糟。正因为如此,我非常失落,我怎么得到这些值转换成他们属于?!

Getting the lastname string, because it is a max of 15 characters, it spills over into the first name string right after it and takes what remaining characters it needs in order to fill up the lastname char array. Obviously I do not want this. As you can see I have tried strtok but it doesnt do anything, not sure what I have to do though as I have never used it before. Also have tried just including all the variables into fscanf statement, but I either get the same output, or it becomes a mess. As it is, I am extremely lost, how do I get these values into the variables they belong?!

编辑:更新了我的code,我已经变得更远了一点,但不多。我现在可以打印出刚刚过去的名称,但不能更从那里远,我不能得到的名字字符串或任何变量超越它。

updated my code, I have gotten a little farther but not much. I can now print out just the last name but can not more farther from there, I cant get to the firstname string or any of the variables beyond it.

推荐答案

我有一个问题要问你......如果你想知道如何使用金刚石刀具,你试试吧,看看,还是你的咨询手册?这里的问题是不是你选择的结果,但您的选择本身。不管你信不信,我常常说我已经厌倦了重复自己的回答了这些问题。答案是所有的手册

I have a question for you... If you want to know how to use a diamond cutter, do you try it and see, or do you consult the manual? The problem here isn't the result of your choice, but your choice itself. Believe it or not, I have answered these questions so often that I'm tired of repeating myself. The answer is all in the manual.

2004年的POSIX scanf函数手动 - 或 POSIX2013分之2008版本 - 和答案<一个href=\"http://stackoverflow.com/questions/14472004/how-to-use-fgets-and-sscanf-for-integers-in-loop/14472455#14472455\">this问题,你就会有你没有做什么,你应该有所了解。即使的fscanf code应该使用断言辅助调试,以确保项目的数量看是正确的。

Read the POSIX 2004 scanf manual — or the POSIX 2008/2013 version — and the answer this question and you'll have some idea of what you're not doing that you should be. Even fscanf code should use assert as a debugging aid to ensure the number of items read was correct.

%[^,] S 这好像这里有一个错误。也许你的意思%[^,] 。在%[格式说明是一个不同的格式说明符的%S 格式说明,因此,在presumably错误的code有两个指令:%[^,] 取值。在取值指令告诉 scanf函数读取一个S并将其丢弃。

%[^,]s It seems as though there's a mistake here. Perhaps you meant %[^,]. The %[ format specifier is a different format specifier to the %s format specifier, hence in the presumably mistaken code there are two directives: %[^,] and s. The s directive tells scanf to read an 's' and discard it.

这篇关于读取多个变量类型由单一行文件C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆