Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. fstream help

fstream help

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    afrodriguez
    wrote on last edited by
    #1

    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...

    C 1 Reply Last reply
    0
    • A afrodriguez

      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...

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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++

      Q J 2 Replies Last reply
      0
      • C Christian Graus

        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++

        Q Offline
        Q Offline
        qfegd
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • C Christian Graus

          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++

          J Offline
          J Offline
          Jeremy Thornton
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • Q qfegd

            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

            J Offline
            J Offline
            Jeremy Thornton
            wrote on last edited by
            #5

            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 add in_file.ignore(255,'\n'); to ignore upto 255 characters at the end of the line and any new line. hope this helps? :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups