从文件中读取行并将每个单词存储到数组中(C语言)
read line from file and store each word into an array (C language)
我正在尝试在 C 中创建一个函数,该函数读取第一行表单文件并将每个单词存储到一个字符串数组中,而不是 return 数组或打印它(我使用了 strtok()) .我已经编写了我的代码,但是当我测试它时出现错误:"segmentation error" 我不知道它是什么意思。
有帮助吗???我看到了这个问题 Segmentation fault with array of strings 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);
}
这里是 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;
}
这是一个文件示例:methodes.txt
afficher tableau
partager elements roles
nommer type profession
fin
这是我的输出:
file opened
Erreur de segmentation
注意:输出的是法语,所以消息表示分段错误
谢谢你,对不起所有的细节,我只是想确保人们明白我的意思。
char ** tab;
是指向指针的未初始化指针。你需要的是一个指针数组。
char *tab[10];
而不是 10,使用您认为合适的大小并调整您的代码以包括边界检查。
http://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htm
数组指针:
int *ptr[MAX];
我正在尝试在 C 中创建一个函数,该函数读取第一行表单文件并将每个单词存储到一个字符串数组中,而不是 return 数组或打印它(我使用了 strtok()) .我已经编写了我的代码,但是当我测试它时出现错误:"segmentation error" 我不知道它是什么意思。 有帮助吗???我看到了这个问题 Segmentation fault with array of strings 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);
}
这里是 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;
}
这是一个文件示例:methodes.txt
afficher tableau
partager elements roles
nommer type profession
fin
这是我的输出:
file opened
Erreur de segmentation
注意:输出的是法语,所以消息表示分段错误 谢谢你,对不起所有的细节,我只是想确保人们明白我的意思。
char ** tab;
是指向指针的未初始化指针。你需要的是一个指针数组。
char *tab[10];
而不是 10,使用您认为合适的大小并调整您的代码以包括边界检查。
http://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htm
数组指针:
int *ptr[MAX];