• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
silverphyre673

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

16 posts in this topic

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[i]!=' ' )
        {
            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!
0

Share this post


Link to post
Share on other sites
"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";
}

0

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.
0

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?
0

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;

0

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.
0

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. ]
0

Share this post


Link to post
Share on other sites
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 <
0

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[i] !=' ' )
{
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...
0

Share this post


Link to post
Share on other sites
wtf!! stupid forum cuts off everything after the less than symbol apparently...
but it works fine in the preview
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
wtf!! stupid forum cuts off everything after the less than symbol apparently...
but it works fine in the preview


Because it strips out that tag for AP's, which can't use hyperlinks. Use &lt; instead.
0

Share this post


Link to post
Share on other sites
wait! It still doesn't work for me. Now, the fail bit is set, although nice catch with the semicolon thing. I hate it when I do that.

[EDIT] I just ran it again, and I think its looping through each line of the file, but its just getting a newline. Let me look some more.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
[smile] 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.


that is probably because the file is delimited by 0A (hex) instead of the usual 0D0A. Also, you might want to see if there is some kind of tokenizer function, it would make your life easier. Then you can just keep the first token, skip three, keep the next, skip three, ...etc
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
that is probably because the file is delimited by 0A (hex) instead of the usual 0D0A. Also, you might want to see if there is some kind of tokenizer function, it would make your life easier. Then you can just keep the first token, skip three, keep the next, skip three, ...etc


I agree, if all that is beging done is replacing the tabs... I'd use something like this. Then you can simply make a new string with token1 + " " + token2 + " " + token3 and be done rather than all that looping.
0

Share this post


Link to post
Share on other sites
wait, wait, I think I'm tracking it down. Replaced readline function with simple

std::getline( fin, ret );

and now thats working... so something it wrong with the fix function. just a minute.
0

Share this post


Link to post
Share on other sites
done! correct source:


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

std::string readline( std::ifstream & fin )
{
std::string ret;
char c;
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[i]!=' ' )
{
for ( unsigned j=i; j< line.length(); ++j )
{
if ( line[j]==' ' )
return ret;
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 ( line = fix ( readline ( fin ) ), fin.good() )
{
fout << line << "\r\n";
}
if ( !fin.good() )
{
std::cout <<
"good: " << fin.good() << std::endl <<
"fail: " << fin.fail() << std::endl <<
"bad : " << fin.bad() << std::endl <<
"eof : " << fin.eof() << std::endl;
}
fout.close();
fin.close();
std::cout << "Operation successful.\n";
std::cin.get();
return 0;
}



It wasn't returning after it reached a space - it was looping back to the beginning of the line, further one. So if the line was
MARY 1 2

It would do this:

MARY
ARY
RY
Y

etc.

YEARGH!! Thanks you guys, that could have taken a long time ( especially finding that crappy semicolon). Awesome.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0