473,566 Members | 2,924 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_s ize,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 868
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_s ize,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************* *********@hotma il.com
Remove only "_nos_pam"

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

"Bruno van Dooren" <br************ **********@hotm ail.comдÈëÏûÏ¢
news:95******** *************** ***********@mic rosoft.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_s ize,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************* *********@hotma il.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
6572
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
5
8954
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 correct? If so:
5
17594
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 two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
1
3898
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 = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/"; I think the idea at play here is whether the pointer is constant or not ?
5
3663
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 == NULL ) return;
24
3053
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 null pointer. But my question is about the syntax used to test this: envp!=(char *)0 !=(char *)0;i++)] What exactly does (char *)0 stand for?...
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 etc and their implementation.the way compiler treats them and the scenarios where one
31
7708
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
9126
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 inside of a structure. I keep getting segmentation fault errors and I am having trouble understanding why. Here's the parts of the code in question......
13
3120
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
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
926
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.