reading from file

hello
i want to read from a file, i know how to read line by line or word by word ,,, iwant combination of them,,
for example,, if i have file named "data",, that contian
1
2
3
4
2 2 m3 m5
4 8 2 m1 
8 1 m7


to read the file i write this code

1
2
3
4
5
6
7
8
9
10
ifstream fin("data.txt");
string s;
//if i want to read word by word
while (fin>>s)
cout<<s<<endl;

// if i want to read line by line

while (getline(fin,s))
cout<<s<<endl;



what about if i want to read each word in each line,, so i can manipulate each line separetly ,, attention that the number of words differ from line to line,,,
i mean i want to read the first line which is 2 2 m3 m5,, then manipulate each word in this line separeatly,, after finishing i can read the second line with its words,,,

i hope my question is clear :)





A way of doing that could be:
- get the line from the file into a string
- use the string to initialize a stringstream ( http://www.cplusplus.com/reference/iostream/stringstream/ )
- use that stringstream to read the single words
- start again with the next line
i think u mean some kind of tokenizing ??

use that stringstream to read the single words

do umean by loop cutting the line wordes??
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ifstream fin("data.txt");


string s;
//  read line by line

while (getline(fin,s))
{
//now reading line words

do
{
string subs;
iss >> subs;
cout << "Substring: " << subs << endl;
} while (s);

}



i hope it will work

please if u have better recmmendation, , please help
Last edited on
You only need to declare string subs; once -- so put it out of the do... while loop.

Here's a link for a topic I made a few days ago. It has some good examples written by Duoas of things you can do: http://cplusplus.com/forum/beginner/11402/

I was able to do this:
1
2
charges >> sentence_murder;       charges.ignore(99, '\n');
charges >> sentence_manslaughter; charges.ignore(99, '\n')


or

1
2
3
4
5
6
7
8
9
10
11
12
template <typename CharType, typename CharTraits>
std::basic_istream <CharType, CharTraits> &
endl(
  std::basic_istream <CharType, CharTraits> & ins
  ) {
  return ins.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  }

...

charges >> sentence_murder       >> endl
        >> sentence_manslaughter >> endl;



And that way I was able to read each line and then choose one based on my input.
thanks Bazzy and chrisname

i see the link and it is helpful,, i make the code like this and it works ,, but i will surly see ur link

the code become:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main()
	{
	
	ifstream fin("data");
	string s;
	string subs;
	while (getline(fin,s))
	{
		istringstream iss(s);
		cout<<"\n****************************\n	reading new line		"<< s <<endl;
		while(iss)
		  {
				iss >> subs;

	//adding operations for each word
		}//end 2ed while

	}//end 1st while

		
	}
	return 0;
		}//end main




Topic archived. No new replies allowed.