请更正以下的CSV程序 谢了 c++

请更正以下的CSV程序 谢了 c++,第1张

// 更正好了。现在可以读取一个全数塌告凳字的CSV文件,并统计每行的平均值了。输友搏入文件的最后结尾是有空行无空行都能适应。尽量团旅保持了原来的代码风格,只加了必要的改动。

#include<iostream>

#include<iomanip>

#include<fstream>

#include<string>

#include<cstdlib>

#include<sstream>

const int MaxEntries = 120// max length of characters in each (actually 49 since last byte is null '\0'

int grades[500][20] = {0}

int grades_columns[500] = {0}// 记录每一行的实际个数

using namespace std

string int2string( int value )

{

stringstream strStream

strStream <<value

return strStream.str()

}

int main()

{

cout<<"These are the students quizz averages:"<<endl

cout<<"------------------------------------------------------"<<endl

char s[MaxEntries]// declare a char array to hold the string of characters read

ofstream outFile("c:\\avgGrades.csv")

ifstream inFile ("c:\\grades2.csv")// open the file specified by the path

int QuizzEntry = 0// count how many student records read

int columns// the scope is now greater than including in the while loop

while(!inFile.eof() )

{

inFile.getline(s, MaxEntries)

columns = 0

char* p = strtok(s, ",")// separate into substrings by comma delimiter and get the first token

if ( p )

{

while(p!=NULL)

{

grades[QuizzEntry][columns++] = atoi(p)

p=strtok(NULL, ",")// get the next token

}

grades_columns[QuizzEntry] = columns

++QuizzEntry// increment the rows

}

}

for(int rows=0rows <QuizzEntryrows++)

{

string str=""

cout<<rows+1<<" )"

int sum=0

double average

for(int c=0c <grades_columns[rows]c++)

{

cout<<setw(6)<<grades[rows][c]

sum+= grades[rows][c]

str+= int2string(grades[rows][c]) +","

}

average=(double)sum/grades_columns[rows]

str+=int2string(average)

outFile<<str<<endl

cout<<"\t average="<<average<<endl

}

inFile.close()

return 0

}

不要用csv库, 直接用pandas

pandas读取完csv文件以后,哪厅直接加一列,或者直接修改列的内容

至于怎么迅者补全字符串,可以去搜“python字符串补齐亩缓薯”


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12320318.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存