Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文本文件读取记录 < >我是C++新手,但我正在做很多练习。_C++_Search_Text - Fatal编程技术网

从文本文件读取记录 < >我是C++新手,但我正在做很多练习。

从文本文件读取记录 < >我是C++新手,但我正在做很多练习。,c++,search,text,C++,Search,Text,这是我的问题,有人可以看看我的源代码,并引导我在这里正确的方向请 这就是我想做的 程序应该能够读取带有记录的文本文件 在里面。(那样做了) 我还想使用文本文件中的字符串搜索记录 (没有这样做) 此外,使用十进制从最高到最低对记录进行排序 文本文件中的数字或双精度。我想用气泡排序法 功能 这是我的代码 #include <iostream> #include <fstream> #include <string> using namespace std; //

这是我的问题,有人可以看看我的源代码,并引导我在这里正确的方向请

这就是我想做的

  • 程序应该能够读取带有记录的文本文件 在里面。(那样做了)
  • 我还想使用文本文件中的字符串搜索记录 (没有这样做)
  • 此外,使用十进制从最高到最低对记录进行排序 文本文件中的数字或双精度。我想用气泡排序法 功能
  • 这是我的代码

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    //double gpa;
    //string
    
    int main () 
     {
      string line;
      ifstream myfile ("testfile.txt");
      if (myfile.is_open())
     {
        while ( myfile.good() )
     {
          getline (myfile,line);
          cout << line << endl;
    
     }
        myfile.close();
     }
    
    else cout << "Unable to open file"; 
    
    char c;
    cout<<"\n enter a character and enter to exit: ";
    cin>>c;
    return 0;
    }
    
    请注意,
    getline(myfile,line)
    可能会失败,因此在这种情况下使用
    line
    的值是不正确的:

    while (myfile.good())
    {
        getline(myfile, line);
        cout << line << endl;
    }
    
    while(myfile.good())
    {
    getline(myfile,line);
    
    Cuth@ RADU,这不是什么问题,如果是家庭作业:谢谢输入。我不是C++中的诗句,我对这个编程语言很陌生,即使是像它所说的那样基本。我已经给了它很多想法,尝试不同的变体。
    while (myfile.good())
    {
        getline(myfile, line);
        cout << line << endl;
    }
    
    while (getline(myfile, line))
    {
        cout << line << endl;
    }