473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

file size error - urgent



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
4 1853
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
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
# //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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Peter Ballard | last post by:
Hi all, I've got a C program which outputs all its data using a statement of the form: putchar(ch, outfile); This has worked fine for years until it had to output more than 2GB of data...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: Amratash | last post by:
Hi, I needyour help.Its urgent. My main aim is to log the site activities in a database at runtime. I'm using W3C Extended Log File format for logging information. I can use ODBC Logging directly...
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
1
by: igotyourdotnet | last post by:
Ok, here is the issue: I have 2 web pages one does a file upload to a database and the other page does a FTP to a server, both pages use the c:\temp directory. The page that uploads to a database...
1
by: sruthini | last post by:
Hi, I am trying to checking the size of a file using javascript, I am using the following code <html> <head> <script language="JavaScript"> function A() { var oas = new...
1
by: shyaminf | last post by:
hi everybody! iam facing a problem with the transfer of file using servlet programming. i have a code for uploading a file. but i'm unable to execute it using tomcat5.5 server. kindly help me how to...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I cut and paste the following code from msdn help page which it just introduces view and multiview server controls. Here is what I do: in vs studio 2005, File --New Web Site, it...
4
by: liberty1 | last post by:
Hi everyone. I appreciate your effort at helping newbies like me. I have the following problems and will appreciate urgent help. PROBLEM NUMBER 1: Using PHP and MySQL, I am able to upload...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.