c - 将数组中的字符打印到 C 中的输出文件

标签 c arrays file

我想写一个名为 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/

相关文章:

javascript - 关闭从点数组生成的打开的 Catmull-Rom 样条曲线,并具有平滑曲线

python - 为什么一个文件对象刷新,而另一个不刷新?

javascript - 数组包含通过引用调用的值,因此它们的指针应该变成相同的值并等于 true

c - 如何在 Postgres 的 C 语言函数中返回位(n)或位变化(n)字段?

bash - 在shell脚本中同时从两个文件中逐行读取

python - 在 Python 3 中打开文件、重新格式化并写入新文件

c - 遍历给出段错误的字符列表

c - 如何告诉 Make 忽略尚未更新的文件?

Android NDK 混合 C 和 C++ 错误未定义对 mult(int, int) 的引用

c++ - 安装 GCC : fatal error: gnu/stubs-32. h: 没有那个文件或目录