| |

VerySource

 Forgot password?
 Register
Search
View: 716|Reply: 6

Help: file problems

[Copy link]

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-27 20:20:01
| Show all posts |Read mode
There are many words in the file, read them out and store them in the linked list, brothers and sisters, what should I do, give a specific answer that can be run without errors, thank you brother! !! !!
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-2-22 19:45:01
| Show all posts
getline () reads a line into str, separating each word
Then list :: push_back ()
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-29 20:30:02
| Show all posts
Can you elaborate on it, a younger rookie.
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-2 18:15:01
| Show all posts
#include <iostream>
#include <fstream>
#include <list>
#include <string>
using namespace std;
bool is_word (word) {
// Judge the word or not is up to you, if it is rough
return true;
};
int main () {
cout << "Please enter a file name:";
string fname;
cin >> fname;
ifstream in (fname);
string word;
list <string> ls;
while (in >> word) {
if (is_word (word)) {
  ls.push_back (word);
} // endif
} // endwhile
cout << "Word list:\n";
copy (ls.begin (), ls.end (), ostream_iterator <string> (cout, "\n"));
}
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-2 20:15:01
| Show all posts
Missing two header files
#include <algorithm>
#include <iterator>
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-3-4 18:30:01
| Show all posts
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <fstream>

using namespace std;

void separate_word (const string&str, vector <string>&svec)
{
string :: size_type pos = 0;
string :: size_type prev_pos = 0;

string word;
int lenOfWord;

while ((pos = str.find_first_of ('', pos))! = string :: npos)
{
lenOfWord = pos-prev_pos;
word = str.substr (prev_pos, lenOfWord);
svec.push_back (word);
prev_pos = ++ pos;
}

// the last word
lenOfWord = str.size ()-prev_pos;
word = str.substr (prev_pos, lenOfWord);
svec.push_back (word);

return;
}

void getstr (string file_read, vector <string>&svec_line)
{
ifstream infile (file_read.c_str ());
if (! infile)
{
cerr << "open file error" << endl;
exit (-1);
}
else
{
string textline;
while (getline (infile, textline, '\n'))
{
svec_line.push_back (textline);
}
}
}

void initList (list <string>&slist, vector <string>&svec)
{
vector <string> :: const_iterator cit = svec.begin ();
while (cit! = svec.end ())
{
slist.push_back (* cit);
}
}

int main ()
{
cout << "Please enter the file name:";
string file_name;
cin >> file_name;

vector <string> svec_line;
getstr (file_name, svec_line);

vector <string> :: const_iterator cit = svec_line.begin ();
vector <string> svec;
for (; cit! = svec_line.end (); ++ cit)
{
separate_word (* cit, svec);
}
Ranch
list <string> slist;
initList (slist, svec);

return 0;
}
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-3-6 14:00:01
| Show all posts
thank you!
Is there any written in c, my brother is polite again!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list