Hello all, I am currently writing a code for an assignment I have and was just n
ID: 3810032 • Letter: H
Question
Hello all, I am currently writing a code for an assignment I have and was just needed some help resolving an issue. When compiled, my write document is full of endless "ÿ" character. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fPointer_Read;
FILE * fPointer_Write;
fPointer_Read = fopen("pj3read.txt","r");
fPointer_Write = fopen("pj3write.txt","w");
char singleLine[150];
char write_variable;
int count = 1;
while(fgets(singleLine, sizeof(singleLine), fPointer_Read)){
printf("%03d: %s", count++, singleLine);
}
do{
write_variable = fgetc(fPointer_Read);
fputc(write_variable, fPointer_Write);
}while (write_variable = EOF);
return 0;
}
Explanation / Answer
The while condition is not proper and u have to close the pointers after operations is completed
#include<stdio.h>
int main() {
FILE *fPointer_Read, *fPointer_Write,*fPointer_Rd; //additional pointer to read and print on screen
char write_variable;
char singleLine[150];
int count = 1;
fPointer_Read = fopen("pr.txt", "r");
fPointer_Rd = fopen("pr.txt", "r");
if (fPointer_Read == NULL) { //error handling
puts("cannot open this file");
}
fPointer_Write = fopen("pw.txt", "w");
if (fPointer_Write == NULL) {
puts("Not able to open this file");
fclose(fPointer_Read);;
}
while (1) {
if(fgets(singleLine, sizeof(singleLine), fPointer_Rd)==NULL){ // fPointer this is to print on screen
break;}
else{
printf("%03d: %s", count++, singleLine);
}
}
do{
write_variable = fgetc(fPointer_Read); //read the input file using fPointer_Read
fputc(write_variable, fPointer_Write);
} while (write_variable != EOF); //check till EOF
fcloseall(); //close all the file pointers
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.