I have a program that is supposed edit strings in a text document, but before I
ID: 3620196 • Letter: I
Question
I have a program that is supposed edit strings in a text document, but before I try and figure out how to edit the strings, I want to know how to copy a file to another file.I haven't taken C programming for 2.5 years, and this new prof expects us to be pretty good in C programming....
here is an attempt that I made after finding different examples..
#include<stdio.h>
int
main()
{
FILE *city;
FILE *editcity;
char buffer[200];
city=fopen("c://citiesin.txt", "r");
editcity=fopen("c://citiesout.txt", "w");
while(fgets(buffer, 200, city)!=0)
fprintf(editcity, "%s",buffer);
fclose(city);
fclose(editcity);
getche();
return 0;
}
I don't think I'm using the fgets right since I'm unsure what the buffer is.
Thanks for your help
Explanation / Answer
please rate - thanks you are doing it almost perfectly. the textbook definition of fgets is #include<stdio.h>int
main()
{
FILE *city;
FILE *editcity;
char buffer[200];
city=fopen("c://citiesin.txt", "r");
if(city == NULL)
{ printf("Error opening input file! ");
getche();
return 0;
}
editcity=fopen("c://citiesout.txt", "w");
while(fgets(buffer, 200, city)!=NULL)
fprintf(editcity, "%s",buffer);
fclose(city);
fclose(editcity);
getche();
return 0;
}
#include<stdio.h>
int
main()
{
FILE *city;
FILE *editcity;
char buffer[200];
city=fopen("c://citiesin.txt", "r");
if(city == NULL)
{ printf("Error opening input file! ");
getche();
return 0;
}
editcity=fopen("c://citiesout.txt", "w");
while(fgets(buffer, 200, city)!=NULL)
fprintf(editcity, "%s",buffer);
fclose(city);
fclose(editcity);
getche();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.