By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,347 Members | 1,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,347 IT Pros & Developers. It's quick & easy.

file size error - urgent

P: n/a


Hello All

Am trying to read a huge txt file and write to a file back.

pls see the code

int main()
{
char* pData;

//write to some file

delete pData;

}
bool GetData( char* &pData)
{

char pTempFileLocation="C:\\temp\\test.txt";
//read back the data from the temp file
FILE *fp = fopen(pTempFileLocation,"r+t");
//This Code is to find the size of the file
//simplest way
fseek(fp,0L,SEEK_END);

//now file poitner in the last poisition.
//so pos will have the size :)
long pos = ftell(fp);

//move the file pointer back to begin
//for reading..
fseek(fp,0L,SEEK_SET);*/

//allocate the data with the pos
pData = new char[pos];

//Just Inializing the pData to null
//for safe side initialization
memset(pData,0,pos);

//read the data back and update the passes variable
if(fp)
fread(pData, sizeof( char ),pos,fp);
else
// file not found
return false;
fclose(fp);

return true;
}
The problem is, I am getting the filesize correctly in the pos var. And am
allocatiin the pData correctly.
but while writing back to new file, it writes some more junk characters.Y it
is so.

File size is > 3MB.

THanks in Adv
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Imran wrote on 10/08/04 :

Am trying to read a huge txt file and write to a file back.

pls see the code

int main()
{
char* pData;

//write to some file

delete pData;
Not a C word.

you want

news:comp.lang.c++

}
bool GetData( char* &pData)


I confirm...

Please learn to make the difference between C and C++. Ther are
different languages.

BTW, The 'urgent' word may have an unexpected side effect...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2

P: n/a
Imran <im******@in.bosch.com> spoke thus:
bool GetData( char* &pData)
You're either using a C++ compiler or you didn't post your real code.
Either way, you have a problem WRT this newsgroup.
{
char pTempFileLocation="C:\\temp\\test.txt";
Clearly not your real code. Do post it, pray.
//read back the data from the temp file
And you seem to be using a C++ compiler...
//allocate the data with the pos
pData = new char[pos];
Yep. Get thee to comp.lang.c++!
The problem is, I am getting the filesize correctly in the pos var. And am
allocatiin the pData correctly.
but while writing back to new file, it writes some more junk characters.Y it
is so.


Perhaps you'd like to post the code where you do that? I bet you're
missing a '\0' on the end of the string you write to the file.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3

P: n/a
# //read back the data from the temp file
# FILE *fp = fopen(pTempFileLocation,"r+t");

Why not do something like
if (!fp) perrror(pTempFileLocation);
so that if the fopen is failing, you can get some error message.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
No pleasure, no rapture, no exquiste sin greater than central air.
Nov 14 '05 #4

P: n/a
Imran wrote:
[...]
FILE *fp = fopen(pTempFileLocation,"r+t");

//This Code is to find the size of the file
//simplest way
fseek(fp,0L,SEEK_END);

//now file poitner in the last poisition.
//so pos will have the size :)
long pos = ftell(fp);

[...]

Well, ignoring the fact that you appear to have C++ code, and not C, my
understanding is that on a text file (as noted by your "r+t" on fopen),
the number of bytes gotten with fread() is not necessarily the same as
the poisition of EOF. (In fact, under DOS/Windows, you are virtually
guaranteed that fread will get less bytes than your fseek/ftell combo
tells you are there.) This is because end-of-line will be returned as
a single '\n' character even though it may be stored on disk as more
than a single byte.

For example, a text file on Windows containing a single line of the
word "foobar" will contain 8 or 9 bytes according to fstat or the
fseek/ftell combo.

It will either be:

66 6f 6f 62 61 72 0d 0a

or

66 6f 6f 62 61 72 0d 0a 1a

In either case, fread will only get 7 characters:

66 6f 6f 62 61 72 0a

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.