Jump to content
  • Advertisement
Sign in to follow this  
silverphyre673

Is there a limit to the size of a file that ifstream can load?

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I tried this code:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

std::string readline( std::ifstream & fin )
{
    std::string ret;
    std::getline ( fin, ret );
    return ret;
}

std::string fix ( std::string line )
{
    std::string ret;
    for ( unsigned i=0; i<line.length(); ++i )
    {
        if( line!=' ' )
        {
            for ( unsigned j=i; j< line.length(); ++j )
            {
                if ( line[j]==' ' )
                    break;
                ret += line[j];
            }
        }
    }
    return ret;
}

int main()
{
    std::string filename_in, filename_out;
    std::string line;
    
    std::cout << "Enter filename to fix: ";
    std::getline ( std::cin, filename_in );
    std::cout << "Enter filename for output: ";
    std::getline ( std::cin, filename_out );
    if ( filename_in == filename_out )
    {
        std::cout << "Can't use the same file!  Abort.\n";
        std::cin.get();
        return 1;
    }
    std::ifstream fin ( filename_in.c_str(), std::ifstream::in );
    if (!fin)
    {
        std::cout << "Could not open " << filename_in << " for reading!\n";
        std::cin.get();
        return 1;
    }
    std::ofstream fout ( filename_out.c_str(), std::ifstream::out );
    if (!fout )
    {
        if (fin)
            fin.close();
        std::cout << "Could not open " << filename_out << " for writing!\n";
        std::cin.get();
        return 1;
    }
    std::cout << "Files opened successfully\n";
    while ( !fin.eof() );
    {
        line = fix ( readline ( fin ) );
        std::cout << line << '\n';
        fout << line << "\r\n";
    }
    fout.close();
    fin.close();
    std::cout << "Operation successful.\n";
    std::cin.get();
    return 0;
}

which compiles and runs, and is meant to turn the name files from the census into a list of names without the extra information (i.e. just take the name, cut everything else off, and put the name into a new file). Its just for my own use, so its not very applicable elsewhere, but the code should be fairly easy to follow. Anyways, the problem: it runs, opens both the input and the output file with no problem, but just sort of hangs when it gets to the "while (!fin.eof() )" line, where it does the actual IO from the files. I have NO idea why, as it loads both files correctly. It freezes there no matter what, even if you change it to "while ( 1 )". Could someone please compile it and tell me what the problem is, or if it worked on your computer? You would have to download the name list ( just cut and paste into a text file). I would really appreciate it. By the way, the above is the complete source code. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
"freezes" or gets stuck in an infinite loop?

You may be hitting a failure of some sort -- istreams can be in bad or fail states in addition to eof. So 2 things:

1) loop while fin.good() -- that way the loop will exit if input fails, goes bad, or eofs.

2) eof(), bad(), fail(), and good() all inform you about PREVIOUS operations. If good() is true, the next io operation might succeed, otherwise it wont. You should always do the good() chack after the read, but before the processing. Methods:

for (;;) {
// read from fin
if ( !fin.good() ) break;
// use the input
}
// or use the comma operator
while ( line = fix ( readline ( fin ) ), cin.good() ) {
std::cout << line << '\n';
fout << line << "\r\n";
}

Share this post


Link to post
Share on other sites
Ok, thanks, I'll try that. But it just freezes, doesn't get caught in a loop. I used the debugger on dev-cpp and it keeps just stopping on that line. But let me give that a try. Thanks.

Share this post


Link to post
Share on other sites
well, that helped. at least it doesn't freeze anymore, but why would fin be "bad"?? I changed it to

while ( (read from file), fin.good() )

fin is bad so it just exits. :( Why would it do that?

Share this post


Link to post
Share on other sites
One clarification: !good does not mean bad. good() means !(eof() || fail() || bad())

Which of the reasons for !good() are you getting?

cerr << boolalpha << "How is fin?" << endl
<< " good: " << fin.good() << endl
<< " fail: " << fin.fail() << endl
<< " bad: " << fin.bad() << endl
<< " eof: " << fin.eof() << endl;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
while ( !fin.eof() ); <

Share this post


Link to post
Share on other sites
good: 0
fail: 1
bad : 0
eof : 1

Why would it think its at the end of the file? The file is hundreds of lines long, and it only loads one ( incorrectly ) before it quits. Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
while ( !fin.eof() ); <


Nice catch, AP =)

Explanation: It reads the whole file, ending on a failed read that sets eof. Then what's supposed to be the loop runs, reading and processing nothing from the stream ( because all io stream operations are NOPs if !good, iirc )

[ rant: That kinda thing's the reason I prefer K&R brace positions -- I find while ( fin.good() );{ much more noticable. ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ok the stupid forum cut of 90% of my post... at least it got the important part..
It shows up fine in the preview this time, but I'm not going to tempt fate by trying again. Also, after the preview, the caret doesn't show up in the edit ctrl... someone <

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
while ( !fin.eof() ); <


[smile] Nice catch!

Note that running the program might take a bit, I think you will need to fix your Fix function as well. A 7MB file is generated from that data source. Perhaps you were looking for something like this?


std::string fix ( std::string line )
{
std::string ret = ""; // Always set to empty to ensure that the string is indeed empty ;)

for ( unsigned i=0; i<line.length(); ++i )
{
if( line !=' ' )
{
for ( unsigned j=i; j< line.length(); ++j )
{
if ( line[j]==' ' )
{
ret += ' ';
break;
}
ret += line[j];
}
i = j; // Since you advanced the position in J, update the new position of I
}
}
return ret;
}



Just an idea, I don't know what you are trying to fix though...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!