我想读一个.csv的文件.我已经学习了解析和使用strtok来解析.我也已经写了代码,但我的代码不能正常工作.我的代码只读取了一部分数据到 struct 体的数组中,而另一部分没有写入 struct 体的数组中.我的代码也奇怪地从第2931行读取数据,而不是从头开始.

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char location[100];
    char city[100];
    int price;
    int room;
    int bathroom;
    int carpark;
    char type[100];
    char furnish[100];
} data;

int main() {

    data house[4000];

    FILE *file = fopen("data.csv", "r");
    char buffer[1000];
    fgets(buffer, 1000, file);
    int n = 0; 
    while (fgets(buffer, 1000, file)) {
        char *token = strtok(buffer, ",");
        strcpy(house[n].location, token);

        token = strtok(NULL, ",");
        strcpy(house[n].city, token);

        token = strtok(NULL, ",");
        house[n].price = atoi(token);

        token = strtok(NULL, ",");
        house[n].room = atoi(token);

        token = strtok(NULL, ",");
        house[n].bathroom = atoi(token);

        token = strtok(NULL, ",");
        house[n].carpark = atoi(token);

        token = strtok(NULL, ",");
        strcpy(house[n].type, token);

        token = strtok(NULL, "\n");
        strcpy(house[n].furnish, token);

        n++;
    }

    fclose(file);

    for (int i = 0; i < n; i++) {
         printf("%s,%s,%d,%d,%d,%d,%s,%s\n",
                house[i].location, house[i].city, house[i].price,
                house[i].room, house[i].bathroom, house[i].carpark,
                house[i].type, house[i].furnish);
    }

    return 0;
}

这是data.csv的链接,我的程序从实际的3940个数据中读取了大约This is the image of comparison.0个数据.This is the image of comparison.

This is from the terminal, which shown the data from line 2931, not from the beginning.

我想我必须提到这一点:在成功读取数据后,我想要做的是创建一个包含like this个菜单的菜单.所以程序可以做一些事情,比如显示数据,搜索数据,排序数据,并在排序后导出数据.在我的代码中使用这样的 struct 数组来完成我提到的所有任务是否已经正确?或者我可以换成更有效、更高效的东西?

推荐答案

要读取文件数据,可以使用fgetslib

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
 
const char* getfield(char* line, int num)
{
    const char* data;
    for (data = strtok(line, ";");
            data && *data;
            data = strtok(NULL, ";\n"))
    {
        if (!--num)
            return data;
    }
    return NULL;
}
 
int main()
{
    char line[1024];
    while (fgets(line, 1024, stdin))
    {
        char* tmp = strdup(line);
        printf("Field 3 would be %s\n", getfield(tmp, 3));
        // NOTE strtok clobbers tmp
        free(tmp);
    }
}

要将css写入文件,可以使用cout方法或ofstream



#include <iostream>
#include <fstream>



int main()
{

    std::ofstream Myfile("boogaloo.txt");

    int width =10, height =10;
      

    for (int w = 0; w< width;w++)
    {   
        for (int h = 0; h < height; h++)
        {
            Myfile << h<< ",";
        }

        Myfile << "\n ";
    }

    Myfile.close();
    return 0;
}

your code looks good to me however you console is running out of lines to print the data to . so click on the console if on windows go to proporties and increase the buffer size to print to . I did it with buffer height . enter image description here

C++相关问答推荐

漏洞仅出现在FreeBSD上,但在Windows、Linux和MacOS上运行得非常好

找出文件是否包含给定的文件签名

如何在C中只使用一个带双方括号([i][j])访问语法的malloc来分配动态大小的2d数组?

使用C时,Windows CMD中的argc参数是否包含重定向命令?

在C语言中,在数学运算过程中,为什么浮点数在变量中的行为不同

是否需要包括<;errno.h>;才能使用perror?

如何仅使用软件重新初始化STM32微控制器中的USB枚举?

为 struct 中的数组动态分配内存时出错

我在C中运行和调试时得到了不同的输出

初始成员、公共初始序列、匿名联合和严格别名如何在C中交互?

如何在c中使用具有不同变量类型的内存分配?

如果格式字符串的内存与printf的一个参数共享,会发生什么情况?

Valgrind用net_pton()抱怨

为什么我的二叉树删除删除整个左部分的树?

*S=0;正在优化中.可能是GCC 13号虫?或者是一些不明确的行为?

在吉陀罗中,_2_1_和CONCAT11是什么意思?

C中2个数字的加法 - 简单的人类方法

无法理解 fgets 输出

一元运算符

如何让 unlinkat(dir_fd, ".", AT_REMOVEDIR) 工作?