我想写一个名为 print array 的方法,它有 2 个参数,如下所示。
我想(很可能使用 for 循环)遍历字符数组并将每个字符传递到输出文本文件,以便在该文件的同一行上打印。所以如果数组有 a, b, c, d, e, f, g。在我希望它显示 abcdefg 的文件中。我真的不知道如何开始。
void printArray(char * array, FILE * fout)
{
//i think using a for loop is the way to go, i just dont know exactly what to do after
}
最佳答案
试试这个:
void printArray(char * array, FILE * fout, int MAX_CHAR)
{
int i;
fout = fopen("file.txt","a+"); /* open the file in append mode */
for (i=0; i<MAX_CHAR; i++)
fprintf(file,"%c",*(array+i)); /* write */
fclose(file); /* close the file pointer */
return 0;
}
关于c - 将数组中的字符打印到 C 中的输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9697137/