Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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++ 从文件读入时,如何阻止空格显示为回车符? void displayFile() { char userInput[20];//用于存储用户键入的输入命令。 ifstream ip(“D:\\H Drive ITT\\Level 8 Degree\\Soft_C++_File_Input_Stl - Fatal编程技术网

C++ 从文件读入时,如何阻止空格显示为回车符? void displayFile() { char userInput[20];//用于存储用户键入的输入命令。 ifstream ip(“D:\\H Drive ITT\\Level 8 Degree\\Soft

C++ 从文件读入时,如何阻止空格显示为回车符? void displayFile() { char userInput[20];//用于存储用户键入的输入命令。 ifstream ip(“D:\\H Drive ITT\\Level 8 Degree\\Soft,c++,file,input,stl,C++,File,Input,Stl,从文件读入时,如何阻止空格显示为回车符? void displayFile() { char userInput[20];//用于存储用户键入的输入命令。 ifstream ip(“D:\\H Drive ITT\\Level 8 Degree\\Software Dev(Andrew)\\MiniProject\\about.txt”);//从文件输入 string line;//将每一行存储在字符串中 int exit=0;//用于保存指示函数是否应结束的值,该值由用户决定。 if(ip)/

从文件读入时,如何阻止空格显示为回车符?
void displayFile()
{
char userInput[20];//用于存储用户键入的输入命令。
ifstream ip(“D:\\H Drive ITT\\Level 8 Degree\\Software Dev(Andrew)\\MiniProject\\about.txt”);//从文件输入
string line;//将每一行存储在字符串中
int exit=0;//用于保存指示函数是否应结束的值,该值由用户决定。
if(ip)//如果文件存在,则ip为true。
{}
其他的
{
cout线;

这里有两个问题

  • >
    运算符不读取行,它在空白处停止。请按照Ramana在注释中的建议使用
    std::getline
  • 您使用的
    eof()
    不正确。它在您读取完文件结尾后返回true,而不是在您读取完文件结尾之前返回true。因此循环将重复太多次。最好的方法是使用
    std::getline
    的返回值作为循环条件

  • 您可以使用getline逐行读取文件。虽然(std::getline(ip,line))指出了一个非常明显的问题,即它的重要性-
    endl
    相当于一个换行符。(如果OP想知道,我怀疑这是他的问题,但是JIK…!)另外,不相关的效率注意事项-如果代码中的
    if(ip)
    下面真的是一个空块,您可以将其删除,然后说
    if(!ip)
    以表示“if it note”(如果它不存在)。
    >
    读取下一个单词,单词定义为“由空格分隔”;以及“whitespace”(空格)包含的内容远不止换行符。换句话说,您当前不是逐行阅读,而是逐字阅读。一旦您知道如何逐行阅读,您的输出将变得微不足道。
    void displayFile()
    {
        char userInput[20];//Used to store the typed input commands from the user.
        ifstream ip("D:\\H Drive ITT\\Level 8 Degree\\Software Dev (Andrew)\\MiniProject\\about.txt");//Input from file
    
        string line ;//Store each line in the string
        int exit = 0;//Used to hold the value which indicates whether or not the function should end determined by the user.
    
        if (ip)//ip is true if the file exists.
        {}
        else
        {
            cout << "-Error_File_Not_Found" << endl;
        }
    
    
        while (!ip.eof())//Scan till end of file
        {
            ip >> line;
            cout << line << endl;
    
        }
        cout << "Type exit to return" << endl << endl << ">>>" ;
        do {
            cin >> userInput;
            if (strcmp(userInput, "exit") == 0 || strcmp(userInput, "EXIT") == 0 || strcmp(userInput, "eXIT") == 0 || strcmp(userInput, "Exit") == 0)
            {
                exit = 1;
            }
            else
            {
                exit = 0;
            }
    
        } while (exit !=1);
        ip.close();
    
    
    }
    
    //The
    //output
    //displays
    //like 
    //this.