如何修复 fprintf 循环?
How to fix the fprintf loop?
我有一个这样写的 .txt 文件:
PersonName colour1 colour2 colour3
例如:
Nick blue green black
Lisa orange yellow green
Mark white blue orange
我的程序:editColour(fileName, colourInTheFile, colourIWant)
应该用 colourIWant 和 return 更改 colourInTheFile 的次数。问题是它开始循环
#include <stdio.h>
#include <string.h>
typedef char String[100];
int editColour(String fileName,String current_colour,String new_colour){
String name,c1,c2,c3;
int num_s = 0; /*number of occurence*/
FILE *fp = fopen(fileName,"r");
/*this is just a test, so i'm tryng to put the fileName content in this one*/
FILE *ftemp = fopen("/Users/valerio/Desktop/PROGRAMMI/C/Lezione12_esercizio1/Lezione12_esercizio1/file.txt","w+");
long pos;
while(!feof(fp)){
pos = ftell(fp);
fscanf(fp, "%s %s %s %s\n",name,c1,c2,c3);
if(strcmp(c1, current_colour)==0){
fseek(fp,pos,SEEK_SET);
fprintf(ftemp, "%s %s %s %s\n",name,new_colour,c2,c3);
num_s++;
}
if(strcmp(c2, current_colour)==0){
fseek(fp,pos,SEEK_SET);
fprintf(ftemp, "%s %s %s %s\n",name,c1,new_colour,c3);
num_s++;
}
if(strcmp(c3, current_colour)==0){
fseek(fp,pos,SEEK_SET);
fprintf(ftemp, "%s %s %s %s\n",name,c1,c2,new_colour);
num_s++;
}
fprintf(ftemp, "%s %s %s %s\n",name,c1,c2,c3);
}
fclose(fp);
fclose(ftemp);
return num_s;
}
int main(int argc, const char * argv[]) {
printf("%d\n\n", editColour("/Users/valerio/Desktop/PROGRAMMI/C/Lezione12_esercizio1/Lezione12_esercizio1/social_users copia.txt",
"orange", "poop"));
}
预期:
Nick blue green black
Lisa poop yellow green
Mark white blue poop (in the new file)
实际:
Nick blue green black
Lisa poop yellow green
Lisa orange yellow green
Lisa poop yellow green
Lisa orange yellow green
Lisa poop yellow green
Lisa orange yellow green
Lisa poop yellow green
Lisa orange yellow green
.
.
.
循环中
以下建议代码:
- 干净地编译
- 执行所需的功能
- 正确检查错误
- 避免错误的说法:
while( !feof(fp) )
现在,建议的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHAR 100
int editColour( char fileName[], char current_colour[], char new_colour[] )
{
char name[ MAX_CHAR ];
char c1[ MAX_CHAR ];
char c2[ MAX_CHAR ];
char c3[ MAX_CHAR ];
int num_s = 0; /*number of occurence*/
FILE *fp = fopen(fileName,"r");
if( !fp )
{
perror( "fopen for input file failed" );
exit( EXIT_FAILURE );
}
FILE *ftemp = fopen("/Users/valerio/Desktop/PROGRAMMI/C/Lezione12_esercizio1/Lezione12_esercizio1/file.txt","w");
if( !ftemp )
{
perror( "fopen for output file failed" );
fclose( fp );
exit( EXIT_FAILURE );
}
while( fscanf( fp, "%99s %99s %99s %99s\n", name, c1, c2, c3 ) == 4 )
{
if( !strcmp( c1, current_colour ) )
{
fprintf( ftemp, "%s %s %s %s\n", name, new_colour, c2, c3 );
num_s++;
}
if( !strcmp( c2, current_colour ) )
{
fprintf( ftemp, "%s %s %s %s\n", name, c1, new_colour, c3 );
num_s++;
}
if( !strcmp( c3, current_colour ) )
{
fprintf( ftemp, "%s %s %s %s\n", name, c1, c2, new_colour );
num_s++;
}
else
{
fprintf( ftemp, "%s %s %s %s\n", name, c1, c2, c3 );
}
}
fclose(fp);
fclose(ftemp);
return num_s;
}
int main( void )
{
printf("%d\n\n",
editColour("/Users/valerio/Desktop/PROGRAMMI/C/Lezione12_esercizio1/Lezione12_esercizio1/social_users copia.txt",
"orange", "poop"));
}
我有一个这样写的 .txt 文件:
PersonName colour1 colour2 colour3
例如:
Nick blue green black
Lisa orange yellow green
Mark white blue orange
我的程序:editColour(fileName, colourInTheFile, colourIWant) 应该用 colourIWant 和 return 更改 colourInTheFile 的次数。问题是它开始循环
#include <stdio.h>
#include <string.h>
typedef char String[100];
int editColour(String fileName,String current_colour,String new_colour){
String name,c1,c2,c3;
int num_s = 0; /*number of occurence*/
FILE *fp = fopen(fileName,"r");
/*this is just a test, so i'm tryng to put the fileName content in this one*/
FILE *ftemp = fopen("/Users/valerio/Desktop/PROGRAMMI/C/Lezione12_esercizio1/Lezione12_esercizio1/file.txt","w+");
long pos;
while(!feof(fp)){
pos = ftell(fp);
fscanf(fp, "%s %s %s %s\n",name,c1,c2,c3);
if(strcmp(c1, current_colour)==0){
fseek(fp,pos,SEEK_SET);
fprintf(ftemp, "%s %s %s %s\n",name,new_colour,c2,c3);
num_s++;
}
if(strcmp(c2, current_colour)==0){
fseek(fp,pos,SEEK_SET);
fprintf(ftemp, "%s %s %s %s\n",name,c1,new_colour,c3);
num_s++;
}
if(strcmp(c3, current_colour)==0){
fseek(fp,pos,SEEK_SET);
fprintf(ftemp, "%s %s %s %s\n",name,c1,c2,new_colour);
num_s++;
}
fprintf(ftemp, "%s %s %s %s\n",name,c1,c2,c3);
}
fclose(fp);
fclose(ftemp);
return num_s;
}
int main(int argc, const char * argv[]) {
printf("%d\n\n", editColour("/Users/valerio/Desktop/PROGRAMMI/C/Lezione12_esercizio1/Lezione12_esercizio1/social_users copia.txt",
"orange", "poop"));
}
预期:
Nick blue green black
Lisa poop yellow green
Mark white blue poop (in the new file)
实际:
Nick blue green black
Lisa poop yellow green
Lisa orange yellow green
Lisa poop yellow green
Lisa orange yellow green
Lisa poop yellow green
Lisa orange yellow green
Lisa poop yellow green
Lisa orange yellow green
.
.
.
循环中
以下建议代码:
- 干净地编译
- 执行所需的功能
- 正确检查错误
- 避免错误的说法:
while( !feof(fp) )
现在,建议的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHAR 100
int editColour( char fileName[], char current_colour[], char new_colour[] )
{
char name[ MAX_CHAR ];
char c1[ MAX_CHAR ];
char c2[ MAX_CHAR ];
char c3[ MAX_CHAR ];
int num_s = 0; /*number of occurence*/
FILE *fp = fopen(fileName,"r");
if( !fp )
{
perror( "fopen for input file failed" );
exit( EXIT_FAILURE );
}
FILE *ftemp = fopen("/Users/valerio/Desktop/PROGRAMMI/C/Lezione12_esercizio1/Lezione12_esercizio1/file.txt","w");
if( !ftemp )
{
perror( "fopen for output file failed" );
fclose( fp );
exit( EXIT_FAILURE );
}
while( fscanf( fp, "%99s %99s %99s %99s\n", name, c1, c2, c3 ) == 4 )
{
if( !strcmp( c1, current_colour ) )
{
fprintf( ftemp, "%s %s %s %s\n", name, new_colour, c2, c3 );
num_s++;
}
if( !strcmp( c2, current_colour ) )
{
fprintf( ftemp, "%s %s %s %s\n", name, c1, new_colour, c3 );
num_s++;
}
if( !strcmp( c3, current_colour ) )
{
fprintf( ftemp, "%s %s %s %s\n", name, c1, c2, new_colour );
num_s++;
}
else
{
fprintf( ftemp, "%s %s %s %s\n", name, c1, c2, c3 );
}
}
fclose(fp);
fclose(ftemp);
return num_s;
}
int main( void )
{
printf("%d\n\n",
editColour("/Users/valerio/Desktop/PROGRAMMI/C/Lezione12_esercizio1/Lezione12_esercizio1/social_users copia.txt",
"orange", "poop"));
}