istream 无法在 DEV C++ 中工作

发布于 2024-09-26 08:27:40 字数 1786 浏览 0 评论 0原文

我在 DEV c++ 中使用 ifstream 和 ofstream 操作,但它们似乎无法正常工作。我一直在尝试编写一些素数生成器代码,但它不起作用:\ 当我在任何时候显示 fstream::tellg() 时,它都会显示 -1:

#include<iostream>
#include<math.h>
#include<fstream>
using namespace std;
int prime (unsigned long long n)
{   
    ifstream f1; 
    ofstream f2;             
    unsigned long long i,m,root;
    int flag=0;
    for(i=2;i<=n;i++)
    {   
        f1.open("prime2.txt",ios::binary);    
        if(!f1.is_open())
        {   
            cout<<"NOT OPEN";    
        }              
        cout<<f1.tellg()<<" ";      //Displaying getpointer pos    
        flag=0;
        root=(unsigned long long)sqrt(i);
        while(f1.read((char*)&m,sizeof(m)))
        {   
            if((i%m)==0)
            {   
                flag=1;
                break;
            }      
            if(m>root)
            {   
                break;
            }                                       
        }             
        f1.close();
        if(!flag)
        {   
            f2.open("prime2.txt",ios::app|ios::binary);
            f2.write((char*)&i,sizeof(i));
            cout<<i<<" ";             //Displaying num being written
            f2.close();
        }   
    }    
    return 1;
}   

int main(int argc, char* argv[])
{   
    prime(50);
    system("pause");
}   

(很抱歉,我只是无法使整个事情都在一个代码块中。我猜 [code] 标签有问题?)

输出:

NOT OPEN-1 2 -1 3 -1 4 -1 5 -1 6 -1 7 -1 8 -1 9 -1 10 -1 11 -1 12 -1 13 -1 14 -1 15 -1 16 -1 17 -1 18 -1 19 -1 20 -1 21 -1 22 -1 23 -1 24 -1 25 -1 26 -1 27 -1 2 8 -1 29 -1 30 -1 31 -1 32 -1 33 -1 34 -1 35 -1 36 -1 37 -1 38 -1 39 -1 40 -1 41 -1 42 -1 43 -1 44 -1 45 -1 46 -1 47 -1 48 -1 49 -1 50 按任意键继续 。 。 。

I'm using ifstream and ofstream operations in DEV c++ but they don't seem to work correctly. I've been trying to write a little prime generator code but it doesn't work :\ When I display fstream::tellg() at any point, it displays -1:

#include<iostream>
#include<math.h>
#include<fstream>
using namespace std;
int prime (unsigned long long n)
{   
    ifstream f1; 
    ofstream f2;             
    unsigned long long i,m,root;
    int flag=0;
    for(i=2;i<=n;i++)
    {   
        f1.open("prime2.txt",ios::binary);    
        if(!f1.is_open())
        {   
            cout<<"NOT OPEN";    
        }              
        cout<<f1.tellg()<<" ";      //Displaying getpointer pos    
        flag=0;
        root=(unsigned long long)sqrt(i);
        while(f1.read((char*)&m,sizeof(m)))
        {   
            if((i%m)==0)
            {   
                flag=1;
                break;
            }      
            if(m>root)
            {   
                break;
            }                                       
        }             
        f1.close();
        if(!flag)
        {   
            f2.open("prime2.txt",ios::app|ios::binary);
            f2.write((char*)&i,sizeof(i));
            cout<<i<<" ";             //Displaying num being written
            f2.close();
        }   
    }    
    return 1;
}   

int main(int argc, char* argv[])
{   
    prime(50);
    system("pause");
}   

(I'm sorry, i just couldn't make the whole thing get in one codeblock. I guess theres something wrong with [code] tag?)

Output:

NOT OPEN-1 2 -1 3 -1 4 -1 5 -1 6 -1 7 -1 8 -1 9 -1 10 -1 11 -1 12 -1 13 -1 14 -1
15 -1 16 -1 17 -1 18 -1 19 -1 20 -1 21 -1 22 -1 23 -1 24 -1 25 -1 26 -1 27 -1 2
8 -1 29 -1 30 -1 31 -1 32 -1 33 -1 34 -1 35 -1 36 -1 37 -1 38 -1 39 -1 40 -1 41
-1 42 -1 43 -1 44 -1 45 -1 46 -1 47 -1 48 -1 49 -1 50 Press any key to continue
. . .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2024-10-03 08:27:40

tellg()的返回值-1 表示失败。检查文件路径是否正确。您的输出清楚地表明该文件无法打开。如果无法打开文件,则不应对该文件(或在本例中为流)执行读/写操作。

编辑

f1.open("prime2.txt",ios::binary);

这里 prime2.txt 的位置是相对的。由于您更改了编译器,因此很可能找不到该文件。首先尝试使用一些绝对值(例如 C:\myprograms\test\prime2.txt),然后找出将 prime2.txt 放在哪里,以便您的程序仅通过在 < 中指定 prim2.txt 来获取它代码>打开()。

The return value -1 of tellg() indicates failure. Check that the file path is correct. Your output clearly indicates that the file cannot be opened. If a file cannot be opened, you shouldn't perform read/write operation on the file, or in this case stream.

EDIT

f1.open("prime2.txt",ios::binary);

Here the location of prime2.txt is relative. Since you changed compiler there is a good chance that the file cannot be found. Try with some absolute value first (like C:\myprograms\test\prime2.txt) and then figure out where to put prime2.txt for your program to get it by only specifying prim2.txt in open().

漫漫岁月 2024-10-03 08:27:40

你输出的第一件事是“NOT OPEN”。如果文件打开失败,不要指望其他任何东西都能工作。

检查文件是否存在于程序执行的目录中。尝试在文件名中使用绝对路径而不是相对路径。

最好逐步构建此逻辑,边做边测试,而不是编写整个内容,然后尝试调试失败代码的一堆输出。

The first thing in your ouput is 'NOT OPEN'. If file open fails, don't expect anything else to work.

Check file is present in the dir where your program executes. Try with absolute path in the filename instead of relative.

It might be better to build this logic up step by step, testing as you go, instead of writing the whole thing and then trying to debug a pile of output from failing code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文