1 Replies - 817 Views - Last Post: 23 March 2009 - 09:50 PM Rate Topic: -----

#1 waterspark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-March 09

parse a file and put the specified integers in arrays

Posted 23 March 2009 - 05:33 PM

Hey guys,

I'm trying to parse a file to apply the banker's algorithm to in unix shell. The format of the file is as follows:

# resources
# processes
Process_# R0_alloc R1_alloc .. R0_max R1_max .. R0_avail R1_avail .. [R0_req R1_req ..]


What I've got so far is
 cin>>argv[1];
ifstream input(argv[1]);

string line;
while(getline(input, line))
{
// parse the string into different sections divided by a space and put those in arrays
}


Sample input should be something like this:
4
8
0   1 2 3 4   4 4 4 4   0 0 0 1   1 2 1 0
1   4 3 2 1   4 4 4 4				  0 2 2 0
2   0 0 0 3   1 2 2 3				 0 0 1 0
3   3 0 0 0   4 0 0 2				0 0 0 1
4   1 1 2 1   3 3 2 4			   0 0 0 1
5   0 0 2 2   0 0 2 3			   0 0 0 2
6   2 0 2 0   2 0 2 2			   1 0 0 0
7   1 1 1 1   2 2 2 1			  1 1 0 0



I don't know how to convert the string into integers and put them in arrays. Also the last array 'req' might not be specified, I don't know how to handle that case either.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: parse a file and put the specified integers in arrays

#2 bodom658   User is offline

  • Villiage Idiom
  • member icon

Reputation: 114
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: parse a file and put the specified integers in arrays

Posted 23 March 2009 - 09:50 PM

go look at the documentation for the <sstream> header file. it allows the programmer to treat a string as if it were an istream. for example,

string str = "1000";
int myInt;
istringstream iss(str);
iss >> myInt;
cout << myInt;



the output should be 1000
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1