[英]a program that reads in a variable-length array from a file using loops
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的新手,有人可以讓我知道我是否正確執行此操作
您在此功能中遇到了一些錯誤。 這是我建議您的:
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函數 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.