小编Mar*_*rla的帖子

无法在c中使用for循环写入文本文件

我在将字符串写入txt文件时遇到问题.我的线路每次都会被覆盖.我
gcc -Wall -o filename filename.c用来编译和./filename 10 Berlin cat resultat.txt执行.txt文件总是只有一行(最后一行)如何保存所有记录.

我有一个包含城市名称和一些居民的CSV文件,我需要过滤城市名称和最少的居民.

到目前为止我尝试了什么:

.....
void write_file(char *result[], int len) {
   FILE *fp = fopen("resultat.txt", "w");
   if (fp == NULL){
       perror("resultat.txt");
       exit(1);
   }
   for (int i=0; i<len; i++) {
       fprintf(fp, "%s\n", result[i]);
   }
   fclose(fp);
}

int main(int argc,char **argv) {

    int anzahl = atoi(argv[1]);
    char *string_array[100];

    char *erste_zeile;
    erste_zeile = (char *) malloc(1000 * sizeof(char));

    char staedte[MAX_LAENGE_ARR][MAX_LAENGE_STR];
    char laender[MAX_LAENGE_ARR][MAX_LAENGE_STR]; 
    int bewohner[MAX_LAENGE_ARR];

    int len = read_file("staedte.csv", staedte, laender, bewohner);
    for …
Run Code Online (Sandbox Code Playgroud)

c malloc for-loop c-strings writefile

2
推荐指数
1
解决办法
2061
查看次数

标签 统计

c ×1

c-strings ×1

for-loop ×1

malloc ×1

writefile ×1