Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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/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++ 使用ifstream读取文本文件_C++_File_Output_Fstream - Fatal编程技术网

C++ 使用ifstream读取文本文件

C++ 使用ifstream读取文本文件,c++,file,output,fstream,C++,File,Output,Fstream,所以我试图从一个文本文件中读取。它显示我可以成功地从文件中读取,但当我尝试读取值时,它只显示0,而文本文件中有其他值 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { std::ifstream file("numbers.txt"); if (file) { cout << "Managed

所以我试图从一个文本文件中读取。它显示我可以成功地从文件中读取,但当我尝试读取值时,它只显示0,而文本文件中有其他值

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main()
{

    std::ifstream file("numbers.txt");
if (file) {
    cout << "Managed to read file successfully. \n";
}else{
    cout << "Unable to read file.";
}


int x, y;

file >> x >> y;

cout << "Num 1: " << x << endl;
cout << "Num 2: " << y << endl;

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
std::ifstream文件(“numbers.txt”);
如果(文件){
cout x>>y;

cout上述代码将打印您的操作状态,如果您想读取和显示内容,您必须这样编写代码:

#include<iostream>
#include<fstream>

using namespace std;         

int main()    
{
            ifstream stream1("D:\\file1.txt");    
            char a[80];

            if(!stream1)    
            {
                        cout << "While opening a file an error is encountered" << endl;    
            }    
            else    
            {    
                        cout << "File is successfully opened" << endl;    
            }

            while(!stream1.eof())    
            {    
                        stream1 >> a;    
                        cout << a << endl;    
            }

            return(0);    
}
#包括
#包括
使用名称空间std;
int main()
{
ifstream stream1(“D:\\file1.txt”);
chara[80];
如果(!stream1)
{

无法使用numbers.txt文件

45
15
您的代码可以在gcc中找到

int main()
{
     std::ifstream file("numbers.txt");
     if (file) 
     {
        cout << "Managed to read file successfully. \n";
        int x, y;
        file >> x >> y;

        cout << "Num 1: " << x << endl;
        cout << "Num 2: " << y << endl;
    }
    else
    {
        cout << "Unable to read file.";
    }
    return 0;
}
intmain()
{
std::ifstream文件(“numbers.txt”);
如果(文件)
{
cout>x>>y;

无法显示文件的内容。当您打印
成功读取文件时,
,您没有从文件中读取任何内容。您所做的只是打开文件。为什么这个问题听起来如此奇怪?现在有在线课程吗?这不是一个答案,它不能解释他的专业版有什么问题gram或者你是如何修复它的。它应该是一个评论。当你获得声誉=50时,你将能够评论其他人的问题。