473,499 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char pointer issue

Hi,all
I use the following code to read 10000 bytes from a file(fp1) and write
them to another file(fp2).

char *pStr = new char[10001];
fread(pStr,dw_size,1,fp1);
int i = 0;
while(i<10000)
{
fwrite(pStr,1,1,fp2);
pStr++;
}

I found only 1682 bytes was written to fp2!

Could any body tell me why?

Thanks in advance!

Anderson
Jul 25 '06 #1
2 864
I use the following code to read 10000 bytes from a file(fp1) and write
them to another file(fp2).

char *pStr = new char[10001];
fread(pStr,dw_size,1,fp1);
what is the value of dw_size here?
int i = 0;
while(i<10000)
{
fwrite(pStr,1,1,fp2);
pStr++;
}
1) i never changes, so this is an endless loop. Is this really the all of
the code in your function?
2) you write 1 byte at a time. why not write the whole buffer at once?
>
I found only 1682 bytes was written to fp2!
Apart from the things I already mentioned, You really should check the
return values that you get from the different IO functions. how else would
you know if there was a problem or not?

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Jul 25 '06 #2
Thanks Bruno for metioned these points!

"Bruno van Dooren" <br**********************@hotmail.comдÈëÏûÏ¢
news:95**********************************@microsof t.com...
> I use the following code to read 10000 bytes from a file(fp1) and
write
them to another file(fp2).

char *pStr = new char[10001];
fread(pStr,dw_size,1,fp1);

what is the value of dw_size here?
> int i = 0;
while(i<10000)
{
fwrite(pStr,1,1,fp2);
pStr++;
}
1) i never changes, so this is an endless loop. Is this really the all of
the code in your function?
2) you write 1 byte at a time. why not write the whole buffer at once?
>>
I found only 1682 bytes was written to fp2!

Apart from the things I already mentioned, You really should check the
return values that you get from the different IO functions. how else would
you know if there was a problem or not?

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Jul 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
6534
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
5
8929
by: wallacej | last post by:
Does anybody know why unsigned char myImage; works but unsigned char myImage; does not? I think its because the second line is a value too big for unsigned char, is this ...
5
17556
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
1
3884
by: electric sheep | last post by:
Hi, can somebody explain the following syntax to me. This is straight from a gnu info file: int main(void) { /* Hashed form of "GNU libc manual". */ const char *const pass =...
5
3652
by: Johnathan Doe | last post by:
Why is is necessary to write a function that allocates memory for, or changes, a pointer to a char array using char** instead of char*? E.g. void modify_string( char** string ) { if( string...
24
3046
by: Rookie | last post by:
I was just reading about the envp argument for main function. Since there is no argc equivalent for envp (i.e the number of elements for the envp array are not known) the last element will be a...
4
1042
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok...
31
7699
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
17
9122
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
13
3113
by: arnuld | last post by:
i see the use of pointers, from K&R2 but what is the use of: 1. "pointer to pointer": char c; char** ppc; 2. pointer to function:
0
7134
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
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
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...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.