• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Stupid C error

Sayth

Gawd
Joined
Oct 7, 2001
Messages
618
I have a program that reads a file, then writes thhe contents to another file, but I keep getting this stupid ÿ thing at the end of the output file. Any ideas?
Here's my code

#include <stdio.h>
int main()
{
char ifilename[] = "i:/emplistin.txt";
char ofilename[] = "i:/emplistout.txt";
char name;
char ch;
FILE *ofp, *ifp;

{
/* Open file for input */
ifp = fopen(ifilename,"r");

/* Open file for output */
ofp = fopen(ofilename,"w");

while (ch != EOF)


{
/* Read data */
ch = fgetc(ifp);

/* Write out data */
fputc(ch, ofp);

}

/* Close Files */
fclose(ifp);
fclose(ofp);
}

printf("hit enter to end program");
getchar(ch);

return 0;
}
 
You are printing out EOF to your output file.

-btw why the random bracket block in the middle of your code?
 
yeah don't know why that was there... thanks for pointing it out

OKay so how do I not print the EOF? I have to stop the reading at the EOF somehow and the only place I see it in my code is where I want the loop to stop.
 
I cant just *tell* you with the homework policy etc, best advice I can give you would be to open your book and look over looping constructs and flow control.
 
ah yeah I see.. hardforums has become so tightass it's not funny... I remember the good old days... General discussion was the best. Ah well thanx anyways.
 
Hint: "int fgetc(FILE *stream);"

Might also want to think over what happens with an empty file. That'd solve your control-flow problem.
 
okay thanx guys I fixed my problem, but come accross another...
Is it possible to make the 3 in this line a variable?

fputc(ch+3, ofp);
 
Back
Top