打破ifstream输入单词? [英] Break ifstream input into words?

查看:64
本文介绍了打破ifstream输入单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,


我这里是C ++新手 - 我搞砸了VB,但我主要坚持使用网页

语言,所以我觉得C ++有时候很混乱。基本上,我是试图导入文本文件的b $ b,但我想逐字逐句地进行。我对如何做到这一点很困惑。通常情况下,我认为尝试将单词输入字符串会有意义,但是对于这个应用程序来说,我需要使用字符数组和指针。那么

的最佳方式是什么呢?我知道我需要做什么 - 逐个字符地转移和/或
转储到一个数组中,直到我们得到一个空格或其他形式的

标点符号,但我'无法将其转化为代码。如果你能分享一些关于如何解决这个问题的任何想法,那么这将是非常值得赞赏的!我假设它会像while循环一样

导入字符,而!=空格,逗号,句号等等,然后

停止那个。但是再次 - 不知道如何通过

字符来做这件事 - 我习惯了字符串。


非常感谢,非常感谢!


Dan

Hey guys,

I''m a C++ newbie here - I''ve messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what''s the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I''m having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I''m assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I''m used to strings.

Thanks a lot, much appreciated!

Dan

推荐答案

dm ******* @ gmail.com 写道:
dm*******@gmail.com wrote:

尝试导入文本文件,但我想要一字一句地做。我对如何做到这一点很困惑。通常情况下,我认为尝试将单词输入字符串会有意义,但是对于这个应用程序来说,我需要使用字符数组和指针。那么

的最佳方式是什么呢?
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what''s the best way to
go about this?



将单词输入字符串,然后将其复制到字符数组,或者为指针分配空间并复制到它。或者只是将字符串的c_str()

传递给采用常量c风格字符串的函数,如果

合适。


-

Salu2

Input the words into strings, and then copy it to the character arrays, or
alllocate space for the pointers and copy to it. Or just pass the c_str ()
of the strings to the functions that takes constant c-style strings, if
appropriate.

--
Salu2




dmurra ... @ gmail.com写道:

dmurra...@gmail.com wrote:

嘿伙计们,


我这里是C ++新手 - 我搞砸了VB,但我主要坚持网络

语言,所以我觉得C ++有时候很混乱。基本上,我是试图导入文本文件的b $ b,但我想逐字逐句地进行。我对如何做到这一点很困惑。通常情况下,我认为尝试将单词输入字符串会有意义,但是对于这个应用程序来说,我需要使用字符数组和指针。那么

的最佳方式是什么呢?我知道我需要做什么 - 逐个字符地转移和/或
转储到一个数组中,直到我们得到一个空格或其他形式的

标点符号,但我'无法将其转化为代码。如果你能分享一些关于如何解决这个问题的任何想法,那么这将是非常值得赞赏的!我假设它会像while循环一样

导入字符,而!=空格,逗号,句号等等,然后

停止那个。但是再次 - 不知道如何通过

字符来做这件事 - 我习惯了字符串。


非常感谢,非常感谢!


Dan
Hey guys,

I''m a C++ newbie here - I''ve messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what''s the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I''m having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I''m assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I''m used to strings.

Thanks a lot, much appreciated!

Dan



这是一个建议。在尝试这样的事情之前,熟悉

与std :: string和容器如std :: vectors。它相当难以处理char数组和指针更容易出错。

我会完全避免使用指针并将新的/删除分配留给

古代历史。

一本好书会有所帮助,请参考这个新闻组获取一些推荐的

标题。


#include < iostream>

#include< string>

#include< vector>


int main()

{

std :: string s(短字符串);


std :: vector< std :: string vs; //一个字符串向量

vs.push_back(s);

vs.push_back("另一个字符串");

vs. push_back(最后一个字符串);


for(size_t i = 0; i< vssize(); ++ i)

{

std :: cout<< vs [i]<< " \ n";

}

返回0;

}

Here is a suggestion. Before trying something like this, get familiar
with std::string and containers like std::vectors. Its considerably
more difficult and error prone to deal with char arrays and pointers.
I''ld avoid pointers altogether and leave new/delete allocations to
ancient history.
A good book would help, consult this newsgroup for some recommended
titles.

#include <iostream>
#include <string>
#include <vector>

int main()
{
std::string s("a short string");

std::vector< std::string vs; // a vector of strings
vs.push_back(s);
vs.push_back("another string");
vs.push_back("the last string");

for(size_t i = 0; i < vs.size(); ++i)
{
std::cout << vs[i] << "\n";
}
return 0;
}


谢谢,我真的很感激。我很乐意使用字符串,而且我知道每个人都知道这是更好的方法,但是我使用的

指南包括使用字符数组和

分配指针。基本上,我创建的程序需要

从输入文件中收集一个单词列表,然后分析它们出现时如何和

。目前的计划是将这些单词放入一个链接的

列表中,按字母顺序排列,然后从那里开始。但是,我被困在

分开的话。一旦我可以将它们变成char数组,我就可以了。
应该没问题。这只是将文件分解为单词

我遇到了很多问题。我猜测如果我使用

字符数组,最好的方法是逐个字符地查看文件

,寻找空格和标点符号并将
单词放入链接列表中的节点。但是,我不知道如何这样做 - 我不熟悉如何查看

文件中的每个字符,然后如何用数据创建一个字符数组(word)。


再一次,感谢您的帮助,希望这会让它多一点

清除我想要的是什么!


Dan

Salt_Peter写道:
Thanks, I really appreciate it. I would love to use strings, and I
understand everyone knows it''s the better way to go, however the
guidelines I was given to work with included using character arrays and
assigning pointers to them. Basically, the program I create needs to
gather a list of words from an input file and do an analysis of how and
when they appear. The current plan is to put these words into a linked
list, alphabetize them, and go from there. However, I''m stuck at
getting the words seperated. Once I can get them into char arrays, I
should be fine. It''s just the breaking down of the file into words that
I''m having huge problems with. I''m guessing that if I go with the
character array, the best way to do this will be to go through the file
character by character, looking for spaces and punctuation and putting
words into their nodes in a linked list. However, I don''t know how to
do this - I''m not familiar with how to look at each character in a
file, and then how to create a character array ("word") with the data.

Again, thanks for all your help, hopefully this makes it a little more
clear what I''m going for!

Dan
Salt_Peter wrote:

dmurra。 .. @ gmail.com写道:
dmurra...@gmail.com wrote:

嘿伙计们,


我是C ++新手 - 我'与VB混淆了,但我主要坚持使用web $

语言,所以我觉得C ++有时会让人很困惑。基本上,我是试图导入文本文件的b $ b,但我想逐字逐句地进行。我对如何做到这一点很困惑。通常情况下,我认为尝试将单词输入字符串会有意义,但是对于这个应用程序来说,我需要使用字符数组和指针。那么

的最佳方式是什么呢?我知道我需要做什么 - 逐个字符地转移和/或
转储到一个数组中,直到我们得到一个空格或其他形式的

标点符号,但我'无法将其转化为代码。如果你能分享一些关于如何解决这个问题的任何想法,那么这将是非常值得赞赏的!我假设它会像while循环一样

导入字符,而!=空格,逗号,句号等等,然后

停止那个。但是再次 - 不知道如何通过

字符来做这件事 - 我习惯了字符串。


非常感谢,非常感谢!


Dan
Hey guys,

I''m a C++ newbie here - I''ve messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what''s the best way to
go about this? I know what I need to do - go character by character and
dump into an array until we get to either a space or some other form of
punctuation, but I''m having trouble getting this into code. If any of
you could share some ideas on how to go about this, it would be much
appreciated! I''m assuming its going to be something like a while loop
that imports characters while != a space, comma, period, etc, then
stops when it gets to that. But again - not sure how to do this by
character - I''m used to strings.

Thanks a lot, much appreciated!

Dan



这是一个建议。在尝试这样的事情之前,熟悉

与std :: string和容器如std :: vectors。它相当难以处理char数组和指针更容易出错。

我会完全避免使用指针并将新的/删除分配留给

古代历史。

一本好书会有所帮助,请参考这个新闻组获取一些推荐的

标题。


#include < iostream>

#include< string>

#include< vector>


int main()

{

std :: string s(短字符串);


std :: vector< std :: string vs; //一个字符串向量

vs.push_back(s);

vs.push_back("另一个字符串");

vs. push_back(最后一个字符串);


for(size_t i = 0; i< vssize(); ++ i)

{

std :: cout<< vs [i]<< " \ n";

}

返回0;

}


Here is a suggestion. Before trying something like this, get familiar
with std::string and containers like std::vectors. Its considerably
more difficult and error prone to deal with char arrays and pointers.
I''ld avoid pointers altogether and leave new/delete allocations to
ancient history.
A good book would help, consult this newsgroup for some recommended
titles.

#include <iostream>
#include <string>
#include <vector>

int main()
{
std::string s("a short string");

std::vector< std::string vs; // a vector of strings
vs.push_back(s);
vs.push_back("another string");
vs.push_back("the last string");

for(size_t i = 0; i < vs.size(); ++i)
{
std::cout << vs[i] << "\n";
}
return 0;
}


这篇关于打破ifstream输入单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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