拆分字符串并将其放入数组中 [英] splitting a string and put it into an array

查看:87
本文介绍了拆分字符串并将其放入数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我是新手,我必须尽快解决这个问题。但是

此时我没有取得多大成功。

任何人都可以帮助我(并理解我的英语:-))?


我有一个.txt文件,其中数据的结构是这样的:

Project-Nr。 ID name lastname

33 9 Lars Lundel

33 12 Emil Korla

34 19 Lara Keuler

33 13 Thorsten Lammert


这些数据必须逐行读出。

每一行都必须拆分(分隔符为TAB)并且必须保存在

一个二维数组。


背景是在下一步我必须在

中搜索一个ID数组之后将使用这个数组的部分,它具有

属于该数据的(行的)数据的内容。

数据是稍后在程序中使用。


我尝试了不同的方法,但没有成功。

这是我写的代码,现在只是为了打开文件和

显示数据:


FILE * importFile;

char row [100] = {0};

char test_array [100];

int anzahl = 0;

importFile = fopen(" datei.txt"," r") ;

if(impo rtFile == NULL)printf(" Datei geschlossen.\ n);

else printf(" Datei offen.\\\
);


while(fgets(row,sizeof row,importFile)!= NULL)

{

puts(row);

anzahl ++;

}

if(EOF)printf("%d \ n",anzahl);

fclose(importFile);

我希望,任何人都可以帮助我。

Thanx很多。


Kai Jaensch

解决方案

" Kai Jaensch" < KA ********* @ gmx.de>在留言中写道

news:bu ************ @ ID-189932.news.uni-berlin.de ...

你好,

我是新手,我必须尽快解决这个问题。但是
此时我没有太大的成功。
任何人都可以帮助我(并理解我的英语:-))?

我有一个.txt-以这种方式构建数据的文件:
Project-Nr。 ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

这些数据必须读出逐行。
每一行都必须拆分(分隔符为TAB)并且必须保存在一个二维数组中。

背景是在下一行步骤我必须在
该数组中搜索一个ID,之后将使用这部分数组,它具有属于该ID的数据(行的)内容。这些数据将在程序的后期使用。

我尝试了不同的方法,但没有成功。
这里是我写的代码,现在只是为了打开文件和
显示数据:

FILE * importFile;
char row [100] = {0};
char test_array [100];
int anzahl = 0;
importFile = fopen(" datei.txt"," r");
if(importFile == NULL)printf(" Datei geschlossen.\ n);
else printf(Datei offen.\" );

while(fgets(row,sizeof row,importFile)!= NULL)
{
puts(row);
anzahl ++;
}
如果(EOF)printf(%d \ n,anzahl);
fclose(importFile);

我希望,任何人都可以帮助我。
Thanx很多。

Kai Jaensch



你好,


既然你是在C ++中发帖,建议您使用:文件流

而不是C文件I / O.

也可以使用std :: map创建一个2D数组将项目关联

编号/ Id与用户名


-

Elias




" lallous" < LA ***** @ lgwm.org>在消息中写道

news:bu ************ @ ID-161723.news.uni-berlin.de ...

" ; Kai Jaensch < KA ********* @ gmx.de>在消息中写道
新闻:bu ************ @ ID-189932.news.uni-berlin.de ...

你好,
我是新手,我必须尽快解决这个问题。但是
此时我没有太大的成功。
任何人都可以帮助我(并理解我的英语:-))?

我有一个.txt-以这种方式构建数据的文件:
Project-Nr。 ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

这些数据必须读出逐行。
每一行都必须拆分(分隔符为TAB)并且必须保存在一个二维数组中。

背景是在下一行步骤我必须在
该数组中搜索一个ID,之后将使用这部分数组,它具有属于该ID的数据(行的)内容。这些数据将在程序的后期使用。

我尝试了不同的方法,但没有成功。
这里是我写的代码,现在只是为了打开文件和
显示数据:

FILE * importFile;
char row [100] = {0};
char test_array [100];
int anzahl = 0;
importFile = fopen(" datei.txt"," r");
if(importFile == NULL)printf(" Datei geschlossen.\ n);
else printf(Datei offen.\" );

while(fgets(row,sizeof row,importFile)!= NULL)
{
puts(row);
anzahl ++;
}
如果(EOF)printf(%d \ n,anzahl);
fclose(importFile);

我希望,任何人都可以帮助我。
Thanx很多。

Kai Jaensch


你好,

由于你是用C ++发布的,所以建议你您使用:文件流而不是C文件I / O.
还使用std :: map创建一个2D项目,将项目
数字/ Id与用户名相关联/>
-
Elias



他的代码的哪一部分是C ++?


< blockquote>

" Sean Kenwrick" < SK ******* @ hotmail.com>在消息中写道

news:bu ********** @ sparta.btinternet.com ...


" lallous" < LA ***** @ lgwm.org>在消息中写道
新闻:bu ************ @ ID-161723.news.uni-berlin.de ...

" Kai Jaensch" < KA ********* @ gmx.de>在消息中写道
新闻:bu ************ @ ID-189932.news.uni-berlin.de ...

你好,
我是新手,我必须尽快解决这个问题。
但是在这个时候我没有太大的成功。
任何人都可以帮助我(并理解我的英语:-))?

我有一个.txt-以这种方式构建数据的文件:
Project-Nr。 ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

这些数据必须读出逐行。
每一行都必须拆分(分隔符为TAB)并且必须保存在一个二维数组中。

背景是在下一行步骤我必须在该数组中搜索一个ID,然后使用这部分数组,
具有属于该ID的数据(行的)内容。这些数据将在程序的后期使用。

我尝试了不同的方法,但没有成功。
这里是我写的代码,现在只是为了打开文件和
显示数据:

FILE * importFile;
char row [100] = {0};
char test_array [100];
int anzahl = 0;
importFile = fopen(" datei.txt"," r");
if(importFile == NULL)printf(" Datei geschlossen.\ n);
else printf(Datei offen.\) ;

while(fgets(row,sizeof row,importFile)!= NULL)
{
puts(row);
anzahl ++;
}
if(EOF)printf("%d \ n",anzahl);
fclose(importFile);

我希望,任何人都可以帮助我。
很多。

Kai Jaensch


你好,

因为你是用C ++发布的,所以建议你使用:file
streams而不是C文件I / O.
还使用std :: map创建一个2D数组,它将项目
number / Id与用户名相关联>
-
Elias


他的代码的哪一部分是C ++?



对不起我现在看看你的回复是因为他发帖到comp.lang.c ++


Hello,

i am an newbie and i have to to solve this problem as fast as i can. But
at this time i don′t have a lot of success.
Can anybody help me (and understand my english :-))?

I have a .txt-file in which the data is structured in that way:
Project-Nr. ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

These data have to be read out row by row.
Every row has to be splitted (delimiter is TAB) and has to be saved in
an two-dimensional array.

The background is that in the next step i have to search for an ID in
that array and after that this part of an array is to be used, that has
the content of data (of the row) which belongs to the ID.
The data are used later in the program.

I tried different ways, but without success.
And here is the code i′ve written til now just to open the file and to
show the data:

FILE *importFile;
char row[100] = {0};
char test_array[100];
int anzahl=0;
importFile = fopen("datei.txt","r");
if(importFile == NULL) printf("Datei geschlossen.\n");
else printf("Datei offen.\n");

while(fgets(row, sizeof row, importFile) != NULL)
{
puts(row);
anzahl++;
}
if(EOF) printf("%d\n", anzahl);
fclose(importFile);
I hope, anybody can help me.
Thanx a lot.

Kai Jaensch

解决方案

"Kai Jaensch" <ka*********@gmx.de> wrote in message
news:bu************@ID-189932.news.uni-berlin.de...

Hello,

i am an newbie and i have to to solve this problem as fast as i can. But
at this time i don′t have a lot of success.
Can anybody help me (and understand my english :-))?

I have a .txt-file in which the data is structured in that way:
Project-Nr. ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

These data have to be read out row by row.
Every row has to be splitted (delimiter is TAB) and has to be saved in
an two-dimensional array.

The background is that in the next step i have to search for an ID in
that array and after that this part of an array is to be used, that has
the content of data (of the row) which belongs to the ID.
The data are used later in the program.

I tried different ways, but without success.
And here is the code i′ve written til now just to open the file and to
show the data:

FILE *importFile;
char row[100] = {0};
char test_array[100];
int anzahl=0;
importFile = fopen("datei.txt","r");
if(importFile == NULL) printf("Datei geschlossen.\n");
else printf("Datei offen.\n");

while(fgets(row, sizeof row, importFile) != NULL)
{
puts(row);
anzahl++;
}
if(EOF) printf("%d\n", anzahl);
fclose(importFile);
I hope, anybody can help me.
Thanx a lot.

Kai Jaensch


Hello,

Since you''re posting in C++, it is recommended that you use: file streams
instead of C file I/O.
Also use std::map to create a 2D array that will associate the project
number/Id with a user name

--
Elias



"lallous" <la*****@lgwm.org> wrote in message
news:bu************@ID-161723.news.uni-berlin.de...

"Kai Jaensch" <ka*********@gmx.de> wrote in message
news:bu************@ID-189932.news.uni-berlin.de...

Hello,

i am an newbie and i have to to solve this problem as fast as i can. But
at this time i don′t have a lot of success.
Can anybody help me (and understand my english :-))?

I have a .txt-file in which the data is structured in that way:
Project-Nr. ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

These data have to be read out row by row.
Every row has to be splitted (delimiter is TAB) and has to be saved in
an two-dimensional array.

The background is that in the next step i have to search for an ID in
that array and after that this part of an array is to be used, that has
the content of data (of the row) which belongs to the ID.
The data are used later in the program.

I tried different ways, but without success.
And here is the code i′ve written til now just to open the file and to
show the data:

FILE *importFile;
char row[100] = {0};
char test_array[100];
int anzahl=0;
importFile = fopen("datei.txt","r");
if(importFile == NULL) printf("Datei geschlossen.\n");
else printf("Datei offen.\n");

while(fgets(row, sizeof row, importFile) != NULL)
{
puts(row);
anzahl++;
}
if(EOF) printf("%d\n", anzahl);
fclose(importFile);
I hope, anybody can help me.
Thanx a lot.

Kai Jaensch


Hello,

Since you''re posting in C++, it is recommended that you use: file streams
instead of C file I/O.
Also use std::map to create a 2D array that will associate the project
number/Id with a user name

--
Elias


Which part of his code is C++?



"Sean Kenwrick" <sk*******@hotmail.com> wrote in message
news:bu**********@sparta.btinternet.com...


"lallous" <la*****@lgwm.org> wrote in message
news:bu************@ID-161723.news.uni-berlin.de...

"Kai Jaensch" <ka*********@gmx.de> wrote in message
news:bu************@ID-189932.news.uni-berlin.de...

Hello,

i am an newbie and i have to to solve this problem as fast as i can. But at this time i don′t have a lot of success.
Can anybody help me (and understand my english :-))?

I have a .txt-file in which the data is structured in that way:
Project-Nr. ID name lastname
33 9 Lars Lundel
33 12 Emil Korla
34 19 Lara Keuler
33 13 Thorsten Lammert

These data have to be read out row by row.
Every row has to be splitted (delimiter is TAB) and has to be saved in
an two-dimensional array.

The background is that in the next step i have to search for an ID in
that array and after that this part of an array is to be used, that has the content of data (of the row) which belongs to the ID.
The data are used later in the program.

I tried different ways, but without success.
And here is the code i′ve written til now just to open the file and to
show the data:

FILE *importFile;
char row[100] = {0};
char test_array[100];
int anzahl=0;
importFile = fopen("datei.txt","r");
if(importFile == NULL) printf("Datei geschlossen.\n");
else printf("Datei offen.\n");

while(fgets(row, sizeof row, importFile) != NULL)
{
puts(row);
anzahl++;
}
if(EOF) printf("%d\n", anzahl);
fclose(importFile);
I hope, anybody can help me.
Thanx a lot.

Kai Jaensch


Hello,

Since you''re posting in C++, it is recommended that you use: file streams instead of C file I/O.
Also use std::map to create a 2D array that will associate the project
number/Id with a user name

--
Elias


Which part of his code is C++?


Sorry I see now that your reply was because he cross posted to comp.lang.c++


这篇关于拆分字符串并将其放入数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆