Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 silverphyre673   Members   -  Reputation: 454

Like
0Likes
Like

Posted 24 April 2005 - 08:49 AM

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!

Sponsor:

#2 me22   Members   -  Reputation: 190

Like
0Likes
Like

Posted 24 April 2005 - 08:59 AM

"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";
}



#3 silverphyre673   Members   -  Reputation: 454

Like
0Likes
Like

Posted 24 April 2005 - 09:01 AM

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.

#4 silverphyre673   Members   -  Reputation: 454

Like
0Likes
Like

Posted 24 April 2005 - 09:06 AM

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?

#5 me22   Members   -  Reputation: 190

Like
0Likes
Like

Posted 24 April 2005 - 09:21 AM

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;



#6 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 24 April 2005 - 09:21 AM

while ( !fin.eof() ); <

#7 silverphyre673   Members   -  Reputation: 454

Like
0Likes
Like

Posted 24 April 2005 - 09:25 AM

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.

#8 me22   Members   -  Reputation: 190

Like
0Likes
Like

Posted 24 April 2005 - 09:29 AM

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

#9 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 24 April 2005 - 09:30 AM

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 <

#10 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
0Likes
Like

Posted 24 April 2005 - 09:32 AM

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

#11 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 24 April 2005 - 09:35 AM

wtf!! stupid forum cuts off everything after the less than symbol apparently...
but it works fine in the preview

#12 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
0Likes
Like

Posted 24 April 2005 - 09:38 AM

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.

#13 silverphyre673   Members   -  Reputation: 454

Like
0Likes
Like

Posted 24 April 2005 - 09:42 AM

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.

#14 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 24 April 2005 - 09:42 AM

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

#15 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
0Likes
Like

Posted 24 April 2005 - 09:45 AM

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.

#16 silverphyre673   Members   -  Reputation: 454

Like
0Likes
Like

Posted 24 April 2005 - 09:49 AM

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.

#17 silverphyre673   Members   -  Reputation: 454

Like
0Likes
Like

Posted 24 April 2005 - 09:53 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS