简体   繁体   English

将从格式化文本文件读取的文本数据存储到链表

[英]Store text data read from a formatted text file to a linked list

I'm working on a project for a Student Course Registration System.我正在为学生课程注册系统做一个项目。 I'm having problems reading data from a text file and storing it in singly linked list, which has to get updated every time a new student is added.我在从文本文件读取数据并将其存储在单链表中时遇到问题,每次添加新学生时都必须更新。 The data is stored in an formatted way.数据以格式化的方式存储。 The problem is my struct has type char variables, so it gives me as Assignment Error.问题是我的结构有类型char变量,所以它给了我赋值错误。

The struct is defined as:结构体定义为:

struct Student {
  char stdID[10];
  char stdName[30];
  char stdSemester[5];
  Student  *next; } *Head, *Tail;

The code that saves the struct is:保存结构体的代码是:

// For Saving: 
            SFile << std->stdID << '\t' << std->stdName << '\t' << std->stdSemester << '\n';

The code to read the text file and display the struct is:读取文本文件并显示结构的代码是:

// Display:
system("cls");
cout << "\n\n\n";
cout << "\t\t\t\t           LIST OF COURSES" << endl;
cout << "\t\t\t   ====================================================\n" << endl;
cout << "\t" << "ID" << "\t" << setw(15) << "Course Name" << "\n\n";

// Initialize:
char ID[10];
char Name[30];
char Sem[5]; 
ifstream SFile("StudentRecord.txt");
Student *Temp = NULL;

while(!SFile.eof()) {

    // Get:
    SFile.getline(ID, 10, '\t');
    SFile.getline(Name, 30, '\t');
    SFile.getline(Sem, 5, '\t');

    Student *Std = new Student;   //<======== OUCH! Assignment error here
    //node*c=new node;

    // Assign:
    Std->stdID = *ID;

    if (Head == NULL) {
        Head = Std;
    } 
    else {
        Temp = Head;
        {
            while ( Temp->next !=NULL ) {
                Temp=Temp->next;
            }
            Temp->next = Std;
        }
    }
}
SFile.close();
system("pause"); }

PS: I'm Having Problem at Assign Comment; PS:我在分配评论时遇到问题;

Will I have to change data type and make the whole project in string ?我是否必须更改数据类型并将整个项目设为string I preferred char because I was able to format the output, and in string i'm sure it reads line by line, so I wont be able stores values from single line.我更喜欢char因为我能够格式化输出,并且在string我确定它逐行读取,所以我不能从单行存储值。

To use strings ?使用字符串?

If IDs would be std:string , you could do:如果 ID 是std:string ,你可以这样做:

Std->stdID = ID;

And you could use std::getline() :你可以使用std::getline()

getline(SFile, ID, '\t');

You wouldn't have to worry about maximum length, but you could still decide to check the lngth of the string and shorten it if necessary.您不必担心最大长度,但您仍然可以决定检查字符串的长度并在必要时缩短它。

Or not to use strings ?还是不使用字符串?

But if you prefer (or have to) to use char[] instead, then you need to use the strncpy() for doing assignments :但是,如果您更喜欢(或必须)使用char[]来代替,那么您需要使用strncpy()进行赋值:

strncpy( Std->stdID, ID, 10 );  // Std->stdID = *ID;

Honnestly, in the 21st century, I'd go for std::string, and not stick to the old char[] that date back to the 70s...老实说,在 21 世纪,我会选择 std::string,而不是坚持可以追溯到 70 年代的旧char[] ......

File loop文件循环

It's unrelated, but you should never loop on eof :这是无关的,但你永远不应该在eof循环:

while (SFile.getline(ID, 10, '\t') 
     && SFile.getline(Name, 30, '\t')  && SFile.getline(Sem, 5, '\n') {
   ...
}

Why ?为什么 ? Look here for more explanations在这里查看更多解释

And by the way, your last getline() should certainly look for '\\n' as delimiter, according to your writing function.顺便说一句,根据您的写作功能,您的最后一个getline()当然应该寻找'\\n'作为分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM