Problem with fstream and seekg()

This is a discussion on Problem with fstream and seekg() within the C++ Programming forums, part of the General Programming Boards category; i'm making a project that the fstream save the value of 3-demensional array (int is[3][15][29). When it save, it will ...

  1. #1
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200

    Problem with fstream and seekg()

    i'm making a project that the fstream save the value of 3-demensional array (int is[3][15][29). When it save, it will generate 1176 line of '0' and '1'. But after it trying to load, it gets error:
    Code:
    int loadvars(int var[15][29], int var2[15][29], int var3[15][29], char name[])
    {
    	using std::ifstream;
    
    	ifstream f[3];
    	int seek[3];
    
    
    	f[0].open(fname);
    	f[0].seekg(0);
    	seek[0] = f[0].tellg();
    	f[0].read(fname, seek[0]);
    	
    	if (f[0].is_open())
    	{
    	
    		for(j=1;j<=28;j++)
    		{
    			for(k=1;k<=14;k++)
    			{
    				f[0]>> var[k][j];	
    			}
    		}
    		for(j=1;j<=28;j++)
    		{
    			for(k=1;k<=14;k++)
    			{
    				f[0]>> var2[k][j];			
    			}
    		}
    		for(j=1;j<=28;j++)
    		{
    			for(k=1;k<=14;k++)
    			{
    				f[0]>> var3[k][j];			
    			}
    		}
    		
    		}
    	else
    	{
    		return false;
    	}
    
    	return (var[15][29], var2[15][29], var3[15][29]);
    
    
    
    }
    How do i load everything at once without any breaking point. I've tried with the seekg() in the middle of each 'for' loop but it get error. Please help me!!!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Code:
    	f[0].seekg(0);
    	seek[0] = f[0].tellg();
    	f[0].read(fname, seek[0]);
    What is that code supposed to be doing?

    What does your file look like? This:
    Code:
    0101010010101...
    This:
    Code:
    0 1 0 1 0 1 0 0 1 ...
    or this:
    Code:
    0
    1
    0
    1
    0
    ...
    ?

  3. #3
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    The file save looks like this:
    Code:
    0
    1
    0
    0
    0
    0
    ...
    Code:
    	f[0].seekg(0);
    	seek[0] = f[0].tellg();
    	f[0].read(fname, seek[0]);
    I declared this variable at first ( ifstream f[3]). I was planning to do like this:

    Code:
    f[0].seekg(0);
    seek[0] = f[0].tellg();
    f[0].read(fname, seek[0]);
    
    //stream in the data for the first variable.
    
    f[1].seekg(393);
    seek[1] = f[1].tellg();
    f[1].read(fname, seek[1]);
    //Stream in the data for second variable start at line 393
    
    //same thing with the third one start at 785;
    See, the problem is that they try to open a file at the same time, then it gets error. So how do i fix this problem?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Why don't you just use a single fstream and get rid of all the seekg stuff? Once it is done reading into var1, the file pointer will be at the 393rd line (because it just finished reading the first 392 lines into var1). So you don't have to seek to the right position. The same would be true after reading into var2.

    I think it would work if you just got rid of all the seek stuff and just used a single fstream instead of three of them.

  5. #5
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    That is what i did (the code in my first post), but the thing is after line 392, it still load everything into var1 instead jump down and load into var2.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Are you sure about that? The posted code doesn't look like it would do that because your for loops only go 14*28 times (392), so the first for loop with j would end and the second for loop with j would begin.

    I think the code you posted is fine except for the seeking stuff. Put a cout statement in each loop to be sure that it is or isn't getting called there.

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    its supposed to be loading a 3x392 or whatever, because of the third dimension in the array, so therefore i think its like 1170 lines of stuff, which looks like

    0
    1
    0
    0
    0
    0
    ....

    I'm working with him on this project, and you're saying if we load one thing, its already on that line so we don't have to seekg();...


    Well that hasnt been the case, we've tried loading the ENTIRE thing without seekg, but it only loads the first 1170 lines of the file...I would assume that its loading those same first 1170 lines 3 times over instead of starting where it left off....

    the exact error, that we tried to load them all at once, was a windows end now error thingy, not a compiler error, just a....

    Program starts, windows error, end now or debug, etc etc

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The problem is probably somewhere else in your code. You might want to post all of it if it is small, or at least the parts where you create the original arrays and the parts where you call the function.

    One thing to mention is that arrays start their indexes at 0, not 1. In your code you ignore the element at index 0 and you make your arrays one size too big (15 and 29 instead of 14 and 28). It is very possible that your crash was because you were using an array index that was out of bounds.

    Here is an example program that uses the code above with some small changes (using array indexes 0-13 and 0-27) and a main function to demonstrate that it's working. Maybe it will help you find out where you are going wrong.
    Code:
    #include <iostream>
    #include <fstream>
    
    bool loadvars(int var[14][28], int var2[14][28], int var3[14][28], char name[])
    {
        using std::ifstream;
    
        ifstream f;
        f.open(name);
        
        if (f.is_open())
        {
            int j=0;
            int k=0;
    
            for(j=0;j<28;j++)
            {
                for(k=0;k<14;k++)
                {
                    f >> var[k][j]; 
                }
            }
            for(j=0;j<28;j++)
            {
                for(k=0;k<14;k++)
                {
                    f >> var2[k][j];            
                }
            }
            for(j=0;j<28;j++)
            {
                for(k=0;k<14;k++)
                {
                    f >> var3[k][j];            
                }
            }
        }
        else
        {
            return false;
        }
    
        return true;
    }
    
    int main()
    {
        using std::cout;
        using std::endl;
    
        int data[3][14][28];
        loadvars(data[0], data[1], data[2], "datafile.txt");
        for (int i=0; i<3; i++)
        {
            cout << "In section #" << i+1 << endl;
            for (int j=0; j<14; j++)
                for (int k=0; k<28; k++)
                    std::cout << data[i][j][k];
            cout << endl;
        }
    }

  9. #9
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Well, at first i used 0 for starting the index of the array, but i got some problem (no idea) with the drawing block. One array suppose to draw 392 squares (one array determine the coordinates and sizes of 392 squares). That why i shifted it up one unit, as well as let the indexes start at 1, and somehow it worked.

    I change the value of the array indexes in your code from 0 to 1, and it still work just fine. Finally i can rest now (not for long). Thanks a lot for helping .
    Hello, testing testing. Everthing is running perfectly...for now

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    OMGOOSES yes you roxxors Daved, our code is bug free for once!

    Now we need to add lots of buttons and stuff to our gui, and we NEED to be able to add FIELDS to our GUI!!!

    Like,
    Name: _______ <- can type there!!!

    Do you have any information on this?

    PS: If we were to post the whole thing, it'd be around 1500-2000 lines of code, I don't think many of you want to review that much code :d

  11. #11
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Actually, the only code we need is storing and displaying. The codes should do that whenever the users press a key on the keyboard, it will store that key into a variable (char) then display on the screen. (we're using OpenGL anyway). I've been able to make it print out the character but after the key released, the character disappear as well.
    Hello, testing testing. Everthing is running perfectly...for now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. a simple, silly yet big problem....
    By punk in forum C++ Programming
    Replies: 7
    Last Post: 12-20-2007, 11:25 AM
  3. Really big problem
    By arjunajay in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2005, 06:03 AM
  4. unusual fstream problems
    By georgefoxjunior in forum C++ Programming
    Replies: 11
    Last Post: 09-09-2004, 09:21 AM
  5. tellg and seekg
    By Zoalord in forum C++ Programming
    Replies: 1
    Last Post: 01-11-2004, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21