Results 1 to 10 of 10

Thread: Random Access Files, Why isn't this returning the position of the last character?

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Question Random Access Files, Why isn't this returning the position of the last character?

    Hello everyone, i'm trying to print a file in reverse, to do this, I need to use random acccess file functions. My idea was to go to the last character and just print until I get to the first character of the file. To find the last character, which is really the start of the file, i thought i would tell the file market to go to the last character in the file, ios::end, and then travel to the beginning of the character, so the file marker should stop at the first character, put it all together and u get seekg(0,ios::end); here's my code
    Code:
    void reverseFile(ifstream& theCopy)
    {
    	char ch;
    	int last;
    	theCopy.seekg(0,ios::end);		//returns the size of the file
    	last = theCopy.tellg();
    	cout <<"Last: " << last << endl;
    
    	for(int offset = 1; offset <= last; offset++)
    	{
    		theCopy.seekg(-offset,ios::end);
    		ch = theCopy.get();
    		cout <<ch;
    	}
    }
    When i ouptut last, it says -1, which isn't right!
    any idea's where I went wrong?
    Thanks
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    -1 - Probably means EOF (End of File)

  3. #3
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Try some simple C first -
    Code:
    /* rev.c */
    #include <stdio.h>
    #include <sys/stat.h>
    #include <stdlib.h>
    off_t filesize(char *);
    int main(int argc,char *argv[]){
            char *buf;
            off_t i=0;        
            int j=0;
            FILE *in;
            i=filesize(argv[1]);
            buf=malloc(i+1);
            memset(buf,0x00,(size_t)i+1);
            in=fopen(argv[1],"rb");
            if(fread(buf,i,1,in))
                 fclose(in);
            else{
                 perror("Error reading file");
                 exit(EXIT_FAILURE);
            }               
            while(i>(-1)){
                if(*(buf+i) ) printf("%c",*(buf+i));
                i--;
            }
            printf("\n");
    	return 0;
    }
    
    off_t filesize(char *fname){
         struct stat st;
         if(stat(fname,&st)){
              printf("Cannot stat file: %s\n",fname);
              exit(1);
         }
         return st.st_size;     
    }

  4. #4

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    Thanks for the replies, if -1 signal's EOF, its still not correct, because the way the seekg works, it should have returned the current file marker, which should have been the start of the file, have to use random access files, so i can't just use c to do it unfortunatley.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    EOF doesn't always mean EOF - it can mean there are was error opening the files maybe due to permissions or something.

    I havn't used C++ File IO functions before but C can handle random access no problem using fread and fwrite.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    From tellg:
    If fail is false, the member function returns rdbuf -> pubseekoff(0, cur, in). Otherwise, it returns pos_type(-1).
    Might this be your problem?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    THis was a very weird problem, the reason it wasn't working correct was I had to create 2 ifstreams, one ifstream to copy the file into an output file, such as:
    Code:
                    ifstream inFile;
    	ifstream reverse;
    	ofstream outFile;
    
    	inFile.open("fileOne.txt",ios::in);
    	outFile.open("fileCopy.txt",ios::out);
    	copy(outFile,inFile);
    	inFile.close();
    	reverseFile(reverse);
    If i tried the following code:
    Code:
        ifstream inFile;
    	//ifstream reverse;
    	ofstream outFile;
    
    	inFile.open("fileOne.txt",ios::in);
    	outFile.open("fileCopy.txt",ios::out);
    	copy(outFile,inFile);
    	inFile.close();
    	reverseFile(inFile);
    It wouldn't open the file from the inFile stream, I created a test, such as
    Code:
    if(inFile.fail())
    {
    cout<<"Cannot open file, inside reverseFile function\n";
    }
    Any idea's why I had to make another ifstream and why I couldn't just close the orginal ifstream after I read the file and copied its contents into the ostream, then re-opened the file inside the reverseFile function?
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Maybe the open/close pair never gets the ifstream out of the EOF state it undoubtly is in after copy? Try calling reset after closing the first file.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    When you said, try calling reset, do you mean set the file marker back to the start of the file? if thats what you ment, It still wont' read the file, I did this to set the filemarker to the start of the file:
    Code:
    void reverseFile(ifstream& theCopy)
    {
    	theCopy.open("fileCopy.txt",ios::in);
    	theCopy.seekg(0,ios::beg);
    	if(theCopy.fail())
    	{
    		cout <<"Cannot open file inside reverse File\n";
    	}
    
    ........
    ........
    ........
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, I mean the reset method of ifstream.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width