在csv中的线读取非常奇怪 [英] Getline reading in a csv very oddly

查看:111
本文介绍了在csv中的线读取非常奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取csv使用get行提取三个变量用逗号分隔。



我在第一行读得很好,但它放入了奇怪的换行符,并将格式发送到集群中。



这是我的代码:

  #includeheader.h

string student :: GetCourse(){
return course;
}

string student :: GetName(){
return name;
}

string student :: GetGrade(){
return grade;
}

void student :: setname(string n){
name = n;
}

void student :: setCourse(string c){
course = c;
}

void student :: setGrade(string g){
grade = g;
}
void sort(vector< student>& List){

student temp;
int first = 1;
int vectorLength = List.size() - 1;

for(int i = vectorLength; i> 0; i--){
first = i;
for(int j = 0; j if(List [j] .GetName()> List [first] .GetName())
first = j;
}
temp = List [first]
List [first] = List [i];
List [i] = temp;
}

}

void main(){
ifstream file;
矢量< student>学生列表;

file.open(short.txt);

while(!file.eof()){

file.ignore(8196,'\\\
');

string tempname,tempgrade,tempcourse =;

if(file!=\\\
){
getline(file,tempname,',')
getline(file,tempcourse,',');
getline(file,tempgrade,',');
}

student s;
s.setCourse(tempcourse);
s.setname(tempname);
s.setGrade(tempgrade);

StudentList.push_back(s);

}
// sort(StudentList);

for(int i = 0; i cout< StudentList [i] .GetName()< < StudentList [i] .GetCourse()< < StudentList [i] .GetGrade()< endl;
}
}

任何想法,我有一个真正的困难时期


b b $ b
  • 如果(file!=\\\
    比较是无意义的。

  • 成绩之后的分隔符不是',',它是'\\\
    '

  • while(!file.eof())不正确。只有在EOF已经发生之后才检查。您应该检查 getline()的返回值



  • / p>


    • 通常在C ++中,您可以 std :: ifstream file(short.txt); 。您不需要分别调用 open()

    • 您不需要初始化 std :: string 更改为。这会自动发生。即使你不得不这样做,你应该写成



      std :: string a =,b = ;



      如果你执行 std :: string a,b,c =something



    I am trying to read a csv using get line to extract three variables separated by commas. Name, Course, and Grade.

    I am reading in the first line fine but it puts in weird new line breaks and sends the format into a cluster.

    Here is my code :

    #include "header.h"
    
    string student::GetCourse() {
        return course;
    }
    
    string student::GetName() {
        return name;
    }
    
    string student::GetGrade() {
        return grade;
    }
    
    void student::setname(string n) {
        name = n;
    }
    
    void student::setCourse(string c) {
        course = c;
    }
    
    void student::setGrade(string g) {
        grade = g;
    }
    void sort (vector <student> &List) {
    
        student temp;
        int	first = 1;
        int vectorLength = List.size() - 1;
    
        for (int i = vectorLength; i > 0; i--) {
        	first = i;
        	for (int j = 0; j < i; j++) {
        		if (List[j].GetName() > List[first].GetName())
        		first = j;
        	}
        	temp = List[first];
        	List[first] = List[i];
        	List[i] = temp;
        }
    
    }
    
    void main () {
        ifstream file;
        vector <student> StudentList;
    
        file.open("short.txt");
    
        while (!file.eof()) {
    
        	file.ignore(8196,'\n');
    
        	string tempname, tempgrade, tempcourse = "";
    
        	if (file != "\n") {
        		getline(file, tempname, ',');
        		getline(file, tempcourse, ',');
        		getline(file, tempgrade, ',');
        	}
    
        	student s;
        	s.setCourse(tempcourse);
        	s.setname (tempname);
        	s.setGrade (tempgrade);
    
        		StudentList.push_back(s);
    
        }
        //sort (StudentList);
    
        for (int i = 0; i < StudentList.size(); i++) {
        	cout << StudentList[i].GetName() << " " << StudentList[i].GetCourse() << " " << StudentList[i].GetGrade() << endl;
        }
    }
    

    Any ideas, I am having a real hard time on reading in this file.

    解决方案

    Well, here goes

    • if (file != "\n") comparison is nonsensical. It does not do what you think it does.
    • Your delimiter after the grade is not ',', it is '\n'.
    • while (!file.eof()) is incorrect. That only checks the EOF after it has already happened. You should check the return value of your getline() instead

    Also

    • usually in C++ you do std::ifstream file("short.txt");. You don't need to call open() separately.
    • You don't need to initialize std::string to "". That happens automatically. Even if you had to do that, then you should have written

      std::string a = "", b = "", c = "";.

      If you do std::string a, b, c = "something" then only c is initialized to something.

    这篇关于在csv中的线读取非常奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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