"C + + Stream class library and input output" experiment Seven

Source: Internet
Author: User
Tags setf

  1. 1. Basic exercises (1) Textbook Exercise 11-7 (2) Textbook Exercise 11-3 (3) Textbook Exercise 11-4

    2. The application exercise (1) is known to have class list file List.txt (see Experiment 7 Accessory Pack). Write an application implementation randomly draw a point 5 students, display the results on the screen, and also write the results to the file roll.txt. ① Write a program to achieve the basic functional requirements of the topic. (Must do) ②****** choose to do ******) ① in the implementation of the basic functions to improve and expand, making this name application more flexible and more convenient. For example: a) from the keyboard input class file name, support for different classes of the name operation; b) Enter the file name from the keyboard to hold the name result. More flexibility to automatically get the current system date as a file name, such as 20180612.txt. (If you want finer granularity, the file name can go to the hour and minute levels); c) random extraction of the number of people is not fixed, keyboard keys to control when the end of the point; d) through the menu and Program function Module division, or class design and implementation, to make a more perfect application, and so on. (2) Statistics English text file character number, Word number, line number, the file name is entered by the keyboard. ① Write C + + program to achieve the basic functional requirements of the topic. (Must do) ②****** choose to do ****** a) to provide the menu, by the user to select the statistical content; b) think when the content of the text is too large, ① the implementation of the program can be competent to achieve rapid statistics? Is there any further improvement in algorithmic and processing logic?

  2. Answer:
    1. 11-3
      1#include <iostream>2#include <fstream>3 using namespacestd;4 intMain () {5Ofstream out("Test1.txt"); 6     if(! out) {7cout <<"fail to open."<<Endl;8         return 1;9     }Ten       out<<"The file was written successfully! "<<Endl; One       out. Close (); A      -     return 0; -}

    2. 11-4:

      1#include <iostream>2#include <fstream>3#include <string>4 using namespacestd;5 intMain () {6 7     strings;8Ifstreaminch("Test1.txt");9     if(!inch) {Tencout <<"fail to open."<<Endl; One         return 1; A     } -     inch>>s; -cout <<s; the     inch. Close (); -     return 0; -}

    3. 11-7

      //p510, Ex11-7#include <iostream>using namespacestd;intMain () {ios_base::fmtflags original_flags=cout.flags ();//Save the current parameter settingscout<<812<<'|'; COUT.SETF (Ios_base::left, Ios_base::adjustfield);//output left JustifiedCout.width (Ten);//sets the width of a field, but only works on the first outputcout <<813<<815<<'\ n'; COUT.UNSETF (Ios_base::adjustfield);//Clear left-to-GillCout.precision (2);//two-bit floating point numberCOUT.SETF (ios_base::uppercase|ios_base::scientific);//Scientific Counting Methodcout <<831.0;    Cout.flags (Original_flags); //restore the original format settings        return 0;} 

    4. I read the paper from the teacher in three different ways, but I can't read it in this question. The test output is a space garbled. I put the list.txt file and the code in the same directory, but it is not read. Then I wrote myself a new Test1.txt file, which reads the output normally. Is there any special input or special characters for the documents given by the teacher? Be sure to read the file into this list.txt file is not what knowledge is being examined? Unfortunately, I did not successfully read into the file Ah!!! The following gives me the file I created myself to run
      1#include <iostream>2#include <string>3#include <cstdlib>4#include <fstream>5 using namespacestd;6 structstudent{7         stringorder;8       stringXuehao;9         stringname;Ten         stringclassname;  One }; A intMain () { -Student s[ the]; -Ifstream infile ("Test1.txt"); the     if(!infile)//test is open successfully -     {   -cerr<<"Open error!"<<Endl;  -        return 0;  +     }   -     /*fetching file information with row reads*/ +     /*int i=0; A Char c[83]; at       - While (!infile.eof ()) { - Infile.getline (c,83); - cout<<c[i]<<endl; - i++; -      } in Infile.close (); File reads with row read-in*/  -       to       +       /*read from the Reading method of employee's salary in the courseware*/   -   /*While (!infile.eof ()) the     {   * Infile.read (reinterpret_cast<char*> (&s), sizeof (s)); can only binary be read like this?  $ if (infile) {Panax Notoginseng cout << s.order << "<< S.xuehao << Endl; -         } the     }   + infile.close ();*/ A      the      intI=0; +      while(infile>>s[i].order>>s[i].xuehao>>s[i].name>>s[i].classname) -     { $i++; $     }  - infile.close (); -     intA=1; theOfstream out("Roll.txt"); -      for(intI=1; i<=5;++i)Wuyi     { theA=rand ()%i+1; -cout<<s[a].order<<" "<<s[a].xuehao<<" "<<s[a].name<<" "<<s[a].classname<<Endl; Wu          out<<s[a].order<<" "<<s[a].xuehao<<" "<<s[a].name<<" "<<s[a].classname<<Endl; -     } About      out. Close (); $      -    return 0; -}

        1. The final blue part is also a line of output, because I define the s array is 100, but there are only 5 names in the file, so the output of one of the items that have no value.

        2. The files are written as follows
        3. Use the List.txt in experiment seven to generate the following:
        4. SYSTEMTIME SYS;
          Getlocaltime (&sys);
          Cout<<sys.wyear<<sys.wmonth<<sys.wday<<sys.whour<<sys.wminute<<sys.wsecond <<endl; can read the current time, the header file #include<windows.h> My idea is to force time into string, after the string is concatenated, form a string h and name the file with H. Forced type conversion failed.

    5. Read a Word
      #include <string.h>#include<fstream>#include<iostream>using namespacestd;intMain () {Ofstream out("words");  out<<"You is so much kind!"<<Endl;  out<<"Guss This can SUCCESSD"<<Endl;  out. Close ();//Create a file manually      /*cout<< "Please enter filename:\n";//Enter the file in the Command line window string filename;      cin>>filename; Ifstream in (Filename.c_str ());*/Ifstreaminch("words"); Longlinenum=0, chnum=0, wordnum=0; Charstr[ +];  while(inch. getline (STR, +)){         for(intI=0; I<strlen (str); i++) {Chnum++; if(str[i]==' '|| str[i]==','|| str[i]=='!') Wordnum++; } linenum++; } cout<<"number of rows:"<<linenum<<endl<<"number of characters:"<<chnum<<endl<<"number of words:"<<wordnum<<Endl; inch. Close (); return 0;} 

"C + + Stream class library and input output" experiment Seven

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.