|
-
Dec 9th, 2003, 05:07 PM
#1
Thread Starter
Hyperactive Member
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
-
Dec 9th, 2003, 05:12 PM
#2
-1 - Probably means EOF (End of File)
-
Dec 9th, 2003, 06:05 PM
#3
Frenzied Member
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;
}
-
Dec 9th, 2003, 08:00 PM
#4
Thread Starter
Hyperactive Member
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
-
Dec 10th, 2003, 05:04 AM
#5
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.
-
Dec 13th, 2003, 09:57 AM
#6
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.
-
Dec 13th, 2003, 09:55 PM
#7
Thread Starter
Hyperactive Member
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
-
Dec 14th, 2003, 12:00 PM
#8
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.
-
Dec 14th, 2003, 01:34 PM
#9
Thread Starter
Hyperactive Member
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
-
Dec 15th, 2003, 04:29 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|