简体   繁体   English

如何从C语言中读取文件并将其保存为memory中的字符串,获取存储的字符串并将其切割成特定长度? 关于 linux LF 格式

[英]How do I read files from language C and save them as strings in memory, get the stored strings and cut them into a specific length? on linux LF format

I want to approach the string as an array, cut it to a specific length, and store it in a two-dimensional array.我想将字符串作为数组处理,将其切割成特定长度,并将其存储在二维数组中。 For example, I have 20 lines of text file.例如,我有 20 行文本文件。 like this "input.txt"像这样的“input.txt”

www.google.com
www.naver.com
kbphonemall.com
kbplant.com
k-bplus.com
kbpointreestore.com
kbprint.com
kbprism.com
kbprivatebanking.com
kbpstore.com
kbr9rtudaf5ppy.com
kbrafting.com
kbraille.com
kbrainbank.com
kbrainbow.com
kbrainc.com
kbrainglocal.com
kbrandexpo.com
kbrandingschool.com
kbrandmall.com

and Then, I read this file and tried to crop it on each line using "\n as the key. For example If you want to cut four lines at a time, you should cut it to "kbplant.com" first. And the truncated string looks like this.然后,我阅读了这个文件并尝试使用“\n”作为键在每一行上裁剪它。例如,如果你想一次裁剪四行,你应该先裁剪到“kbplant.com”。然后截断的字符串看起来像这样。

www.google.com\nwww.naver.com\nkbphonemall.com\nkbplant.com\n

and It will then be stored in a pointer array.然后它将存储在一个指针数组中。 like this像这样

char *cutting[n];
cutting[0] = "www.google.com\nwww.naver.com\nkbphonemall.com\nkbplant.com\n"
cutting[1] = "k-bplus.com\nkbpointreestore.com\nkbprint.com\nkbprism.com\n"
.... more

So far, that's the explanation of the functions I want to implement and I'll show you the code.到目前为止,这是对我要实现的功能的解释,我将向您展示代码。

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

#define IPATH "input.txt"

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

    char *ListBuffer;
    int ListSize;

    FILE *InputFile = fopen(IPATH, "r");

    fseek(InputFile, 0, SEEK_END);
    ListSize = ftell(InputFile);

    ListBuffer = malloc(ListSize);
    memset(ListBuffer, 0, ListSize);

    fseek(InputFile, 0, SEEK_SET);
    fread(ListBuffer, ListSize, 1, InputFile);

    int count = 0;
    ListBuffer[ListSize] = '\0'; //add NULL word

    for (int i = 0; i <= ListSize; i++) {
        if (ListBuffer[i] == '\n') {
            count++;
            if (count == 4) {
                printf("c%d\n", i);
                count = 0;
            }
        }
        if (ListBuffer[i] == 0) {
            printf("c%d\n", i);
            count = 0;
        }
    }

    fclose(InputFile);
    free(ListBuffer);
    ListBuffer = NULL;
}

this is my code I have used various functions such as strcpy function, strtok function, memcpy function, etc., but it was difficult to implement the desired function. Is there a better way or algorithm?这是我的代码 我使用了strcpy function、 strtok function、 memcpy function等各种功能,但是很难实现想要的function。有没有更好的方法或算法?

If you need more explanation, I'll answer it quickly.如果您需要更多解释,我会尽快答复。

I would appreciate it if you could reply.如果您能回复,我将不胜感激。 Have a good day.祝你有美好的一天。

It is unclear what you are trying to achieve in the main loop, but there are more problems:目前还不清楚你在主循环中试图实现什么,但还有更多问题:

  • you must allocate one extra byte to set the null terminator at ListSize :您必须额外分配一个字节以在 ListSize 处设置 null 终止ListSize

    ListBuffer = malloc(ListSize + 1); ListBuffer = malloc(ListSize + 1);

  • it is useless to set the array to 0 with memset : allocating with calloc(1, ListSize + 1) would be more efficient for this purpose, but since you read the contents into the array, clearing it first is useless.使用memset将数组设置为0是没有用的:使用calloc(1, ListSize + 1)进行分配对于此目的会更有效,但是由于您将内容读入数组,因此先清除它是没有用的。

  • fread might return a short count, for example in text mode on legacy systems, converting CR/LR sequences to newline bytes \n reduces the number of bytes read: fread可能会返回一个短计数,例如在遗留系统的文本模式下,将 CR/LR 序列转换为换行字节\n会减少读取的字节数:

     ListSize = fread(ListBuffer, 1, ListSize, InputFile); ListBuffer[ListSize] = '\0'; // set the null terminator

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM