c语言从文件中读取坐标到数组,从文件中读取行并将每个单词存储到一个数组中(C语言)(read line from file and store each word into an array (C l...

从文件中读取行并将每个单词存储到一个数组中(C语言)(read line from file and store each word into an array (C language))

我正在尝试在C中创建一个函数来读取第一行表单文件并将每个单词存储到一个字符串数组中,而不是返回数组或打印它(我使用了strtok())。 我已经编写了我的代码,但是当我测试它时,我得到一个错误:“分段错误”,我不知道这意味着什么。 任何帮助??? 我看到这个问题Segmentation fault与字符串数组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

#include

#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

注意:输出是法语,所以消息意味着分段错误,谢谢你,对不起所有的细节,我只是想确保人们理解我的意思。

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()). 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. Here's my code :

the function that reads data from file and store it into an array function is on the file: 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)

#include

#include

#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

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.

原文:https://stackoverflow.com/questions/33324115

更新时间:2020-02-08 14:24

最满意答案

char ** tab;

是指向指针的未初始化指针。 你需要的是一系列指针。

char *tab[10];

而不是10,使用您认为合适的大小并调整代码以包括边界检查。

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.

2015-10-24

相关问答

给它一个。 如果您阅读每个函数(fopen(),scanf(),fclose())的手册页以及如何分配C语言中的数组,您会更好。您还应该为此添加错误检查。 例如,如果Integers.txt不存在或您没有从中读取的权限会发生什么? 如果文本文件包含超过100个数字,怎么办? FILE *file = fopen("Integers.txt", "r");

int integers[100];

int i=0;

int num;

while(fscanf(f

...

如果我猜你正在寻找像这样的代码。 try {

ArrayList list = new ArrayList();

File dir = new File("path of folder that contains my files")

for (File f : dir.listFiles()) {

BufferedReader br = new BufferedReader(new InputStreamReader(ne

...

你有两个错误。 一个是for循环。 for (int i = 0; i <= listOfFiles.length; i++)

^

在迭代数组时,通常会从0到length - 1迭代length - 1而不是0到length 。 然后第二个是你没有考虑路径,只有文件名。 Path pathToFile = Paths.get(listOfFiles[i].getName());

...

您可以使用File.ReadAllLines方法将文件加载到数组中。 然后,您可以使用for循环遍历这些行,并使用字符串类型的Split方法将每行分隔到另一个数组中,并将这些值存储在格式化数组中。 就像是: static void Main(string[] args)

{

var lines = File.ReadAllLines("filename.txt");

for (int i = 0; i < lines.Length; i++)

...

大约有两种方法可以在数据序列中放置分隔符。 不要将分隔符输出到第一个元素。 以下元素在输出元素之前输出分隔符。 //Pseudocode

for(int rows_index = 0; rows_index < rows_size; ++rows_index){

for(int columns_index = 0; columns_index < columns_size; ++columns_index){

if(columns_index != 0)//not firs

...

你没有用fgetc检索一行。 您正在从文件中一次检索一个字符。 该示例保持检索字符,直到EOF字符被加密(文件结束)。 看看fgetc的这个描述。 http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/ 在while循环的每次迭代中,fgetc将检索单个字符并将其放入变量“n”。 可以帮助你在C中使用“字符”的东西就是把它想象成一个字节,而不是实际的字符。 你在这里不理解的是int是4个字节,字符是1个字节,但两个都可以为相同的ASCII

...

首先,使用ifstream代替fstream和ios_base::in 。 接下来,使用std::getline()将一行作为字符串,为它创建一个向量(可能在vector> ),然后解析它(可能使用std::istringstream )。 First, use ifstream instead of fstream with ios_base::in. Next, use std::getline() to get one line as a string, cre

...

char ** tab;

是指向指针的未初始化指针。 你需要的是一系列指针。 char *tab[10];

而不是10,使用您认为合适的大小并调整代码以包括边界检查。 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

...

我认为您的list已包含一些数据,您应该在向其添加新文件数据之前将其清除。 OpenFileDialog txtopen = new OpenFileDialog();

if (txtopen.ShowDialog() == DialogResult.OK)

{

list.Clear(); //

string FileName = txtopen.FileName;

string line;

System.IO.StreamRea

...

您将变量声明两次,一次在参数列表中,一次在“本地声明”中。 功能支架未关闭。 一个fscanf只能读取由其格式字符串指定的多个项目,在本例中为3( "%d%d%d" )。 它读取数字,而不是数组。 要填充数组,您需要一个循环( while或for )。 编辑 好的,这是一种方法: #define MAX 50

#include

int readresults(FILE *results, int *studID, int *score1, int *score2) {

i

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值