c不遵循程序操作步骤

时间:2019-05-08 14:02:46

标签: c fopen

摘要:system(“ clear”);运行不正常。

我正在使用gcc,ubuntu 18.04 LTS版本进行c编程。

我打算做的是“读取每个单词并从两个文本文件中打印。完成读取文件后,延迟3秒钟并擦除端子”

所以我制作了两个文本文件,并使用system(“ clear”);擦除终端。

这是完整的代码。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

void printFiles(char *file1,char *file2,char *change1, char *change2){
  FILE *f;
  char *text = malloc(sizeof(char)*100);
  f=fopen(file1,"r");

  system("clear");
  //while(!feof(f)){
  while(EOF!=fscanf(f,"%s",text)){
    //fscanf(f,"%s", text);
    printf("%s ",text);
    //usleep(20000);
  }
  //sleep(3);

  fclose(f);
  printf("\n");
  //all comment problems are appear here. and if I give delay, such as usleep() or sleep, delay also appear here. Not appear each wrote part.

  f=fopen(file2,"r");
  //while(!feof(f)){
  while(EOF!=fscanf(f,"%s",text)){
    if(strcmp(text,"**,")==0){
      strcpy(text,change1);
      strcat(text,",");
    }

    else if(strcmp(text,"**")==0){
      strcpy(text,change1);
    }
    else if(strcmp(text,"##.")==0){
      strcpy(text,change2);
      strcat(text,".");
    }
    else if(strcmp(text,"##,")==0){
      strcpy(text,change2);
      strcat(text,",");
    }
    printf("%s ",text);
    //usleep(200000);
  }
  fclose(f);
  free(text);
  sleep(3); //here is problem. This part works in the above commented part "//all comment problems are appear here."
  system("clear"); //here is problem. This part works in the above commented part "//all comment problems are appear here."
}


int main(){
  char file1[100] = "./file1.txt";
  char file2[100] = "./file2.txt";
  char change1[100]="text1";
  char change2[100]="text2";
  printFiles(file1,file2,change1,change2);
  return 0;
}

非常抱歉,文件和变量名称由于政策而更改。此外,文件内容也无法上传。

我找不到哪个部分中断了面向过程的编程。我认为那是编译器错误,因为使用一个文件读取并且system(clear);效果很好。

我还创建了两个点变量,例如'FILE * f1;文件* f2; f1 = fopen(文件1); f2 = fopen(file2)...`,但结果相同。

是编译器错误吗?如果是,我该怎么做才能解决这些问题?谢谢。

2 个答案:

答案 0 :(得分:1)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

void printFiles(char *file1,char *file2,char *change1, char *change2){
  FILE *f;
  char *text = malloc(sizeof(char)*100);
  f=fopen(file1,"r");

  system("clear");
  //while(!feof(f)){
  while(EOF!=fscanf(f,"%s",text)){
    //fscanf(f,"%s", text);
    printf("%s ",text);
    fflush(stdout);
    //usleep(20000);
  }
  //sleep(3);

  fclose(f);
  printf("\n");
  //all comment problems are appear here. and if I give delay, such as usleep() or sleep, delay also appear here. Not appear each wrote part.

  f=fopen(file2,"r");
  //while(!feof(f)){
  while(EOF!=fscanf(f,"%s",text)){
    if(strcmp(text,"**,")==0){
      strcpy(text,change1);
      strcat(text,",");
    }

    else if(strcmp(text,"**")==0){
      strcpy(text,change1);
    }
    else if(strcmp(text,"##.")==0){
      strcpy(text,change2);
      strcat(text,".");
    }
    else if(strcmp(text,"##,")==0){
      strcpy(text,change2);
      strcat(text,",");
    }
    printf("%s ",text);
    fflush(stdout);// The answer. 
    //usleep(200000);
  }
  fclose(f);
  free(text);
  sleep(3); //here is problem. This part works in the above commented part "//all comment problems are appear here."
  system("clear"); //here is problem. This part works in the above commented part "//all comment problems are appear here."
}


int main(){
  char file1[100] = "./file1.txt";
  char file2[100] = "./file2.txt";
  char change1[100]="text1";
  char change2[100]="text2";
  printFiles(file1,file2,change1,change2);
  return 0;
}

提示 That's probably just buffering. Do fflush(stdout); before you sleep. – melpomene 谢谢。

答案 1 :(得分:0)

您可以尝试此解决方案以延迟

#include <time.h>
#include <stdio.h>

void delay(double seconds)
{
    const time_t start = time(NULL);
    time_t current;

    do
    {
        time(&current);
    } while(difftime(current, start) < seconds);
}

int main(void)
{
    printf("Just waiting...\n");
    delay(3);
    printf("...oh man, waiting for so long...\n");
    return 0;
}

以下解决方案与上一个解决方案非常相似,但具有清晰的终端解决方案。

#include <time.h>
#include <stdio.h>

#ifdef _WIN32
    #define CLEAR_SCREEN system ("cls");
#else
    #define CLEAR_SCREEN puts("\x1b[H\x1b[2J");
#endif

void delay(double seconds)
{
    const time_t start = time(NULL);
    time_t current;

    do
    {
        time(&current);
    } while(difftime(current, start) < seconds);
}

int main(void)
{
    printf("Just waiting...\n");
    delay(2); //seconds
    printf("...oh man, waiting for so long...\n");
    delay(1);
    CLEAR_SCREEN
    return 0;
}