文件在C中没有正确打印?

时间:2014-04-11 11:31:27

标签: c string file text integer

所以这里我有一个基本程序,它将写入文件中的特定行,方法是将文件内容写入写入新行的临时文件中,然后将该文件的内容复制回起始文件。

(分数)=档案 (Sub)= Temp

#include <stdio.h>
#include <conio.h>
#include <string.h>
void insert(void);

int main()
{
   insert();
}    

void insert(void)
{
    FILE *fp,*fc;
    int lineNum;  
    int count=0;  
    char ch=0;   
    int edited=0; 
    int score=0;  


    fp=fopen("Test 02 Scores.txt","r");
    fc=fopen("Sub.txt","w");

    if(fp==NULL||fc==NULL)
    {
        printf("\nError...cannot open/create files");
        exit(1);
    }
    printf("Enter the score");
    scanf("%d",&score);

    printf("\nEnter Line Number Which You Want 2 edit: ");
    scanf("%d",&lineNum);

    while((ch=fgetc(fp))!=EOF)
    {
        if(ch=='\n')  
            count++;
        if(count==lineNum-1 && edited==0)   
        {
            if(lineNum==1)
            {
            fprintf(fc,"%d\n",score);
            }
            else 
            fprintf(fc,"\n%d\n",score);

            edited=1;  

            while( (ch=fgetc(fp))!=EOF )  
            {                           
                if(ch=='\n')
                    break;
            }
       }
       else
          fprintf(fc,"%d",ch);
    }
    fclose(fp);
    fclose(fc);

    if(edited==1)
    {
        printf("\nLine has been written successfully.");

         char ch;
         FILE *fs, *ft;

         fs = fopen("Sub.txt", "r");

         if( fs == NULL )
         {
         printf("File is not real");
         exit(1);
         }

         ft = fopen("Test 02 Scores.txt", "w");

         if( ft == NULL )
         {
         fclose(fs);
         printf("File is not real\n");
         exit(1);
         }

         while( ( ch = fgetc(fs) ) != EOF )
         fputc(ch,ft);

         printf("\nFile copied\n");
         getch();

         fclose(fs);
         fclose(ft);

    }
    else
    printf("\nLine Not Found");

}

然而,出现问题,我开始编写此代码用于字符串,但由于决定使用数字值,每当我尝试使用整数值进行复制时程序将不会复制任何正确的内容,我知道这可能由char到int引起但我更愿意帮助评估我应该在哪里更改内容。

1 个答案:

答案 0 :(得分:2)

错误在此行

fprintf(fc,"%d",ch)

%d将ch打印为整数,而不是字符,您应该写

fprintf(fc,"%c",ch)

或使用fputc()

您的代码存在一些小问题,这是一个可用的版本。我在改变的地方添加了评论。

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>  // needed for exit()
void insert(void);

int main()
{
  insert();
}

// use fgets to read from keyboard, it is simpler.
int readNumber()
{
  char buffer[64] = {0};
  fgets(buffer, sizeof(buffer), stdin);
  return atoi(buffer);
}

void insert(void)
{
  FILE *fp = NULL; // prefer one decl per row
  FILE *fc = NULL;
  int lineNum = 0;  
  int count=0;  
  int ch=0;     // should be int ch=0;
  int edited=0; 
  int score=0;  

  // file names  
  const char src[] = "Test 02 Scores.txt";
  const char dest[] = "Sub.txt";

  fp=fopen(src,"r");

  if(fp==NULL)
  {
    perror(src); // use perror() instead for better error msg
    exit(EXIT_FAILURE); // there are std constants for exit args
  }

  fc=fopen(dest,"w");

  if(fc==NULL)
  {
    perror(dest); 
    exit(EXIT_FAILURE);
  }

  printf("Enter the score: ");
  score = readNumber(); // using fgets to avoid lingering \n in buffer

  printf("\nEnter Line Number Which You Want 2 edit: ");
  lineNum = readNumber();

  while((ch=fgetc(fp))!=EOF) // fgetc returns int so ch should be int
  {
    if(ch=='\n')  // better to have {} here too
    {
      count++;
    }

    if(count==lineNum-1 && edited==0)   
    {
      if(lineNum==1)
      {
        fprintf(fc,"%d\n",score);
      }
      else // better to { } here too
      {
        fprintf(fc,"\n%d\n",score);
      }

      edited=1;  

      // i guess you want to remove old score
      while( (ch=fgetc(fp))!=EOF )  
      {                           
        if(ch=='\n')
        {
          break;
        }
      }
    }
    else // {} for avoiding future pitfall
    {
      fputc(ch,fc);
    }
  }

  fclose(fp);
  fclose(fc);

  if(edited==1)
  {
    puts("\nLine has been written successfully."); // puts() when u can

    int ch = 0; // int
    FILE *fs = NULL; 
    FILE *ft = NULL;

    fs = fopen(dest, "r");

    if( fs == NULL )
    {
      perror(dest);
      exit(EXIT_FAILURE);
    }

    ft = fopen(src, "w");

    if( ft == NULL )
    {
      perror(src);
      exit(EXIT_FAILURE); // at program exit files will close anyway
    }

    while( ( ch = fgetc(fs) ) != EOF )
    {
      fputc(ch,ft); 
    }

    fclose(fs);
    fclose(ft);

    printf("\nFile copied\n");
    getch();
  }
  else
  {
    printf("\nLine Not Found");
  }
}