0
void printArray(char * array, FILE * fp1, int MAX_CHAR)
{ 
 int i; /* initializing i */
 FILE *fp1
 fp1 = fopen("Array.txt","r+");      /* open the file in append mode */
 for (i=0; i<MAX_CHAR; i++)           /* using for loop */
      fprintf(fp1,"%c",*(array+i)); /* writing the array */ 
 fclose(fp1);                       /* close the file pointer */ 

 return 0; 
}

我是 C 新手,如果我这样做正确,有人可以告诉我吗

4

1 回答 1

0

你在这个函数中遇到了一些错误。这是我的建议:

void printArray(char *array, const int MAX_CHAR)
{ 
    FILE *fp1;
    fp1 = fopen("Array.txt", "a");      /* open the file in append mode */
    for (int i=0; i<MAX_CHAR; i++)           /* using for loop */
       fprintf(fp1,"%c",*(array+i)); /* writing the array */ 
    fclose(fp1);                       /* close the file pointer */ 
}

关于打开文件的模式:fopen 函数。如果您真的想fp1作为输入参数传递,请执行以下操作:

void printArray(char *array, FILE *fp1, const int MAX_CHAR)
{ 
    fp1 = fopen("Array.txt", "a");      /* open the file in append mode */
    for (int i=0; i<MAX_CHAR; i++)           /* using for loop */
       fprintf(fp1,"%c",*(array+i)); /* writing the array */ 
    fclose(fp1);                       /* close the file pointer */ 
}

最后,我建议你看看fwrite 函数

于 2013-11-08T00:22:40.680 回答