fstream help
-
I need help using fstream... i need to read a text file that has multiple lines, each line has the syntax: 'fname lname 01 02 03 04 05 06 07 08 08 10' that is a first name space last name space and 10 test scores (separated by spaces). I then have to move all the data from one file to another except i have to add the average of the 10 scores to the end of each line... I can read the full text and display as it is from one file to the other. The problem is i can't read just the numbers in order to get the average...here's the code i have so far... #include #include #include #include using namespace std; #define INPUTFILE "HW660-In.txt" #define OUTPUTFILE "HW660-Out.txt" void rwFiles(ifstream& inSource, ofstream& outSource); bool goOn(); void main() { dispHeader(); do { ifstream inStudentRecord; ofstream outStudentRecord; inStudentRecord.open(INPUTFILE); if (inStudentRecord.fail()) { cout<<"Opening input file failed.\n"; exit(1); }; outStudentRecord.open(OUTPUTFILE); if (outStudentRecord.fail()) { cout<<"Opening output file failed.\n"; exit(1); }; rwFiles(inStudentRecord, outStudentRecord); inStudentRecord.close(); outStudentRecord.close(); }while(goOn()); } void dispHeader() { void rwFiles(ifstream& inSource, ofstream& outSource) { char next; int iGrade = 0, oGrade = 0; inSource.get(next); while (!inSource.eof()) { if(next == '\n') { cout<<" The End"; //this adds "The End" at the end of every line. }; cout<>calcMore; switch (calcMore) { case 'Y': case 'y': ;break; case 'N': case 'n': default: goOn = false; } return goOn; } So basically all my code does is reads exactly what is in the input file and displays it to the screen as well as copies it to the output file. noob in need of help...
-
I need help using fstream... i need to read a text file that has multiple lines, each line has the syntax: 'fname lname 01 02 03 04 05 06 07 08 08 10' that is a first name space last name space and 10 test scores (separated by spaces). I then have to move all the data from one file to another except i have to add the average of the 10 scores to the end of each line... I can read the full text and display as it is from one file to the other. The problem is i can't read just the numbers in order to get the average...here's the code i have so far... #include #include #include #include using namespace std; #define INPUTFILE "HW660-In.txt" #define OUTPUTFILE "HW660-Out.txt" void rwFiles(ifstream& inSource, ofstream& outSource); bool goOn(); void main() { dispHeader(); do { ifstream inStudentRecord; ofstream outStudentRecord; inStudentRecord.open(INPUTFILE); if (inStudentRecord.fail()) { cout<<"Opening input file failed.\n"; exit(1); }; outStudentRecord.open(OUTPUTFILE); if (outStudentRecord.fail()) { cout<<"Opening output file failed.\n"; exit(1); }; rwFiles(inStudentRecord, outStudentRecord); inStudentRecord.close(); outStudentRecord.close(); }while(goOn()); } void dispHeader() { void rwFiles(ifstream& inSource, ofstream& outSource) { char next; int iGrade = 0, oGrade = 0; inSource.get(next); while (!inSource.eof()) { if(next == '\n') { cout<<" The End"; //this adds "The End" at the end of every line. }; cout<>calcMore; switch (calcMore) { case 'Y': case 'y': ;break; case 'N': case 'n': default: goOn = false; } return goOn; } So basically all my code does is reads exactly what is in the input file and displays it to the screen as well as copies it to the output file. noob in need of help...
If you know the format of the file, you can use the << operator to read the file into the appropriate variable types, which in this case is two strings, and 10 numeric types. As it stands, you're reading a char at a time. The real alternative to what I've suggested is to use getline and then munch the string to extract the data by looking for spaces and using substr and atoi to convert to ints. Christian Graus - Microsoft MVP - C++
-
If you know the format of the file, you can use the << operator to read the file into the appropriate variable types, which in this case is two strings, and 10 numeric types. As it stands, you're reading a char at a time. The real alternative to what I've suggested is to use getline and then munch the string to extract the data by looking for spaces and using substr and atoi to convert to ints. Christian Graus - Microsoft MVP - C++
In those situation stringstream can be cool! http://www.cplusplus.com/ref/iostream/stringstream/[^] Here is the code to read one line in your file and to compute the average score. std::ifstream ifs("FileName", std::ios::in); char buffer[255]; ifs.getline(buffer, 255); std::istringstream iss(buffer); char n1[255]; char n2[255]; double grade, mean=0.0; int nGrade = 0; iss >> n1 >> n2; for(; iss >> grade; ++nGrade) mean += grade; mean /= nGrade; ifs.close(); // end Now figure a way to do this for the whole file :) Eric Premature optimization is the root of all evil
-
If you know the format of the file, you can use the << operator to read the file into the appropriate variable types, which in this case is two strings, and 10 numeric types. As it stands, you're reading a char at a time. The real alternative to what I've suggested is to use getline and then munch the string to extract the data by looking for spaces and using substr and atoi to convert to ints. Christian Graus - Microsoft MVP - C++
Christian Graus wrote:
you can use the << operator to read
correction: operator >> is overloaded to read the inbuilt types from an input stream operator << is for writing to an output stream
-
In those situation stringstream can be cool! http://www.cplusplus.com/ref/iostream/stringstream/[^] Here is the code to read one line in your file and to compute the average score. std::ifstream ifs("FileName", std::ios::in); char buffer[255]; ifs.getline(buffer, 255); std::istringstream iss(buffer); char n1[255]; char n2[255]; double grade, mean=0.0; int nGrade = 0; iss >> n1 >> n2; for(; iss >> grade; ++nGrade) mean += grade; mean /= nGrade; ifs.close(); // end Now figure a way to do this for the whole file :) Eric Premature optimization is the root of all evil
qfegd wrote:
In those situation stringstream can be cool!
and (IMHO) utterly superfluous... there is already and input stream open in the form of the input file stream why read a line convert it to another input stream and then use operator>> on that stream:confused: as the input text file is rigorously defined why not use >> directly on the input file stream? operator>> is overloaded for the inbuilt types & defaults to consuming white space and newlines (thanks Bjarne) thus...
char n1[255]; char n2[255]; double total, mean, grade; int count = 10; //specified in the input file format ifstream in_file(IN_NAME); //checking omit'd for clarity while(!in_file.eof()) { in_file >> n1 >> n2; total = 0.0; for(int i = 0, i < count, ++i) { in_file >> grade; total += grade; } mean = total / count; }
i will leave writing to the output file for you (it can all be fitted into the above loop in 3 lines) if your input file might be dirty you could addin_file.ignore(255,'\n');
to ignore upto 255 characters at the end of the line and any new line. hope this helps? :)