从CSV文件存储课程记录,结构存储不正确

时间:2018-08-04 20:59:11

标签: c memory dynamic structure allocation

所以我有这个程序,该程序应以CSV文件的形式读取输入,

CSV:

programming, 03, 60, 141, 1, 30, 0, W, 2015

algebra, 03, 62, 102, 2, 0, 0, S, 2013

religion, 08, 98, 938, 1, 30, 90, W, 2015

我们应该阅读一下结构,然后将打开一个switch/case菜单,您可以在其中添加课程数据,搜索课程并在表格中显示该课程。现在,我在将每个读取的输入存储到其给定的结构变量中时遇到问题。

我已经分配了char []个值来存储这些值,正如您所看到的,我在获得它们后立即将它们打印出来。问题是当我尝试使用strcpy()函数将每个给定变量加载到struct中时,字符串值不会被复制。我尝试查看多个不同的示例,觉得自己已经走到了尽头。我之前在其他程序中使用过这种逻辑。我猜最大的区别是buff char指针的大小为malloc的{​​{1}}。截至目前,structcourseID仅在打印中,这使我相信这是存储字符串的问题。我目前不知道该如何解决这个问题。

courseSections

1 个答案:

答案 0 :(得分:1)

使用sscanf函数看起来像这样。

void loadCourseInfo() //To read all data from the input file (courseInfo.csv) and format and store
//them in an array of strutcures
{
    /* FileStream for the Library File */
    FILE *fptr;
    char buf[100] = "";

    if ( ( fptr = fopen(filename, "r" ) ) == NULL ) //Reading a file
    {
        printf( "File could not be opened.\n" );
        return;
    }

    int i = 0;
    char nameX[40];

    char facX[3];
    char subX[3];
    char levX[4];

    int sec1, sec2, sec3;
    char semX[2];
    char yearX[6];

    while ( i < 3 && fgets(buf, 100, fptr) != NULL)
    {
        if ( 9 == sscanf ( buf, " %39[^,], %2[^,], %2[^,], %3[^,], %d, %d, %d, %1[^,], %4s"
        , nameX, facX, subX, levX, &sec1, &sec2, &sec3, semX, yearX)) { 
            printf("%s ", nameX);
            printf("%s ", facX);
            printf("%s ", subX);
            printf("%s ", levX);
            printf("%d ", sec1);
            printf("%d ", sec2);
            printf("%d ", sec3);
            printf("%s ", semX);
            printf("%s ", yearX);
            printf("\n");

            courseList[i].courseID = i + 1;
            strcpy(courseList[i].courseName, nameX);
            strcpy(courseList[i].courseCode, facX);
            strcpy(courseList[i].Term, semX);
            courseList[i].courseSections[0] = sec1;
            courseList[i].courseSections[1] = sec2;
            courseList[i].courseSections[2] = sec3;     

            i++;
        }
    }
    fclose(fptr);
}

这可能无法解决所有问题。

这一直有效...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define filename "courseInfo.csv"

struct CourseInfo{
    int courseID;
    char courseName[40];
    char courseCode[10];
    char Term [6];
    int courseSections[3];
};

typedef struct CourseInfo courseInfo; //optional
courseInfo courseList[3];

void loadCourseInfo() //To read all data from the input file (courseInfo.csv) and format and store
//them in an array of strutcures
{
    /* FileStream for the Library File */
    FILE *fptr;
    char buf[100] = "";
    char courseCodeX[10];
    char TermX[6];

    if ( ( fptr = fopen(filename, "r" ) ) == NULL ) //Reading a file
    {
        printf( "File could not be opened.\n" );
        return;
    }

    int i = 0;
    char nameX[40];

    char facX[3];
    char subX[3];
    char levX[4];

    int sec1, sec2, sec3;
    char semX[2];
    char yearX[6];

    while ( i < 3 && fgets(buf, 100, fptr) != NULL)
    {
        if ( 9 == sscanf ( buf, " %39[^,], %2[^,], %2[^,], %3[^,], %d, %d, %d, %1[^,], %4s"
        , nameX, facX, subX, levX, &sec1, &sec2, &sec3, semX, yearX)) {
            printf("%s ", nameX);
            printf("%s ", facX);
            printf("%s ", subX);
            printf("%s ", levX);
            printf("%d ", sec1);
            printf("%d ", sec2);
            printf("%d ", sec3);
            printf("%s ", semX);
            printf("%s ", yearX);
            printf("\n");

            strcpy(courseCodeX, facX);
            strcat(courseCodeX, subX);
            strcat(courseCodeX, "-");
            strcat(courseCodeX, levX);
            printf("%s\n", courseCodeX);

            strcpy(TermX, semX);
            strcat(TermX, yearX);
            printf("%s\n", TermX);

            courseList[i].courseID = i + 1;
            strcpy(courseList[i].courseName, nameX);
            strcpy(courseList[i].courseCode, facX);
            strcpy(courseList[i].Term, semX);
            courseList[i].courseSections[0] = sec1;
            courseList[i].courseSections[1] = sec2;
            courseList[i].courseSections[2] = sec3;

            i++;
        }
    }
    fclose(fptr);
}

void displayCourseInfo() //Print a table indicating course information
{
    printf("ID                          Name           Code  Term Sections\n");
    printf("--------------------------------------------------------------\n");
    for(int i = 0; i < 3; i++)
    {
        printf("%2d %39s %3s %4s  %2d,%2d,%2d \n", courseList[i].courseID, courseList[i].courseName, courseList[i].courseCode, courseList[i].Term, courseList[i].courseSections[0], courseList[i].courseSections[1], courseList[i].courseSections[2]);
    }
}

void switchCaseMenu(int selection)
{
    int menuActive = 1;
    while(menuActive)
    {

        printf("Assignment 3\n");
        printf("------------\n");
        printf("1. Add a new course\n");
        printf("2. Seearch for a course ID\n");
        printf("3. Search for a course by course Name\n");
        printf("4. Search for all courses by term\n");
        printf("5. Display course data\n");
        printf("6. Save course data\n");
        printf("7. Exit\n");

        printf("Please enter a selection: ");
        scanf("%i", &selection);

        switch(selection)
        {
            case 7:
                menuActive = 0;
                break;
            case 6:
                printf("Selecteed - Save Course Data\n");

                break;
            case 5:
                printf("Selected - Display Course Data\n");
                displayCourseInfo();

                break;
            case 4:
                printf("Selected - Searfh for All Courses By Term\n");

                break;
            case 3:
                printf("Selected - Search for Course by Course Name\n");

                break;
            case 2:
                printf("Selected - Search for Course ID\n");

                break;
            case 1:
                printf("Selected - Add a New Course\n");
                //addCourseInfo(courseInfo, );

                break;
            default:
                printf("Invalid Input!\n");
        }
    }
    printf("You Have Quit!\n");

}

int main()
{
    loadCourseInfo();

    int select = 0;
    switchCaseMenu(select);

    return 0;
}