why?

This is a discussion on why? within the C++ Programming forums, part of the General Programming Boards category; this only prints % .....what am i doing wrong? the very first line is %2004-10% Code: int seekMonth(char *file, Date ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    why?

    this only prints % .....what am i doing wrong?

    the very first line is
    %2004-10%

    Code:
    int seekMonth(char *file, Date &)
    {
        int length;
        fstream fin;
        fin.open(file, ios::in);
        fin.seekg(ios::end);
        length = fin.tellg();
        fin.seekg(ios::beg);
        
        
        
        char *line;
        line = new char[length];
        fin.getline(line, length);
        
        if(*line == '%')
        {
            cout << line << endl;
           
        }    
            
        return 0;
    }

    i have stripped my code down and ran it so there is no more to this code.
    Last edited by misplaced; 10-11-2004 at 01:00 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > fin.seekg(ios::end);
    This should be:
    fin.seekg(0, ios::end);

    > fin.seekg(ios::beg);
    Same here:
    fin.seekg(0, ios::beg);

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    hehe....i JUST now realized that....

    what does the zero do anyway?

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Use:
    Code:
    fin.seekg(0,ios::end);
    //instead
    fin.seekg(ios::end);
    You don't need to use int main(void) you can use int main() because under C++ (but not C), leaving the parentheses empty is the same as using void in the parentheses. (In C, leaving the parentheses empty means you are remaining silent about whether or not there are arguments.)
    Second
    Certain type-independent information that used to be kept in the ios base class has been moved to the new ios_base class. This includes the various formatting constants such as ios::fixed, which now is ios_base::fixed. Also, ios_base contains some options that weren't available in the old ios.


    The members and methods found in the ios_base class formerly were found in the ios class. Now ios_base is a base class to ios. In the new system, ios is a template class with char and wchar_t specializations, while ios_base contains the non-template features.
    So use ios_base::in not ios::in although don't need to.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ok....

    except i did not post main

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >what does the zero do anyway?
    It indicates how far to move the file pointer, starting from the position specified by the second parameter. So:
    fin.seekg(5, ios::beg);
    would move the file pointer 5 bytes forward from the start of the file.

    fin.seekg(-5, ios::end);
    would move the file pointer 5 bytes backward from the end of the file.

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by misplaced
    ok....

    except i did not post main
    Sorry, obviously that wasn't inteded to you. I was replying to another post and accidentaly paste that, but no harm.
    What zero do?
    This is explanation from bruce eckel:
    In some situations you may want to move this stream
    position. You can do it using two models: One uses an absolute location in the stream called
    the streampos; the second works like the Standard C library functions fseek( ) for a file and
    moves a given number of bytes from the beginning, end, or current position in the file.
    The streampos approach requires that you first call a “tell” function: tellp( ) for an ostream
    or tellg( ) for an istream. (The “p” refers to the “put pointer” and the “g” refers to the “get
    pointer.”) This function returns a streampos you can later use in the single-argument version
    of seekp( ) for an ostream or seekg( ) for an istream, when you want to return to that
    position in the stream.
    The second approach is a relative seek and uses overloaded versions of seekp( ) and seekg( ).
    The first argument is the number of bytes to move: it may be positive or negative. The second
    argument is the seek direction:
    ios::beg From beginning of stream
    ios::cur Current position in stream
    ios::end From end of stream

    Code:
    in.seekg(0, ios::end);
    This call seeks zero bytes off the end of the file, that is, to the end.

    Cheers!

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    thank you...

    now i got a new problem....

    i know tellg returns streampos...but will it's value be the number characters into the file....
    example:
    Code:
    f.getline(line, length);                //get a line "%2004-10%\n  length is 9
    streampos filePos = f.tellg();      //filePos should be 9?       
    f.getline(line, length);

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I was reading Micko's and thinking he had have posted a reply to something else here. Which brings me to the int main() over int main(void) issue. I would typically recommend the opposite....just because in c++ you can do code like this:

    Code:
    for(int i = 0; i < size; i++) {
      // code here
    }
    doesn't mean you should.

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    What is wrong with using a variable that is local to the scope of a for loop?
    Woop?

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    damn i hate working with files

    .....i've been chasing an error around and around.......

    i open a file in one function, search for the desired line and store it in a streampos....
    i then close the file and return streampos to calling function....then i open the file and seekg using the streampos that was returned to calling function....is this not going to work as one would think it would?

    (pseudo-pseudocode)
    Code:
    streampos funct1(file)
    { 
        fstream fileIn;
       streampos fileIn.tellg();
       return streampos;
    }
    funct2()
    {
         streampos1 = funct1(filename);
         fstream fileIn;
         fileIn.seekg(streampos1);     
    }
    Last edited by misplaced; 10-11-2004 at 03:41 AM.

  12. #12
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by master5001
    I was reading Micko's and thinking he had have posted a reply to something else here. Which brings me to the int main() over int main(void) issue. I would typically recommend the opposite....just because in c++ you can do code like this:

    Code:
    for(int i = 0; i < size; i++) {
      // code here
    }
    doesn't mean you should.
    Well, I suppose that's the matter of style, taste and habbits and it is certanly something many would argue about!

  13. #13
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I'll try to clarify thi as best as I can although I'm still beginer when it comes to deal with these issues.

    this only prints % .....what am i doing wrong?

    Code:
    fin.seekg(ios::end);
    In ios.h header I found this:
    Code:
    ...
    class _CRTIMP ios {
    
    public:
        enum io_state {  goodbit = 0x00,
                         eofbit  = 0x01,
                         failbit = 0x02,
                         badbit  = 0x04 };
    
        enum open_mode { in        = 0x01,
                         out       = 0x02,
                         ate       = 0x04,
                         app       = 0x08,
                         trunc     = 0x10,
                         nocreate  = 0x20,
                         noreplace = 0x40,
                         binary    = 0x80 };
    
        enum seek_dir { beg=0, cur=1, end=2 };
    
    ...
    As you can see ios::end or ios_base::end is nothing else then number 2.
    Code:
    fin.getline(line, length);
    For example, suppose you want to use getline() to read a name into the 20-element name array. You would use this call:

    cin.getline(name,20);

    This reads the entire line into the name array, provided that the line consists of 19 or fewer characters.
    So in your case you have read only one character and that was %.
    Now, consider this code:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	ofstream out("test.txt",ios_base::out);
    	out<<"%2004-10%\n";
    	out.close();
    	ifstream in("test.txt",ios_base::in);
    	in.seekg(0,ios_base::end);
    	int len=in.tellg();
    	cout<<len;
    	return 0;//you can ommit this according to the ISO C++ standard
    }
    You'll get 11 as output value.
    Because you're using text files unlike binary files where for example an int is always stored as two bytes whereas it's text version "12345" might be five bytes, you get 11 as output value which means that 11 BYTES are stored within file.
    Nine bytes from %2004-10% and two bytes from \n because every character occupies one byte of memory.
    I hope that will help you!
    Please note that I wrote this with best intentions so if some fact is not true I apologize in advance.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >is this not going to work as one would think it would?
    That should work fine.
    Code:
    streampos funct1(file)
    { 
        fstream fileIn;
       streampos fileIn.tellg();
       return streampos;
    }
    You may need to close the stream before you return:
    Code:
    streampos funct1(file)
    { 
        fstream fileIn;
       streampos fileIn.tellg();
        fileIn.close();
       return streampos;
    }
    If it's not working like it should, print out streampos once you enter the second function.

Popular pages Recent additions subscribe to a feed

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