简体   繁体   English

从文件中读取行并将每个单词存储到数组(C语言)中

[英]read line from file and store each word into an array (C language)

I'm trying to make a function in C that reads a first line form file and store each word into an array of strings, than return the array or print it (I used strtok()). 我试图在C中创建一个函数,该函数读取第一个行格式文件并将每个单词存储到字符串数组中,而不是返回该数组或将其打印出来(我使用strtok())。 I've written my code but when I test it I get an error: "segmentation error" which I don't know what it means. 我已经编写了代码,但是在测试时我得到一个错误:“分段错误”,我不知道它是什么意思。 any help??? 任何帮助??? I saw this question Segmentation fault with array of strings C i think it's similar but I still don't understand. 我看到了这个问题用字符串C的数组分割错误,我认为这是相似的,但我仍然不明白。 Here's my code : 这是我的代码:

the function that reads data from file and store it into an array function is on the file: methodes.c 从文件读取数据并将其存储到数组函数的函数在文件上:methodes.c

void lireFichier (char *file)
{
    int i = 0;
    int nbElement = 4;
    char ** tab;
    char line [1000];
    char *str[1000];
    const char s[2] = " ";
    char *token;
    FILE *myFile;

    myFile = fopen(file, "r");
    if(!myFile)
    {
        printf("could not open file");
    } else
    {
        printf("file opened\n");
        //while(fgets(line,sizeof line,myFile)!= NULL) 

        //get the fisrt line 
        fgets(line,sizeof line,myFile);

        //fprintf(stdout,"%s",line); 
        //get the fisrt word
        token = strtok(line, s);

        for(i =0; (i< nbElement) && (token != NULL); i++)
        {
            int len = strlen(token); 
            tab[i] = malloc(len);
            strncpy(tab[i], token, len-1);
            token = strtok(NULL, s);
            //printf( "%s\n", tab[i]);
        }   
    }
    fclose(myFile);
}

and here's the main.c // I pass the file as argument (in argv) 这是main.c //我将文件作为参数传递(在argv中)

#include <stdio.h>
#include <stdlib.h>
#include "methodes.h"


int main(int argc, char *argv[])
{
    int result = 1;
    if(argc < 2)
    {
        printf("Erreur dans les arguments\n");
    } else
    {
        int idx;
        for (idx = 0; idx < argc; idx++)
        {
            printf("parameter %d value is %s\n", idx, argv[idx]);
        }

       lireFichier(argv[1]);

    }
    return 0;
}

and here's an example of the file : methodes.txt 这是文件的示例:methodes.txt

afficher tableau
partager elements roles
nommer type profession
fin

and this is my output: 这是我的输出:

file opened
Erreur de segmentation

note: the output is in french, so the message means segmentation error thank you and sorry for all the details, i just wanted to make sure that people understand what i mean. 注意:输出为法语,因此该消息表示细分错误,非常感谢,并为所有详细信息感到抱歉,我只是想确保人们理解我的意思。

 char ** tab;

Is a uninitialised pointer to a pointer. 是指向指针的未初始化指针。 What you need is a array of pointers. 您需要的是一个指针数组。

char *tab[10];  

Instead of 10, use the size you see fit and adjust your code to include bounds checking. 而不是10,使用您认为合适的大小并调整代码以包括边界检查。

暂无
暂无

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

相关问题 如何从非结构化的.txt文件中读取单词并将每个单词存储在C中的char数组中? - How to read words from unstructured .txt file and store each word in a char array in C? C语言帮助:如何将.txt文件中的字符串存储到字符数组中? 从命令行参数btw读取此.txt文件。 - C language help:How to store strings from a .txt file into a character array? This .txt file is read from a command line argument btw. 从文件中读取每一行,然后在C中将该行拆分为一个字符串和一个数组 - Read each line from a file and split the line into a string and an array in C 如何从C中的文件中读取每一行到一个整数数组? - How to read each line into an integer array, from a file in C? 无论如何,在C语言中,它们是逐行读取文件内容并将每个整数值(预行)分别存储到数组中的方式吗? - In C is their anyway way to read the contents of a file, line-by-line, and store each integer value (preline) into a array separately? 从每一行,从c中的文件中逐字保存 - saving word by word ,from each line , from a file in c 如何使用 C 中的系统调用读取文件的每一行并将其存储在内存中 - How to read and store in memory each line of file using syscalls in C 逐行读取文件并将行存储在 C 中的字符串数组中 - Read file line by line and store lines in array of strings in C 如何使用分隔符分割我从文件中读取的行并将它们存储在 C 的每个 int 变量中 - How to split line I read from file with delimiters and store them in each int variable in C 如何从文本文件中读取数据并将其存储在 C 语言的变量中? - How to read data from a text file and store it in a variable in C language?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM