如何从文本文件中读取一行并将它们分隔在不同的变量中?
How to read a line from text file and separate them in different variables?
这是我的示例文本文件
enter image description here
1 Class Physics American 3.6 5 Maria Garcia 1-541-754-3010
2 Class Chemical Australian 3.5 4 Maria Hernandez 1-541-754-3233
我有一组数组和变量。
typedef struct
{
double current;
unsigned int subject;
}CGPA;
typedef struct
{
char program[40];
char state[50];
char name[50];
char contact[50];
string studentid;
CGPA a;
}INFOR;
如何将它们存储在不同的变量中供以后处理使用?
这是我的代码的一部分,但它无法获得正确的值并将它们从我的 txt 文件存储到我的结构数组中:
for( int i = 0; getline(readfile, line); i++)
{
//getline(readfile,student[i].id, '[=12=]');
//getline(readfile,student[i].a.subject, '[=12=]');
//strcpy(student[i].subject, temporary_s);
readfile >> student[i].studentid >> student[i].program >> student[i].state >> student[i].a.current >> student[i].a.subject >> student[i].name >> student[i].contact; //This is my code cannot read my text file correctly
++line_count;
}
这是我的示例输出:
enter image description here
我想删除特定行,更新我文件的特定行并显示较低或较高的 CGPA,这就是为什么我需要将我的文本文件值恢复到数组以供以后处理使用。
此代码无法以正确的方式读取我的文件,我不知道如何处理它
readfile >> student[i].studentid >> student[i].program >> student[i].state >> student[i].a.current >>student[i].a.subject >> student[i].name >> student[i].contact; //This is my code cannot read my text file correct
我不知道为什么你需要一个单独的 CGPA 数据结构,而你可以将它与其余部分一起放入 INFOR 中,但我已经离开了。我确实将所有字符数组更改为 std::string,因为它们作为字符数组没有明显优势。我将 INFOR 的数组更改为 std::vector 因为它作为数组没有明显的优势。如果结构确实需要按照您的方式进行布局,请告诉我,因为代码需要更改。
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct CGPA
{
double current;
unsigned int subject;
};
struct INFOR
{
std::string program;
std::string state;
std::string name;
std::string contact;
std::string studentid;
CGPA a;
};
bool testing = true;
main() {
if(testing) {
std::ofstream writeFile("file.txt");
writeFile << "1\tClass Physics\tAmerican\t3.6\t5\tMaria Garcia\t1-541-754-3010\n"
<< "2\tClass Chemical\tAustralian\t3.5\t4\tMaria Hernandez\t1-541-754-3233\n";
writeFile.close();
}
std::vector<INFOR> students;
std::ifstream readfile("file.txt");
std::string line;
while(std::getline(readfile, line))
{
std::stringstream ss(line);
INFOR student;
std::string column;
getline(ss, column, '\t'); student.studentid = column;
getline(ss, column, '\t'); student.program = column;
getline(ss, column, '\t'); student.state = column;
ss >> student.a.current;
ss >> student.a.subject;
getline(ss, column, '\t'); student.name = column;
getline(ss, column, '\t'); student.contact = column;
students.push_back(student);
}
readfile.close();
int line_count = students.size();
if(testing) {
std::cout << line_count << " lines";
}
return line_count;
}
这是我的示例文本文件 enter image description here
1 Class Physics American 3.6 5 Maria Garcia 1-541-754-3010
2 Class Chemical Australian 3.5 4 Maria Hernandez 1-541-754-3233
我有一组数组和变量。
typedef struct
{
double current;
unsigned int subject;
}CGPA;
typedef struct
{
char program[40];
char state[50];
char name[50];
char contact[50];
string studentid;
CGPA a;
}INFOR;
如何将它们存储在不同的变量中供以后处理使用?
这是我的代码的一部分,但它无法获得正确的值并将它们从我的 txt 文件存储到我的结构数组中:
for( int i = 0; getline(readfile, line); i++)
{
//getline(readfile,student[i].id, '[=12=]');
//getline(readfile,student[i].a.subject, '[=12=]');
//strcpy(student[i].subject, temporary_s);
readfile >> student[i].studentid >> student[i].program >> student[i].state >> student[i].a.current >> student[i].a.subject >> student[i].name >> student[i].contact; //This is my code cannot read my text file correctly
++line_count;
}
这是我的示例输出: enter image description here 我想删除特定行,更新我文件的特定行并显示较低或较高的 CGPA,这就是为什么我需要将我的文本文件值恢复到数组以供以后处理使用。
此代码无法以正确的方式读取我的文件,我不知道如何处理它
readfile >> student[i].studentid >> student[i].program >> student[i].state >> student[i].a.current >>student[i].a.subject >> student[i].name >> student[i].contact; //This is my code cannot read my text file correct
我不知道为什么你需要一个单独的 CGPA 数据结构,而你可以将它与其余部分一起放入 INFOR 中,但我已经离开了。我确实将所有字符数组更改为 std::string,因为它们作为字符数组没有明显优势。我将 INFOR 的数组更改为 std::vector 因为它作为数组没有明显的优势。如果结构确实需要按照您的方式进行布局,请告诉我,因为代码需要更改。
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct CGPA
{
double current;
unsigned int subject;
};
struct INFOR
{
std::string program;
std::string state;
std::string name;
std::string contact;
std::string studentid;
CGPA a;
};
bool testing = true;
main() {
if(testing) {
std::ofstream writeFile("file.txt");
writeFile << "1\tClass Physics\tAmerican\t3.6\t5\tMaria Garcia\t1-541-754-3010\n"
<< "2\tClass Chemical\tAustralian\t3.5\t4\tMaria Hernandez\t1-541-754-3233\n";
writeFile.close();
}
std::vector<INFOR> students;
std::ifstream readfile("file.txt");
std::string line;
while(std::getline(readfile, line))
{
std::stringstream ss(line);
INFOR student;
std::string column;
getline(ss, column, '\t'); student.studentid = column;
getline(ss, column, '\t'); student.program = column;
getline(ss, column, '\t'); student.state = column;
ss >> student.a.current;
ss >> student.a.subject;
getline(ss, column, '\t'); student.name = column;
getline(ss, column, '\t'); student.contact = column;
students.push_back(student);
}
readfile.close();
int line_count = students.size();
if(testing) {
std::cout << line_count << " lines";
}
return line_count;
}