get(char *,num,delim)问题 [英] get(char*, num, delim) question

查看:72
本文介绍了get(char *,num,delim)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有一个正在读取25行数据文件的程序。这是

前两行的例子:


sp / spinnerbait / AAA Lures / Mad Phil / silver / bass / 1/1
f /浮动min鱼/ AAA Lures / Skinny Minney / green / bass / 0/0 0


在我的程序中,我需要读取每一行,但是,我需要根据第一个字段中的字符构建一个

不同的对象。所以,我需要获得第一个字符的
。而这里'我正在做某事

错误。这就是我到目前为止所获得的:


char readInFirstChars(ifstream& fin){

char * first;

fin.get(first,3,''/'');

cout<<第一; //调试行看看是什么输出

}

先退回;

}


上面的代码给我一个编译错误说明:

"从`char *''无效转换为`char''"


我尝试使用单个字符参数的get函数,如:

char first ;

fin.get(第一个);

cout<<第一个<< endl;

首先返回;


它运行正常。所以我必须对指针做错了。


我正在解决的另一个问题是如何通过输入文件正确循环

所以它只读取第一个字段然后

移动到下一行。我试过

while(fin.good()){

char first;

fin.get(first);

cout<<第一个<<结束;

}

先退回;


循环播放,但输出整个文件。我只需要第一个

字段。如果第一个字段只有一个字符,那么我要处理的唯一问题就是循环问题,但是因为它是

1或2个字符(最后的分隔符是''/''),我也是处理get()问题的



感谢任何建议。


Frank

解决方案



" Francis Bell" < pH值********** @ charter.net>在消息中写道

news:10 ************* @ corp.supernews.com ...

你好,

我有一个正在读取25行数据文件的程序。这是前两行的一个例子:

sp / spinnerbait / AAA Lures / Mad Phil / silver / bass / 1/1
f /浮动min鱼/ AAA诱饵/ Skinny Minney / green / bass / 0/0 0

在我的程序中,我需要读取每一行,但是,我需要根据字符构建一个不同的对象在第一个领域。所以,我需要获得第一个角色。而这里'我正在做的事情
错误。这就是我到目前为止所做的:

char readInFirstChars(ifstream& fin){
char * first;
fin.get(first,3,''' /'');
cout<<第一; //调试行看看是什么输出
}
首先返回;

上面的代码给我一个编译错误说明:
"从`char *''无效转换为`char''"

我尝试使用单个字符参数的get函数,如:
char first;
fin.get (第一个);
cout<<第一个<< endl;
首先返回;

它工作正常。所以我必须对指针做错了。




是的,实际上有两件事。当然你应该使用

指针。试试这个


char [3];

fin.getline(first,3,''/'');


重要的一课是了解


char *首先的区别;





char first [3];


第一个声明指向char的指针,它不会在

all中创建任何字符。只是声明一个指针并不会使它指向任何东西。所以在您的

代码中,您没有任何字符可以读取第一个字段。即使

你已经编译好了,当你试图读取

时它会崩溃。


第二个声明一个由三个字符组成的数组,这三个字符是你要读入字段的三个字符。不需要编译

指针,只需使用数组。


你的第二个错误是使用get而不是getline。


john


John Harrison写道:

" Francis Bell" < pH值********** @ charter.net>在消息中写道
新闻:10 ************* @ corp.supernews.com ...

你好,

我有一个正在读取25行数据文件的程序。这是前两行的一个例子:

sp / spinnerbait / AAA Lures / Mad Phil / silver / bass / 1/1
f /浮动min鱼/ AAA诱饵/ Skinny Minney / green / bass / 0/0 0

在我的程序中,我需要读取每一行,但是,我需要根据字符构建一个不同的对象在第一个领域。所以,我需要获得第一个角色。而这里'我正在做的事情
错误。这就是我到目前为止所做的:

char readInFirstChars(ifstream& fin){
char * first;
fin.get(first,3,''' /'');
cout<<第一; //调试行看看是什么输出
}
首先返回;

上面的代码给我一个编译错误说明:
"从`char *''无效转换为`char''"

我尝试使用单个字符参数的get函数,如:
char first;
fin.get (第一个);
cout<<第一个<< endl;
首先返回;

它工作正常。所以我必须对指针做错了。



是的,实际上是两件不同的事情。当然你应该使用
指针。首先尝试这个

char [3];
fin.getline(第一,第三,''/'');

重要的一课是要了解它们的区别

之前的第一个;


首先是char [3];

第一个声明一个指向char,它不会创建任何角色。只是声明一个指针并不会使它指向任何东西。因此,在您的代码中,您没有任何字符可以读取第一个字段。即使你已经将它编译好了,当你试图读取它时它会崩溃。

第二个声明一个由三个字符组成的数组,这三个是
你将要阅读你的字段的字符。不需要编译指针,只需使用数组。

你的第二个错误是使用get而不是getline。

john



嗨约翰,

首先,感谢您的快速回复!不幸的是,我已经被指示使用get()函数使用getline()函数。从

我对带有三个参数的get()函数的理解,它是

将字符读入缓冲区,直到读取了num-1个字符,

或在我的情况下,直到遇到''/''delim字符。这样,如果它读取第一个字段只有一个字符的行,那么它就是
,它会读取该字符然后遇到/分隔符然后停止。但是

它是我不能正确做的第一个参数。 ......但是,

你刚才宣布了一个指针并没有指向任何让我想到的东西的b $ b。我只是尝试了其他的东西

并且它适用于我的两个问题!这就是我做的:


while(fin.good())

{

fin.get(first,4 ,''/'');

cout<<第一个<< endl;

fin.ignore(80,''\ n'');

}

首先删除[];


这会在数据

文件的第一行文本和第二行的'''''屏幕上显示''sp'',并且直到EOF的所有其他行的第一个字段。


所以现在我正在处理程序中的下一个问题。但是如果我需要的话,我会在发布回来之前花几个小时来处理

。再次感谢John的支持,请看这个
。虽然我不能像你建议的那样使用getline,但是你的建议指出了我的问题。谢谢!


Frank




" Francis Bell" < pH值********** @ charter.net>在消息中写道

新闻:10 ************* @ corp.supernews.com ...

John Harrison写道:

" Francis Bell" < pH值********** @ charter.net>在消息中写道
新闻:10 ************* @ corp.supernews.com ...

你好,

我有一个正在读取25行数据文件的程序。这是前两行的一个例子:

sp / spinnerbait / AAA Lures / Mad Phil / silver / bass / 1/1
f /浮动min鱼/ AAA诱饵/ Skinny Minney / green / bass / 0/0 0

在我的程序中,我需要读取每一行,但是,我需要根据字符构建一个不同的对象在第一个领域。所以,我需要获得第一个角色。而这里'我正在做的事情
错误。这就是我到目前为止所做的:

char readInFirstChars(ifstream& fin){
char * first;
fin.get(first,3,''' /'');
cout<<第一; //调试行看看是什么输出
}
首先返回;

上面的代码给我一个编译错误说明:
"从`char *''无效转换为`char''"

我尝试使用单个字符参数的get函数,如:
char first;
fin.get (第一个);
cout<<第一个<< endl;
首先返回;

它工作正常。所以我必须对指针做错事。

是的,实际上有两件事。当然你应该使用
指针。首先尝试这个

char [3];
fin.getline(第一,第三,''/'');

重要的一课是要了解它们的区别

之前的第一个;


首先是char [3];

第一个声明一个指向char,它根本不会创建任何字符
。只是声明一个指针并不会使它指向任何东西。所以在
你的代码中你没有任何字符来读取第一个字段。即使是
,如果你有它编译它会在你试图读取时崩溃。

第二个声明一个由三个字符组成的数组,这三个是
你将要阅读你的字段的字符。不需要
编译指针,只需使用数组。

你的第二个错误是使用get而不是getline。

john
约翰,
首先,感谢您的快速回复!不幸的是,我已被指示使用get()函数使用getline()函数。




任何特殊原因?


我对使用三个参数的get()函数的理解,它将字符读入缓冲区,直到读取了数字 - 1个字符,或者在我的情况下,直到遇到''/''delim字符。


是的,我忘记了这一点,这就是为什么我推荐getline,但是得到

也可以。 get和getline之间的区别在于,getline读取了

分隔符,而得到的却没有。

这样,
如果它读取的行是第一个字段只有一个字符,它会读取该字符,然后遇到''/''分隔符并停止。但这是第一个我没做错的参数。 ......但是,
你刚刚宣布一个指针并且没有指向任何东西时所说的关于我的事情让我想到了什么。我只是尝试了其他的东西
并且它适用于我的两个问题!这就是我所做的:


我猜你错过了


char * first = new char [4];


或类似的东西。

while(fin.good())
{
fin.get(first,4,''/'') ;
cout<<第一个<< endl;
fin.ignore(80,''\ n'');
}
先删除[];




仍然最好使用数组而不是指针。它更有效,更安全,更简单,有什么不喜欢的?


你也循环错了。即使你在文件的末尾处于
,文件仍然可能是好的,所以你最终绕循环一次太多次了。

你的循环应该看起来像这个


while(fin.get(first,4,''/''))

{

cout< <第一个<< endl;

fin.ignore(80,''\ n'');

}


john


Hello,

I''ve got a program that is reading in a data file of 25 lines. Here is
an example of the first two lines:

sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0

In my program, I need to read in each line, BUT, I need to build a
different object based upon the characters in the first field. So, I
need to GET the first characters. And here''s where I''m doing something
incorrectly. This is what I''ve got so far:

char readInFirstChars(ifstream &fin) {
char *first;
fin.get(first, 3, ''/'');
cout << first; // debugging line to see what is output
}
return first;
}

The above code give me a compile error stating:
" invalid conversion from `char*'' to `char''"

I tried the get function with the single character parameter like such:
char first;
fin.get(first);
cout << first << endl;
return first;

and it works fine. So I must be doing something wrong with the pointer.

The other tangent problem I''m having with this how to properly cycle
through the input file so it only reads in the first field and then
moves on to the next line. I tried
while (fin.good()) {
char first;
fin.get(first);
cout << first << endl;
}
return first;

Which looped, but it output the entire file. I only need the first
field. If the first field were only one character, then the only
problem I would be dealing with is the loop problem, but since it''s
either 1 or 2 characters (with the end delimeter being ''/''), I''m also
dealing with the get() problem.

Thanks for any advice.

Frank

解决方案


"Francis Bell" <ph**********@charter.net> wrote in message
news:10*************@corp.supernews.com...

Hello,

I''ve got a program that is reading in a data file of 25 lines. Here is
an example of the first two lines:

sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0

In my program, I need to read in each line, BUT, I need to build a
different object based upon the characters in the first field. So, I
need to GET the first characters. And here''s where I''m doing something
incorrectly. This is what I''ve got so far:

char readInFirstChars(ifstream &fin) {
char *first;
fin.get(first, 3, ''/'');
cout << first; // debugging line to see what is output
}
return first;
}

The above code give me a compile error stating:
" invalid conversion from `char*'' to `char''"

I tried the get function with the single character parameter like such:
char first;
fin.get(first);
cout << first << endl;
return first;

and it works fine. So I must be doing something wrong with the pointer.



Yes, two different things actually. And of course you should be using a
pointer at all. Try this

char first[3];
fin.getline(first, 3, ''/'');

Important lesson is to understand the difference between

char *first;

and

char first[3];

The first declares a pointer to char, it does not create any characters at
all. Just declaring a pointer does not make it point at anything. So in your
code you do not have any characters to read the first field into. Even if
you had got it to compile it would have crashed when you tried to do the
read.

The second declares an array of three characters, these are the three
characters you are going to read your field into. No need for compilcated
pointers, just use an array.

Your second mistake was to use get instead of getline.

john


John Harrison wrote:

"Francis Bell" <ph**********@charter.net> wrote in message
news:10*************@corp.supernews.com...

Hello,

I''ve got a program that is reading in a data file of 25 lines. Here is
an example of the first two lines:

sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0

In my program, I need to read in each line, BUT, I need to build a
different object based upon the characters in the first field. So, I
need to GET the first characters. And here''s where I''m doing something
incorrectly. This is what I''ve got so far:

char readInFirstChars(ifstream &fin) {
char *first;
fin.get(first, 3, ''/'');
cout << first; // debugging line to see what is output
}
return first;
}

The above code give me a compile error stating:
" invalid conversion from `char*'' to `char''"

I tried the get function with the single character parameter like such:
char first;
fin.get(first);
cout << first << endl;
return first;

and it works fine. So I must be doing something wrong with the pointer.


Yes, two different things actually. And of course you should be using a
pointer at all. Try this

char first[3];
fin.getline(first, 3, ''/'');

Important lesson is to understand the difference between

char *first;

and

char first[3];

The first declares a pointer to char, it does not create any characters at
all. Just declaring a pointer does not make it point at anything. So in your
code you do not have any characters to read the first field into. Even if
you had got it to compile it would have crashed when you tried to do the
read.

The second declares an array of three characters, these are the three
characters you are going to read your field into. No need for compilcated
pointers, just use an array.

Your second mistake was to use get instead of getline.

john


Hi John,
First, thanks for the quick reply! Unfortunately, I have been
instructed to use the get() function vice the getline() function. From
what I understand about the get() function with the three parameters, it
reads characters into a buffer until num - 1 characters have been read,
or in my case, until the ''/'' delim character is encountered. This way,
if it reads a line where the first field is only one character, it reads
that character and then encounters the ''/'' delimeter and stops. But
it''s the first parameter that I''m not doing correctly. ... However,
what you said about me having just declared a pointer and not pointing
to anything made me think of something. And I just tried something else
and it worked for both of my problems! This is what I did:

while (fin.good())
{
fin.get(first, 4, ''/'');
cout << first << endl;
fin.ignore(80, ''\n'');
}
delete [] first;

This displays ''sp'' to the screen for that first line of text in the data
file and ''f'' for the second line, and the first field for all of the
other lines until EOF.

So now I''m on to the next problem in my program. But I''ll work on that
for a few hours before posting back if I need to. Thanks again John for
looking at this. Although I couldn''t use the getline as you suggested,
your suggestion pointed me to my problem. Thanks!

Frank



"Francis Bell" <ph**********@charter.net> wrote in message
news:10*************@corp.supernews.com...

John Harrison wrote:

"Francis Bell" <ph**********@charter.net> wrote in message
news:10*************@corp.supernews.com...

Hello,

I''ve got a program that is reading in a data file of 25 lines. Here is
an example of the first two lines:

sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1
f/floating minnow/AAA Lures/Skinny Minney/green/bass/0/0/0

In my program, I need to read in each line, BUT, I need to build a
different object based upon the characters in the first field. So, I
need to GET the first characters. And here''s where I''m doing something
incorrectly. This is what I''ve got so far:

char readInFirstChars(ifstream &fin) {
char *first;
fin.get(first, 3, ''/'');
cout << first; // debugging line to see what is output
}
return first;
}

The above code give me a compile error stating:
" invalid conversion from `char*'' to `char''"

I tried the get function with the single character parameter like such:
char first;
fin.get(first);
cout << first << endl;
return first;

and it works fine. So I must be doing something wrong with the pointer.

Yes, two different things actually. And of course you should be using a
pointer at all. Try this

char first[3];
fin.getline(first, 3, ''/'');

Important lesson is to understand the difference between

char *first;

and

char first[3];

The first declares a pointer to char, it does not create any characters at all. Just declaring a pointer does not make it point at anything. So in your code you do not have any characters to read the first field into. Even if you had got it to compile it would have crashed when you tried to do the
read.

The second declares an array of three characters, these are the three
characters you are going to read your field into. No need for compilcated pointers, just use an array.

Your second mistake was to use get instead of getline.

john


Hi John,
First, thanks for the quick reply! Unfortunately, I have been
instructed to use the get() function vice the getline() function.



Any particular reason?
From
what I understand about the get() function with the three parameters, it
reads characters into a buffer until num - 1 characters have been read,
or in my case, until the ''/'' delim character is encountered.
Yes, I''d forgotten about that, which is why I recommended getline,.but get
works too. The difference between get and getline, is that getline reads the
delimiter whereas get doesn''t.
This way,
if it reads a line where the first field is only one character, it reads
that character and then encounters the ''/'' delimeter and stops. But
it''s the first parameter that I''m not doing correctly. ... However,
what you said about me having just declared a pointer and not pointing
to anything made me think of something. And I just tried something else
and it worked for both of my problems! This is what I did:

I guess you missed out

char* first = new char[4];

or something similar.
while (fin.good())
{
fin.get(first, 4, ''/'');
cout << first << endl;
fin.ignore(80, ''\n'');
}
delete [] first;



Its still preferable to use an array instead of a pointer. Its more
efficient, safer and simpler, what''s not to like?

Also you loop is wrong. The file might still be good even when you are at
the end of the file so you end up going round the loop one too many times.
Your loop should look like this

while (fin.get(first, 4, ''/''))
{
cout << first << endl;
fin.ignore(80, ''\n'');
}

john


这篇关于get(char *,num,delim)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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