I've managed to open a file and read while writing to another file with var=fopen(file,"r")
/ "w"
but even with r+ or w+ moded I can't open a file and alter its contents.
Imagine this:
int formatacao (char original[]){/*se a cadeia nao tiver escrita em maiusculas, esta fun?ao vai alteralas para tal*/
int val1;
FILE * original_open;
original_open = fopen (original,"r+");
if (original_open==0){
printf ("ficheiro %c 1.",original);
}
while ((val1=fgetc(original_open))!=EOF){
if (val1>='a'&&val1<='z'&&val1){
fputc(val1-32,original_open);
}
else
fputc(val1,original_open);
}
fclose (original_open);
return (0);
}
Code works, no errors, no warning, only problem is: it erases the contents on the file if I use it like this BUT this works:
int main (){
int val1,val2,nr=0;
FILE* fp1;
FILE* fp2;
fp1=fopen ("DNAexample.txt","r");
fp2=fopen ("DNAexample1.txt","w");
if (fp1==0){
printf ("EPIC FAIL no 1.
");
}
while ((val1=fgetc(fp1))!=EOF){
if (val1>='a'&&val1<='z'&&val1){
fputc(val1-32,fp2);
}
else
fputc(val1,fp2);
}
fclose (fp1);
fclose (fp2);
return (0);
}
Flawlessly! How can I open a file, read char by char and decide if I want to change the char or not?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…