Hello to you all ,

I have a program who i want to copy one input.txt to a output.txt
and for some reason it duplicates the string and it doest create a full duplicate .

i am over this for a few hours now and i can figure it out.

can you help me?

Input file and source is attached , neutrilze the Z option in the Menu

thanx :)

Dani AI

Generated

For 's menu-driven copy (the "Z" option), the symptom — parts of the input repeating in the output instead of an exact copy — usually comes from control-flow or read-loop mistakes rather than filesystem corruption. Common root causes: a read loop that writes a stale buffer when the read fails (classic while(!feof(...)) / not checking fgets return), opening the destination in append mode ("a") instead of write ("w"/"wb"), a missing break in a switch that lets the copy routine run twice, or accidentally calling the copy routine more than once. 's joke aside and 's note, these are the practical things to check first.

Use explicit read-return checks. For text-copy in C the safe pattern is:

char buf[4096];
FILE *in = fopen("input.txt", "r");
FILE *out = fopen("output.txt", "w");
if (in && out) {
    while (fgets(buf, sizeof buf, in) != NULL) {
        fputs(buf, out);
    }
    fclose(in);
    fclose(out);
}

For an exact byte-for-byte copy (binary files or to avoid newline translation), use fread/fwrite and open with "rb"/"wb":

char buf[8192];
size_t n;
while ((n = fread(buf, 1, sizeof buf, in)) > 0) {
    fwrite(buf, 1, n, out);
}

Quick debugging checklist:

  • Confirm fopen modes: no "a" unless appending is intended.
  • Replace while(!feof(...)) with read-return checks shown above.
  • Search the menu code for missing break in switch or duplicate calls to the copy function.
  • Add simple log prints or increment a write-counter to see how many times data is written.
  • Check return values (fopen, fgets, fread) and run under a debugger to watch control flow through the menu.

These steps fix the vast majority of duplicated-output bugs in file-copy routines.

Recommended Answers

All 3 Replies

Member Avatar for Member #46692

>i am over this for a few hours now and i can figure it out

If you can figure it out I don't see what the problem is. ;)

[IMG][/IMG]
Piworld ™
[Tis simple as Pie]

could not get through too :(

i ment - i can't figure it :) LOL

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.