![]() |
| | #16 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 476
| |
| slingerland3g is offline | |
| | #17 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
What are we teaching today?
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #18 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,357
| Quote:
The point of both Adak and slingerland3g is that we should avoid giving a complete answer to a homework problem until it has been shown that a genuine attempt has been made for a complete solution.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #19 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > if (argc<2) fatal(-1,"Two filenames required."); If argc is 2, what is the value of argv[2] here? > if ((fstOUT=fopen(argv[2],"w"))==NULL) > if ((fstRO=fopen(argv[1],"ro"))==NULL) Which non-standard compiler extension requires "o" in the mode? > case -1: fprintf(fstOUT, "%d. %s\n",ln,buffer); // the "special case" Yes, very special when you have this line in the called function. > if (buffer==NULL) return -1; // last check > buffer=realloc(buffer,++i+1); // more memory for next iteration And if realloc fails, what happened to the memory you used to have? Whilst it doesn't appear to be a problem in this instance, the fact that i starts at 0 and the amount of memory starts at 1 is a recipe for off-by-one errors at some point.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #20 | ||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Quote:
Anyway, the OP does not claim to be a student with homework, which means s/he may not have the benefit of a class of peers and all the examples and advice one can find at an institution of higher learning, so thought I was being helpful, and, in fact, inviting questions with my unorthodox programming style ![]() Which I am beginning to believe is pretty much "bug free" for sure now.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | ||
| MK27 is offline | |
| | #21 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Not to ruin a perfectly good round of public humiliation on another member, but I should point out I posted code yesterday that does basically exactly this task. Though mine works a little differently than MK27's since it is designed to remove or find and replace something from a different file. |
| master5001 is offline | |
| | #22 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| Why was this reopened for discussion. >> Which I am beginning to believe is pretty much "bug free" for sure now. buffer=realloc(buffer, ++i+1); The value of i is undefined. An expression must not reference a stored value more than once, in order to be defined. But that may not be the exact wording... it's in the Statements section of the Standard. Anyway, aside from what Salem pointed out earlier about the realloc statement. He's right about that - if realloc fails, you leak your own memory. Lengthening the buffer one character at a time seems rather braindead. You might as well read character by character. Not to mention that your streamline function will return leaving the string without a terminating zero, sometimes. And also, if streamline returns zero (which should happen if the line does not end in a newline) then you only succeed partially. Consider the last line in a file for example, which may not be followed with a newline and not copied by the program.
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 |
| whiteflags is offline | |
| | #23 |
| POeT GuY Join Date: Feb 2008 Location: Bz
Posts: 213
| Lol, Hey I'm "Fluent" in spanish agreed with MK27 , enjoy guys, heading to class atm |
| Matus is offline | |
| | #24 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| ++i + 1 does not produce an undefined value for i. If he put ++i += i + 1, that would be a problem. |
| master5001 is offline | |
| | #25 | |||||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Quote:
Quote:
Quote:
Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |||||
| MK27 is offline | |
| | #26 | ||||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Quote:
Quote:
buffer[i]='\0'; Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | ||||
| MK27 is offline | |
| | #27 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > If realloc fails, the program is over because the function returns -2. All the allocation is checked. If the program is over Yeah today maybe. But sooner or later, you'll use realloc in the same way and you won't be exiting the program. THEN you'll have a problem. > You have it backwards (the first character is 0, but that still requires 1 byte). Like I said, I didn't think there was a problem, just that the code was messy and error prone.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #28 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Example: Code: #include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if(argc != 3)
puts("Usage: copyfile [from] [to]");
else
{
FILE *ifile, *ofile;
char *buffer;
size_t size, total;
ifile = fopen(argv[1], "rb");
ofile = fopen(argv[2], "wb");
if(!ifile || !ofile)
{
perror("Could not complete this operation.");
if(ifile)
fclose(ifile);
if(ofile)
fclose(ofile);
return EXIT_FAILURE;
}
if(!(buffer = malloc(0x1000))) /* 4k is sufficient */
{
fputs("Out of memory!\n", stderr);
fclose(ifile);
fclose(ofile);
return EXIT_FAILURE;
}
total = 0;
while((size = fread(in, 1, 0x1000, ifile)))
{
fwrite(out, 1, size, ofile);
total += size;
printf("\r%d bytes copied", total);
}
free(buffer);
fclose(ifile);
fclose(ofile);
}
return EXIT_SUCCESS;
}
|
| master5001 is offline | |
| | #29 | ||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Quote:
Anyway, if by error prone you mean "there's some mistakes you could have made by doing it this way, but didn't" then I will take that as a compliment, Salem.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | ||
| MK27 is offline | |
| | #30 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Except "before" is "malloc(1)". Since when does malloc put a null terminator at the end of memory? Never, is when. |
| tabstop is offline | |
![]() |
| Tags |
| copy, data, file, mode, modify |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| File transfer- the file sometimes not full transferred | shu_fei86 | C# Programming | 13 | 03-13-2009 12:44 PM |
| gcc link external library | spank | C Programming | 6 | 08-08-2007 03:44 PM |
| Possible circular definition with singleton objects | techrolla | C++ Programming | 3 | 12-26-2004 10:46 AM |
| spell check in C using a dictionary file | goron350 | C Programming | 10 | 11-25-2004 06:44 PM |
| gcc problem | bjdea1 | Linux Programming | 13 | 04-29-2002 06:51 PM |