C将文件内容读入字符串数组

我需要将文件的内容加载到两个字符串数组中。我尝试了以下操作,但它不起作用。 file.txt包含10条记录,每个记录都有两个用空格分隔的字符串值。

代码:

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

int main(void) {
    char line[12][20];
    FILE *fptr = NULL; 
    int i = 0;
    int tot = 0;

    fptr = fopen("file.txt","r");
    char arr[20][20];

    while (fgets(line,sizeof(line),fptr)) {
        strcpy(arr[i],line);
        i++;
    }
    tot=i;
    for (int i=0; i<tot; i++) {
        printf("first value %s",arr[i][0]);
        printf("second value is %s",arr[i][1]);
        printf("\n");
    }
    return 0;
}
wanglong52044 回答:C将文件内容读入字符串数组

如果我理解正确,则您正在尝试将数据存储在以下结构中:

{{"line1A","line1B"},{"line2A","line2B"},{"line3A","line3B"}}

看起来您需要一个数组,其中每个元素由两个数组(字符串)组成,每个行中的第一个值一个,第二个值一个。如果是这种情况,则需要一个三维字符数组。

在下面的示例中,我将arrayOfLines声明为具有12个元素的数组,每个元素具有2个字符数组(每行两个值),每个字符串中都有20个字符的空间(NULL终止的字符)数组)

您的代码还有其他一些问题:

  • fgets()的第一个参数应该是char *-指向字符串缓冲区的指针。您的代码以多维字符数组传递。
  • 您的while循环应继续直到fgets返回NULL
  • 您需要将每一行分成多个字符串
  • 使用strcpy()复制字符串时检查缓冲区溢出

在示例代码中,我使用了strtok(),以" "空格字符定界-您可能需要解决这个问题-strtok可以接受字符数组作为定界符。在该示例中,我使用第一个空格字符拆分了第一个字符串,第二个字符串由行尾定界。

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


int main(void) 
{
    // Array for 12 lines,each with 2 strings,each string max 20 chars
    // Adjust values as required.
    char arrayOfLines[12][2][20];

    FILE *fptr = NULL; 
    int i = 0;
    int tot = 0;
    fptr = fopen("file.txt","r");
    // char arr[20][20]; not needed

    char line[20];
    while(fgets(line,sizeof(line) / sizeof(line[0]),fptr) != NULL)
    {
        // Rudimentary error checking - if the string has no newline
        // there wasn't enough space in line
        if (strchr(line,'\n') == NULL) {
            printf("Line too long...");
            return EXIT_FAILURE;
        }
        // Split string into tokens
        // NB: Check for buffer overruns when copying strings
        char *ptr1 = strtok(line," ");
        strcpy(arrayOfLines[i][0],ptr1);
        char *ptr2 = strtok(NULL,"\n");
        strcpy(arrayOfLines[i][1],ptr2);
        i++;
    }

    tot=i; // Unecessary - just use a different variable in your loop and use i as the upper bound

    for (int i=0;i<tot;i++)
    {
        printf("first value %s\n",arrayOfLines[i][0]);
        printf("second value is %s\n",arrayOfLines[i][1]);
        printf("\n");
    }

    return 0;
}
,
printf("first value %s",arr[i][0]);
printf("second value is %s",arr[i][1]);

基本上,您要做的就是从i字中打印2个字符,当您要打印完整字符串时,您应该这样做:printf("%s",arr[i]);您说过,值由空格分隔,所以当您从文件中获取行时,如果要将其拆分为2个{{1},则将其保存到arr[i](如果文件的第一行包含“ Hello World”,则arr [0]将包含“ Hello World”) }您需要逐个字符地打印它们,直到空格为止。

编辑:我提醒自己有关函数printf的信息,您可以像使用键盘输入一样使用它来从sscanf获取数据

,

您可以使用它来完成

代码

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

int main(void){

    char line[12][20];
    char arr[20][20];
    FILE *fptr=NULL; 
    int i=0;

    fptr = fopen("file.txt","r");
    if(!fptr){
        printf("cant open file\n");
        exit(1);
    }

    while(fgets(*line,sizeof(line),fptr)){
        strncpy(arr[i],*line,sizeof(*line));
        i++;
    }

    for (int j=0;j<i;j++){
        printf("%s\n",arr[j]);
    }

    return 0;
}

我对您的代码进行的注释和更改:

  • 如果fptr为NULL,请检查open()作为返回值。
  • 删除不必要的tot变量,并在最后一个for循环中使用另一个索引j
  • 使用strncpy()作为strcpy()的更好版本
  • 正确的打印方式printf("%s\n",arr[j]);
  • \n可以嵌入第一个printf()
本文链接:https://www.f2er.com/3157478.html

大家都在问