简体   繁体   English

如何将每一行放入从 c 程序中的文件读取的数组中?

[英]How do I put each line in an array which read from the file in c program?

int main(int argc, char *argv)
{
    char filename[20]={};
    int count=0;

    if(argc==2)
    {
        strcpy(filename, argv[1]);
    }
    else
    {
        printf("Need 2 command line parameters\n");
    }

    FILE *fp=fopen(argv[1], "r");

    while(fgets(filename, 20, fp))
    {
        count++;
    }

    fseek(fp, 0, SEEK_SET);

    int ptr=(int)malloc(count*sizeof(int));
    while(fgets(filename, 20, fp))
    {
        ptr[i]=
    }

    return 0;

for example, my numbers in file are 19293 18239 19405 29302 10492 in each line, and I would like to put each line in an array.例如,我在文件中的数字是 19293 18239 19405 29302 10492 在每一行,我想把每一行放在一个数组中。 How do I do this?我该怎么做呢? It doesn't have to be in a while loop.它不必在一个while循环中。

I think your question is that you want to store the content you read as a string each line.Ok, there are serveal methods to realize it.我认为您的问题是您希望将读取的内容存储为每行的字符串。好的,有多种方法可以实现它。 If you don't care the effenciency, you can define a char pointer array as max as possible to contain all lines in your files.如果您不关心效率,您可以定义一个 char 指针数组,使其尽可能最大,以包含文件中的所有行。 And you can do like this:你可以这样做:

char* content[MAX_LINE] = { NULL };
char tmp[20];
char* ptr;
int i=0;
size_t ret;
FILE* fp;
fopen_s(&fp,"1.txt", "r");
while (fgets(tmp, 20, fp) != NULL)
{
    ret = strlen(tmp);
    ptr = (char*)malloc(ret+1);
    strcpy_s(ptr, ret+1, tmp);
    content[i++] = ptr;
}

And the better way is using pointer list.更好的方法是使用指针列表。

If you want to read and store the whole content of your file in an array, you can do this in this simple way, though there are also other ways:如果您想读取文件的全部内容并将其存储在一个数组中,您可以通过这种简单的方式执行此操作,但还有其他方式:

while( ( c = getc(file) ) != EOF )
  buffer[i++] = c;

where buffer is an char array of 1000 elements (it could be any number, you want), c is a char , and i is an int initialized with 0 .其中buffer1000 elementschar数组(可以是任何数字,你想要), c是一个chari是一个用0初始化的int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从C中的文件中读取每一行到一个整数数组? - How to read each line into an integer array, from a file in C? 从文件中读取每一行,然后在C中将该行拆分为一个字符串和一个数组 - Read each line from a file and split the line into a string and an array in C 如何从C中的文件中保存我在数组中读取的数据 - How to save the data which i read in an array from a file in C 如何从c中的文件读取一行? - How do I read a line from a file in c? 从文件中读取行并将每个单词存储到数组(C语言)中 - read line from file and store each word into an array (C language) 如何将文件中的每个字符存储到 c 中的数组中 - How do I store each char from a file into an array in c 如何读取C文件中方括号内的字符串,每行包含多个这样的字符串 - How do I read a string enclosed within square braces in a file in C with each line containing several such strings 如何使我的C程序从文件中读取多行文本? - How can I get my C program to read more than one line of text from a file? C 从文件中读取文本并将其放入数组 - C Read text from file and put it into array 如何使用分隔符分割我从文件中读取的行并将它们存储在 C 的每个 int 变量中 - How to split line I read from file with delimiters and store them in each int variable in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM