Thread: unusual fstream problems

  1. #1
    Newbie
    Join Date
    Sep 2004
    Posts
    4

    Question unusual fstream problems

    For some reason, a bunch of functions won't compile using fstreams- most importantly, seekg and seekp:

    Error : 'seekg' is not a struct/union/class member
    readertest.cp line 41 readMe.seekg(1);

    I have the fstream and iostream headers included. I've even downloaded example code that worked for other people, but still the same error.

    Is there some reason why such functions would not be recognized? Any suggestions/ideas would be appreciated.

    Thanks!

    (BTW, I'm running an old version of Metrowerks Codewarrior IDE on Macintosh.)

  2. #2
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Well if it's code that's worked before I've no idea. But if you have declared readme as a pointer you'd want:

    Code:
    readMe->seekg(1).

    dt
    Last edited by DominicTrix; 09-07-2004 at 12:32 PM. Reason: unclear to read
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Could you post some code? It's kind of hard to tell if anything's wrong if you can't see the code
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Newbie
    Join Date
    Sep 2004
    Posts
    4

    the code

    Well, the code is really only that one statement. But here goes.

    btw, I'm not sure how to paste in code the correct way, so sorry if it's messy.

    // Headers

    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <stdlib.h>


    // Proto

    int load(int loc);


    // Main

    int main()
    {

    load(1);

    return 0;
    }


    // Load
    int load(int loc=1)
    {
    ifstream readMe( "data file" );
    char buff;
    int count=1;
    int isFound=0;
    int length;


    readMe.seekg(1);
    readMe.get(buff);
    cout << buff;
    cout << length;

    return 1;

    } // end load

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If you want to post code, use [code ] and [/code ] tags, but without the spaces.

    Potential problem: The file begins at 0, not 1.
    Problem: length is never initialized
    Potential problem: If the file is of length 1 or 0, buff will contain a garbage value because you will hit eof() when reading from position 1.

    Problem: You're using old-style headers (iostream.h, etc. instead of <iostream>, <fstream>)
    Problem: The C headers (stdio.h, stdlib.h) should be replaced with <cstdio>, <cstdlib>

    Good to see you're not using void main() though
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Newbie
    Join Date
    Sep 2004
    Posts
    4
    regarding problem 1- the point of using seekg() was to start somewhere besides the beginning of the file. hence (1) and not (0).

    problem 2- I'm not using length any more; the program is only supposed to read 1 char now.

    problem 3- the data files I'm reading from are never of small length. but, when I actually start writing the program I will of course address that.

    problem 4- true enough

    problem 5- doesn't seem to work with my compiler.

    Minor issues aside, I still can't use the seek commands at all. Any ideas about that? Might I need a new <fstream> header file?

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oh right, forgot... you're getting syntax errors, not logic errors.. lol!

    Are you getting any errors before this? If not, try creating a new project instead... and if that doesn't work, reinstall your compiler... and if that doesn't work, try re-downloading then reinstalling your compiler... and if that doesn't work, borrow a friend's compiler... and if that doesn't work, reinstall your OS... and then throw your computer out the window
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Newbie
    Join Date
    Sep 2004
    Posts
    4
    unfortunately it happens with all projects.

    however, in the mean time I created a simple structure to replace seek:

    [code] while (count<loc-1)
    {
    readMe.get();
    count++;
    }
    readMe.get(buff);
    cout << buff; [/uncode]

    a bit inelegant, but it works.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by georgefoxjunior
    Well, the code is really only that one statement. But here goes.

    btw, I'm not sure how to paste in code the correct way, so sorry if it's messy.
    Code:
    // Headers
    
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    // Proto 
    
    int load(int loc);
    
    
    // Main
    
    int	main()
    {
    	
    	load(1);
    	
    	return 0;
    }
    
    
    // Load
    int load(int loc=1)
    {
    	ifstream	readMe( "data file" );
    	char		buff;
    	int			count=1;
    	int			isFound=0;	
    	int			length;	
    	
    	
    	readMe.seekg(1);
    	readMe.get(buff);
    	cout << buff;
    	cout << length;
    	
    	return 1;
    	
    } // end load

    (I added code tags)

    Well, regardless of program quality and program bugs, this appears to me to be a syntactically valid C++ program. How are you compiling it? If you try to compile it as a C program you may get errors about seekg and lots of other things. (Don't have any way to test it with CodeWarrier.)

    Regards,

    Dave

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    ^ what he said.

    >>readertest.cp
    If that's .cp and not .cpp, it's possible that it's compiling as a C program (although the C extension is .c). Rename it to .cpp, then check your options and stuff to make sure you're compiling as a C++ program.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    running result is '7798112', what's mean this ?

    this is edited code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    #include <stdlib.h>
    
    using namespace std;  //add this is ok
    
    int load(int loc);
    
    int main()
    {
        load(1);
    
        cin.ignore();              //add this to stop closing window
        cin.get();
        return 0;
    }
    
    int load(int loc=1)
    {
        ifstream readMe( "data file" );
        char buff;
        int count=1;
        int isFound=0; 
        int length; 
    
    
        readMe.seekg(1);
        readMe.get(buff);
        cout << buff;
        cout << length;
    
        return 1;
    
    }

  12. #12
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    length could be anything; after declaring it, you never use it. It's still set to an uninitalized value. You never use count, either.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

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. more fstream problems
    By howzer in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2006, 06:17 AM
  3. fstream problems
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 09:44 AM
  4. stl fstream error (yes... file problems again)
    By ygfperson in forum C++ Programming
    Replies: 7
    Last Post: 07-13-2002, 10:47 PM
  5. I/O Problems - fstream
    By mol in forum C++ Programming
    Replies: 17
    Last Post: 10-16-2001, 03:50 PM